Spring boot后台搭建二集成Shiro添加Remember Me

Posted baby123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring boot后台搭建二集成Shiro添加Remember Me相关的知识,希望对你有一定的参考价值。

上一片文章实现了用户验证  查看

当用户成功登录后,关闭浏览器,重新打开浏览器访问http://localhost:8080,页面会跳转到登录页,因为浏览器的关闭后之前的登录已失效

Shiro提供了Remember Me的功能,用户的登录状态不会因为浏览器的关闭而失效,直到Cookie过期

1.修改ShiroConfig

(1)添加

/**
     * cookie对象
     * @return
     */
    public SimpleCookie rememberMeCookie() 
        // 设置cookie名称,对应login.html页面的<input type="checkbox" name="rememberMe"/>
        SimpleCookie cookie = new SimpleCookie("rememberMe");
        // 设置cookie的过期时间,单位为秒,这里为一天
        cookie.setMaxAge(86400);
        return cookie;
    

    /**
     * cookie管理对象
     * @return
     */
    public CookieRememberMeManager rememberMeManager() 
        CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
        cookieRememberMeManager.setCookie(rememberMeCookie());
        // rememberMe cookie加密的密钥
        cookieRememberMeManager.setCipherKey(Base64.decode("4AvVhmFLUs0KTA3Kprsdag=="));
        return cookieRememberMeManager;
    

(2)将cookie管理对象设置到SecurityManager

  修改securityManager()为

@Bean
    public SecurityManager securityManager() 
        DefaultWebSecurityManager securityManager =  new DefaultWebSecurityManager();
        securityManager.setRealm(myShiroRealm());
        securityManager.setRememberMeManager(rememberMeManager());//新加
        return securityManager;
    

(3)改权限配置

  将ShiroFilterFactoryBean的

filterChainDefinitionMap.put("/**", "authc")

  修改为

filterChainDefinitionMap.put("/**", "user");

说明:

  user指的是用户认证通过或者配置了Remember Me记住用户登录状态后可访问

2.修改login.html

技术图片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@/css/login.css" type="text/css">
    <link rel="stylesheet" th:href="@css/iCheck/minimal/blue.css" type="text/css">
    <link rel="stylesheet" th:href="@css/app.css" type="text/css">
    <script th:src="@/js/jquery.min.js"></script>
</head>
<body>
<div class="login-page">
    <!-- Form-->
    <div class="form">
        <div class="form-toggle"></div>
        <div class="form-panel one">
            <div class="form-header">
                <h1>账户登录</h1>
            </div>
            <div class="form-content">
                <div class="form-group">
                    <label>用户名</label>
                    <input type="text" name="username" />
                </div>
                <div class="form-group">
                    <label>密码</label>
                    <input type="password" name="password" />
                </div>
                <div class="form-group" style="margin: 0px;">
                    <p>
                        <input type="checkbox" name="rememberme" />&nbsp;&nbsp;记住我</p>
                </div>
                <div class="form-group">
                    <button onclick="login()" id="loginButton">登录</button>
                </div>
            </div>
        </div>
    </div>

</div>
</body>
<script th:inline="javascript">
    var ctx = [[@/]];
        function login() 
            var username = $("input[name=‘username‘]").val();
            var password = $("input[name=‘password‘]").val();
            var rememberMe = $("input[name=‘rememberMe‘]").is(‘:checked‘);
            $.ajax(
                type: "post",
                url: ctx + "login",
                data: "username": username,"password": password,"rememberMe": rememberMe,
                dataType: "json",
                success: function (r) 
                    if (r.code == 0) 
                        location.href = ctx + ‘index‘;
                     else 
                        alert(r.msg);
                    
                
            );
        
</script>
</html>
View Code

3.修改LoginController

技术图片
@PostMapping("/login")
    @ResponseBody
    public ResponseBo login(String username, String password, Boolean rememberMe) 
        // 密码MD5加密
        password = MD5Utils.encrypt(username, password);
        System.out.println(password);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password,rememberMe);
        // 获取Subject对象
        Subject subject = SecurityUtils.getSubject();
        try 
            subject.login(token);
            return ResponseBo.ok();
         catch (UnknownAccountException e) 
            return ResponseBo.error(e.getMessage());
         catch (IncorrectCredentialsException e) 
            return ResponseBo.error(e.getMessage());
         catch (LockedAccountException e) 
            return ResponseBo.error(e.getMessage());
         catch (AuthenticationException e) 
            return ResponseBo.error("认证失败!");
        
    
View Code

rememberMe选中的时候,Shiro就会帮忙记住用户的登录状态

 

以上是关于Spring boot后台搭建二集成Shiro添加Remember Me的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot 入门:集成 Shiro 实现登陆认证和权限管理

spring boot 集成shiro,设置好setLoginUrl后,报404

Shiro:Spring-boot如何集成Shiro(上)

基于Spring Boot和Shiro的后台管理系统FEBS

Spring Boot 入门:集成 AOP 进行日志管理

Shiro - Shiro简介;Shiro与Spring Security区别;Spring Boot集成Shiro