shiro实现登陆的过程
shiro实现登陆的过程:
大致
1,实例化token
2, token放入Subject登陆
1,自定义登陆
1,实例化token
2, token放入Subject登陆
Subject currentUser = SecurityUtils.getSubject(); // 获取当前的Subject
UsernamePasswordToken token = new UsernamePasswordToken(username, password); // 为了验证登录用户而封装的token
token.setRememberMe(true);// 设计记住用户
currentUser.login(token);
if (currentUser.isAuthenticated()) {
System.out.println("用户[" + username + "]登录认证通过");
}
自定义token登陆的时候就不会调用复写的createToken方法
2,使用框架的的登陆
1,实例化token
2, token放入Subject登陆
public abstract class AuthenticatingFilter extends AuthenticationFilter {
protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception {
AuthenticationToken token = this.createToken(request, response);
if(token == null) {
String e1 = "createToken method implementation returned null. A valid non-null AuthenticationToken must be created in order to execute a login attempt.";
throw new IllegalStateException(e1);
} else {
try {
Subject e = this.getSubject(request, response);
e.login(token);
return this.onLoginSuccess(token, e, request, response);
} catch (AuthenticationException var5) {
return this.onLoginFailure(token, var5, request, response);
}
}
}
}
public class MyAuthenticationFilter extends FormAuthenticationFilter{
@Override
protected org.apache.shiro.authc.AuthenticationToken createToken(ServletRequest servletRequest, ServletResponse servletResponse) {
String username = getUsername(servletRequest);
String password = getPassword(servletRequest);
String captchaId = getCaptchaId(servletRequest);
String captcha = getCaptcha(servletRequest);
boolean rememberMe = isRememberMe(servletRequest);
if(!rememberMe){
rememberMe=true;
}
String host = getHost(servletRequest);
String validateCode = (String)((HttpServletRequest) servletRequest).getSession().getAttribute("validateCode");;
return new AuthenticationToken( username, password,
captchaId, captcha, validateCode,
rememberMe, host) ;
}
}