📄 defaultloginmodule.java
字号:
package org.j3de.security;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.NoSuchProviderException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.security.spec.InvalidKeySpecException;
import java.util.Map;
import javax.crypto.NoSuchPaddingException;
import javax.security.auth.Subject;
import javax.security.auth.spi.LoginModule;
import javax.security.auth.login.LoginException;
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.UnsupportedCallbackException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.j3de.exception.ExceptionHandler;
public class DefaultLoginModule implements LoginModule {
// initial state
private Subject subject;
private CallbackHandler callbackHandler;
private Map sharedState;
private Map options;
// the authentication status
private boolean succeeded = false;
private boolean commitSucceeded = false;
// username and password
private String username;
private char[] password;
// parameters
private String provider;
private String userDir;
// User Principals
private ConfigPrincipal configPrincipal;
public void initialize(Subject subject,
CallbackHandler callbackHandler,
Map sharedState,
Map options) {
this.subject = subject;
this.callbackHandler = callbackHandler;
this.sharedState = sharedState;
this.options = options;
this.userDir = (String)options.get("userdir");
this.provider = (String)options.get("provider");
}
public boolean login() throws LoginException {
// prompt for a username and password
if (callbackHandler == null)
throw new LoginException("Error: no CallbackHandler available " +
"to garner authentication information from the user");
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("j3de username: ");
callbacks[1] = new PasswordCallback("j3de password: ", false);
try {
callbackHandler.handle(callbacks);
username = ((NameCallback)callbacks[0]).getName();
char[] tmpPassword = ((PasswordCallback)callbacks[1]).getPassword();
if (tmpPassword == null) {
// treat a NULL password as an empty password
tmpPassword = new char[0];
}
password = new char[tmpPassword.length];
System.arraycopy(tmpPassword, 0,
password, 0, tmpPassword.length);
((PasswordCallback)callbacks[1]).clearPassword();
} catch (java.io.IOException ioe) {
throw new LoginException(ioe.toString());
} catch (UnsupportedCallbackException uce) {
throw new LoginException("Error: " + uce.getCallback().toString() +
" not available to garner authentication information " +
"from the user");
}
try {
configPrincipal = new ConfigPrincipal(provider, userDir, username, password);
succeeded = true; return true;
} catch (FileNotFoundException e) {
ExceptionHandler.handleException(e);
return false;
} catch (SAXException ex) {
ExceptionHandler.handleException(ex);
return false;
} catch (ParserConfigurationException ex) {
ExceptionHandler.handleException(ex);
throw new LoginException("Error: " + ex.toString());
} catch (InvalidKeyException ex) {
ExceptionHandler.handleException(ex);
throw new LoginException("Error: " + ex.toString());
} catch (NoSuchProviderException ex) {
ExceptionHandler.handleException(ex);
throw new LoginException("Error: " + ex.toString());
} catch (InvalidKeySpecException ex) {
ExceptionHandler.handleException(ex);
throw new LoginException("Error: " + ex.toString());
} catch (IOException ex) {
ExceptionHandler.handleException(ex);
throw new LoginException("Error: " + ex.toString());
} catch (NoSuchPaddingException ex) {
ExceptionHandler.handleException(ex);
throw new LoginException("Error: " + ex.toString());
} catch (NoSuchAlgorithmException ex) {
ExceptionHandler.handleException(ex);
throw new LoginException("Error: " + ex.toString());
}
}
public boolean logout() throws LoginException {
subject.getPrincipals().remove(configPrincipal);
succeeded = false;
succeeded = commitSucceeded;
username = null;
if (password != null) {
for (int i = 0; i < password.length; i++)
password[i] = ' ';
password = null;
}
configPrincipal = null;
return true;
}
public boolean commit() throws LoginException {
if (succeeded == false) {
return false;
} else {
try { // add a Principal (authenticated identity)
// to the Subject
// assume the user we authenticated is the SamplePrincipal
if (!subject.getPrincipals().contains(configPrincipal))
{ subject.getPrincipals().add(configPrincipal);
}
// in any case, clean out state
username = null;
for (int i = 0; i < password.length; i++)
password[i] = ' ';
password = null;
commitSucceeded = true;
return true;
} catch (Exception e) { e.printStackTrace(); return true; } }
}
public boolean abort() throws LoginException {
if (succeeded == false) {
return false;
} else if (succeeded == true && commitSucceeded == false) {
// login succeeded but overall authentication failed
succeeded = false;
username = null;
if (password != null) {
for (int i = 0; i < password.length; i++)
password[i] = ' ';
password = null;
}
configPrincipal = null;
} else {
// overall authentication succeeded and commit succeeded,
// but someone else's commit failed
logout();
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -