package apache.shrio;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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;
public class SimpleCallbackHandler implements CallbackHandler {
@Override
public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
// TODO Auto-generated method stub
for(Callback cb:callbacks) {
if(cb instanceof NameCallback) {
NameCallback nc = (NameCallback)cb;
System.out.println(nc.getPrompt());
System.out.flush();
nc.setName(new BufferedReader(new InputStreamReader(System.in)).readLine());
} else if(cb instanceof PasswordCallback) {
PasswordCallback pc = (PasswordCallback)cb;
System.out.println(pc.getPrompt());
System.out.flush();
pc.setPassword(new BufferedReader(new InputStreamReader(System.in)).readLine().toCharArray());
}
}
}
}
package apache.shrio;
import java.io.IOException;
import java.security.Principal;
import java.util.Map;
import java.util.Set;
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.UnsupportedCallbackException;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;
public class SimpleLoginModule implements LoginModule {
private String userName;
private char[] password;
private Subject subject;
private DemoPrincipal principal;
private CallbackHandler callbackHandler;
private Map sharedState;
private Map options;
private String debug;
@Override
public void initialize(Subject subject, CallbackHandler callbackHandler,
Map<String, ?> sharedState, Map<String, ?> options) {
// TODO Auto-generated method stub
this.subject = subject;
this.callbackHandler = callbackHandler;
this.sharedState = sharedState;
this.options = options;
this.debug = (String)options.get("debug");
}
@Override
public boolean login() throws LoginException {
// TODO Auto-generated method stub
Callback[] cbs = new Callback[2];
cbs[0] = new NameCallback("用户名:");
cbs[1] = new PasswordCallback("密码:",false);
try {
callbackHandler.handle(cbs);
userName = ((NameCallback)cbs[0]).getName();
password = ((PasswordCallback)cbs[1]).getPassword();
if(debug.equals("true")) {
System.out.println("你输入的用户名为:"+userName);
System.out.println("你输入的密码为:"+new String(password));
}
if(userName.equals("test") && new String(password).equals("123123")) {
System.out.println("验证成功");
principal = new DemoPrincipal(userName);
return true;
} else {
System.out.println("验证失败");
userName = null;
password = null;
return false;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedCallbackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
@Override
public boolean commit() throws LoginException {
// TODO Auto-generated method stub
System.out.println("commit()");
Set<Principal> principals = subject.getPrincipals();
if(!principals.contains(principal)) {
subject.getPrincipals().add(principal);
}
return true;
}
@Override
public boolean abort() throws LoginException {
// TODO Auto-generated method stub
System.out.println("abort()");
return false;
}
@Override
public boolean logout() throws LoginException {
// TODO Auto-generated method stub
System.out.println("logout()");
return false;
}
/*public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public char[] getPassword() {
return password;
}
public void setPassword(char[] password) {
this.password = password;
}
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
public CallbackHandler getCallbackHandler() {
return callbackHandler;
}
public void setCallbackHandler(CallbackHandler callbackHandler) {
this.callbackHandler = callbackHandler;
}
public Map getSharedState() {
return sharedState;
}
public void setSharedState(Map sharedState) {
this.sharedState = sharedState;
}
public Map getOptions() {
return options;
}
public void setOptions(Map options) {
this.options = options;
}
public String getDebug() {
return debug;
}
public void setDebug(String debug) {
this.debug = debug;
}
*/
}
package apache.shrio;
import java.security.Principal;
public class DemoPrincipal implements Principal {
private String name;
public DemoPrincipal(String name) {
this.name = name;
}
@Override
public String getName() {
// TODO Auto-generated method stub
return name;
}
}
package apache.shrio;
import java.security.Principal;
import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
public class TestJAAS {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("java.security.auth.login.config", "F:\\workspace\\shrio\\src\\main\\java\\jaas.config");
try {
LoginContext lc = new LoginContext("test",new SimpleCallbackHandler());
lc.login();
Subject subject = lc.getSubject();
for(Principal p:subject.getPrincipals()) {
System.out.println(p.getName());
}
System.out.println("登录成功");
} catch (LoginException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
test {
apache.shrio.SimpleLoginModule required debug=true;
};