Spring boot后台搭建二集成Shiro添加Remember Me
2020-12-13 03:26
YPE html>
标签:inline view spring mini cto remember lan rem toggle
上一片文章实现了用户验证 查看
当用户成功登录后,关闭浏览器,重新打开浏览器访问http://localhost:8080,页面会跳转到登录页,因为浏览器的关闭后之前的登录已失效
Shiro提供了Remember Me的功能,用户的登录状态不会因为浏览器的关闭而失效,直到Cookie过期
1.修改ShiroConfig
(1)添加
/** * cookie对象 * @return */ public SimpleCookie rememberMeCookie() { // 设置cookie名称,对应login.html页面的 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
Title class="login-page">class="form">class="form-toggle">class="form-panel one">class="form-header">账户登录
class="form-content">class="form-group">class="form-group">class="form-group" style="margin: 0px;">记住我
class="form-group">
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("认证失败!"); } }
rememberMe选中的时候,Shiro就会帮忙记住用户的登录状态
Spring boot后台搭建二集成Shiro添加Remember Me
标签:inline view spring mini cto remember lan rem toggle
原文地址:https://www.cnblogs.com/baby123/p/11074662.html
文章标题:Spring boot后台搭建二集成Shiro添加Remember Me
文章链接:http://soscw.com/essay/27610.html