22/03/2015, 17:49
|
|
Respuesta: Consulta Parte 1 Muchas gracias por responder,
tengo los siguientes .java: Archivo módulo login:
import java.io.IOException;
import java.util.Map;
import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackEx ception;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;
/**
* Login module that simply matches name and password to perform authentication.
* If successful, set principal to name and credential to "admin".
*
* @author Nicolas Fränkel
* @since 2 avr. 2009
*/
public class PlainLoginModule implements LoginModule {
/** Callback handler to store between initialization and authentication. */
private CallbackHandler handler;
/** Subject to store. */
private Subject subject;
/** Login name. */
private String login;
/**
* This implementation always return false.
*
* @see javax.security.auth.spi.LoginModule#abort()
*/
@Override
public boolean abort() throws LoginException {
return false;
}
/**
* This is where, should the entire authentication process succeeds,
* principal would be set.
*
* @see javax.security.auth.spi.LoginModule#commit()
*/
@Override
public boolean commit() throws LoginException {
try {
PlainUserPrincipal user = new PlainUserPrincipal(login);
PlainRolePrincipal role = new PlainRolePrincipal("tomcat");
subject.getPrincipals().add(user);
subject.getPrincipals().add(role);
return true;
} catch (Exception e) {
throw new LoginException(e.getMessage());
}
}
/**
* This implementation ignores both state and options.
*
* @see javax.security.auth.spi.LoginModule#initialize(jav ax.security.auth.Subject,
* javax.security.auth.callback.CallbackHandler, java.util.Map,
* java.util.Map)
*/
@Override
public void initialize(Subject aSubject, CallbackHandler aCallbackHandler, Map aSharedState, Map aOptions) {
handler = aCallbackHandler;
subject = aSubject;
}
/**
* This method checks whether the name and the password are the same.
*
* @see javax.security.auth.spi.LoginModule#login()
*/
@Override
public boolean login() throws LoginException {
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("login");
callbacks[1] = new PasswordCallback("password", true);
try {
handler.handle(callbacks);
String name = ((NameCallback) callbacks[0]).getName();
String password = String.valueOf(((PasswordCallback) callbacks[1]).getPassword());
if (!name.equals(password)) {
throw new LoginException("Authentication failed");
}
login = name;
return true;
} catch (IOException e) {
throw new LoginException(e.getMessage());
} catch (UnsupportedCallbackException e) {
throw new LoginException(e.getMessage());
}
}
/**
* Clears subject from principal and credentials.
*
* @see javax.security.auth.spi.LoginModule#logout()
*/
@Override
public boolean logout() throws LoginException {
try {
PlainUserPrincipal user = new PlainUserPrincipal(login);
PlainRolePrincipal role = new PlainRolePrincipal("tomcat");
subject.getPrincipals().remove(user);
subject.getPrincipals().remove(role);
return true;
} catch (Exception e) {
throw new LoginException(e.getMessage());
}
}
} Archivo Módulo Usuario rol:
import java.security.Principal;
public class PlainRolePrincipal implements Principal {
String roleName;
public PlainRolePrincipal(String name) {
roleName = name;
}
public String getName() {
return roleName;
}
public String toString() {
return ("RolePrincipal: " + roleName);
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof PlainRolePrincipal) {
PlainRolePrincipal other = (PlainRolePrincipal) obj;
return roleName.equals(other.roleName);
}
return false;
}
public int hashCode() {
return roleName.hashCode();
}
} Módulo usuario:
import java.security.Principal;
public class PlainUserPrincipal implements Principal {
String UserName;
public PlainUserPrincipal(String name) {
UserName = name;
}
public String getName() {
return UserName;
}
public String toString() {
return ("UserPrincipal: " + UserName);
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof PlainUserPrincipal) {
PlainUserPrincipal other = (PlainUserPrincipal) obj;
return UserName.equals(other.UserName);
}
return false;
}
public int hashCode() {
return UserName.hashCode();
}
}
Lo requerido es modificarlo de manera tal que permita:
1. tener usuarios predeterminados con claves predeterminadas: Personal- Cargo- Login- Password
Ned Stark- Gerente- nstark- nstark79
Daenerys Targaryen- Encargado Personal- dtargaryen- dtargaryen79
Tyrion Lannister- Encargado Ventas- tlannister- tlannister79
Petyr Baelish- Encargado Compras- pbaelish- pbaelish79
Ramsay Snow- Mantenimiento- rsnow- rsnow79
2. La aplicación Web a desarrollar debe permitir que sólo los usuarios registrados
puedan acceder a la aplicación con su login y contraseña. A partir de este
momento tienen que visualizar un menú con todos los módulos mencionados
(nóminas, compras y ventas), pero por cada módulo sólo se mostrarán las opciones a
las que tiene acceso (según los roles a los que pertenece). Si hay un módulo al que el
usuario no tiene acceso a ninguna funcionalidad, se debe mostrar un mensaje
indicando que no tiene acceso dicho módulo.
Quedo atento a sus comentarios. Muchas gracias. |