Shiro学习(八) Shiro 自动登录

1、在spring.xml添加配置


        
    

    
        
        
        
    

2、在页面添加选择框

 记住我 

3、在UserController添加token.setRememberMe(user.getRememberMe());

@RequestMapping(value = "/subLogin",method = RequestMethod.POST,
            produces = "application/json;charset=utf-8")
    @ResponseBody
    public String subLogin(User user){
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());

        try {
            token.setRememberMe(user.getRememberMe());
            subject.login(token);
        } catch (AuthenticationException e) {
           return e.getMessage();
        }
//        return "登陆成功";
        if (subject.hasRole("admin")){
            return "有admin权限";
        }

        return "无admin权限";
    }

你可能感兴趣的:(Shiro学习(八) Shiro 自动登录)