码迷,mamicode.com
首页 > 其他好文 > 详细

shiro登陆认证

时间:2017-08-18 23:39:48      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:auth   direct   return   print   code   throw   creat   color   ber   

1.LoginController

@RequestMapping(method = RequestMethod.POST)
public String login(User user, HttpServletRequest request) {
    try {
        ubject subject = SecurityUtils.getSubject();
         UsernamePasswordToken token = new UsernamePasswordToken(user.getLoginName(), user.getPassword());
        token.setRememberMe(true);
        String vcode = request.getParameter("verifyCode");
        String verifyCode = subject.getSession().getAttribute(Global.SESSION_SECURITY_CODE).toString();

        if (vcode.equals(verifyCode)) {
            subject.login(token); //启动认证
        }
    } catch (Exception e) {
        e.printStackTrace();
        return "modules/sys/sysLogin";
    }
    return "redirect:index";
}            

2.AuthenticationInfo

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) {
        // 令牌——基于用户名和密码的令牌
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;

        // 令牌中可以取出用户名
        String username = token.getUsername();
        String password = String.valueOf(token.getPassword());

        // 让shiro框架去验证账号密码
        if (!StringUtils.isEmpty(username)) {

            User record = new User();
            record.setLoginName(username);
            
            User user = userService.queryOne(record);
            
            if (null != user) {
                String pwdEncrypt = CipherUtil.createPwdEncrypt(password, username);
                if (user.getPassword().equals(pwdEncrypt)) {
                    AuthenticationInfo info = new SimpleAuthenticationInfo(user.getLoginName(), password,
                            getName());
                    if (info != null) {
                        UserUtils.setSession(Global.SESSION_USER, user);
                    }
                    return info;
                } else {
                    throw new IncorrectCredentialsException(); /* 错误认证异常 */
                }
            } else {
                throw new UnknownAccountException(); /* 找不到帐号异常 */
            }
        } else {
            throw new AuthenticationException();
        }
    }

 

shiro登陆认证

标签:auth   direct   return   print   code   throw   creat   color   ber   

原文地址:http://www.cnblogs.com/fangts/p/7392434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!