SpringBoot整合Shiro实现RememberMe
接上Springboot Shiro实现用户验
shiro提供了RememberMe功能,用户登录状态不会因为浏览器的关闭而失效,知道cookie过期
更改ShiroConfig
加入cookie管理配置
/**
* 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;
}
将cookie管理对象配置加入到SecurityManager
@Beanpublic SecurityManager securityManager() {// 配置SecurityManager,并注入shiroRealmDefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();securityManager.setRealm(shiroRealm());//cookie管理配置对象securityManager.setRememberMeManager(rememberMeManager());return securityManager;}
修改过滤权限配置
将filterChainDefinitionMap.put("/**", "authc");
修改为 filterChainDefinitionMap.put("/**", "user");
因为已经通过验证,不需要在验证
authc与user有何不同
Controller
更改LoginController中的login()方法
@PostMapping("/login")
@ResponseBody
public ResponseBo login(String username, String password, Boolean rememberMe) {password = MD5Utils.encrypt(username, password);UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);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传入参数true时,shiro会帮我们记住用户的登录状态
SpringBoot整合Shiro实现RememberMe相关推荐
- springboot整合shiro和session的详细过程和自定义登录拦截器
文章目录 1.shiro依赖 2.shiro配置 shiro过滤器配置: 关联自定义的其他管理器 自定义会话工厂: 3.登陆时记录用户信息 4.shiro一些工具类的学习 5.自定义登录拦截器 shi ...
- SpringBoot整合Shiro实现登录认证和授权CHCache
文章目录 一. springboot实现普通登录 1 添加依赖 2 编写配置文件 3 新建实体类和mapper 4 编写业务层代码 5 编写控制器 6 编写启动类 7 编写登录页面和主页面 二. sp ...
- 补习系列(6)- springboot 整合 shiro 一指禅
欢迎添加华为云小助手微信(微信号:HWCloud002 或 HWCloud003),输入关键字"加群",加入华为云线上技术讨论群:输入关键字"最新活动",获取华 ...
- SpringBoot整合Shiro实现权限控制,验证码
本文介绍 SpringBoot 整合 shiro,相对于 Spring Security 而言,shiro 更加简单,没有那么复杂. 目前我的需求是一个博客系统,有用户和管理员两种角色.一个用户可能有 ...
- SpringBoot 整合Shiro 一指禅
目标 了解ApacheShiro是什么,能做什么: 通过QuickStart 代码领会 Shiro的关键概念: 能基于SpringBoot 整合Shiro 实现URL安全访问: 掌握基于注解的方法,以 ...
- springboot整合shiro(超详细,你想要的都在这了)
Springboot整合Shiro 文章目录 pom依赖 前端页面(thymeleaf整合shiro) thymeleaf中shiro标签解释 数据库(整合mybatis) 理解shiro的几个组成部 ...
- 补习系列- springboot 整合 shiro 一指禅
目标 了解ApacheShiro是什么,能做什么: 通过QuickStart 代码领会 Shiro的关键概念: 能基于SpringBoot 整合Shiro 实现URL安全访问: 掌握基于注解的方法,以 ...
- 补习系列(6)-SpringBoot 整合Shiro 一指禅
目标 了解ApacheShiro是什么,能做什么: 通过QuickStart 代码领会 Shiro的关键概念: 能基于SpringBoot 整合Shiro 实现URL安全访问: 掌握基于注解的方法,以 ...
- springboot整合shiro使用shiro-spring-boot-web-starter
此文章仅仅说明在springboot整合shiro时的一些坑,并不是教程 增加依赖 <!-- 集成shiro依赖 --> <dependency><groupId> ...
最新文章
- 防止IIS文件被下载方法
- 深度丨我们可以有多信任人工智能
- python gui打包exe pyinstaller打包运行失败 Failed to execute script pyi_rth_multiprocessing
- c include 多层目录_Rsync 秒杀一切备份工具,你能手动屏蔽某些目录吗?
- CNG 关于 Key 相关的操作
- pythonnumpy函数四舍五入_NumPy函数
- 将ubuntu的apache2修改成https链接
- 配置SCCM 2012 SP1(七)操作系统部署
- 演示:各种网络安全设备、***设备向微软证书服务器申请证书
- 自学如何去学习jQuery
- 微信小程序开发流程、开发者工具下载安装
- java 清屏函数_c++清屏函数是什么
- 那些经典好玩的在线游戏:魂斗罗,超级马里奥,坦克大战
- java程序设计大赛_2016年“小码哥杯”Java程序设计竞赛完美收官
- Windows系统如何远程桌面连接
- Scrapy 2.6 Requests and Responses 请求和响应使用指南
- access select max_从峰米4k激光家庭影院Max开始,爱上看电影
- 家庭网速单位以及单位换算详细解说
- 前端入门: 用css设置文字样式。
- php 倒计时抢购,限时抢购-倒计时_HTML/CSS_互联网开发技术网_传播最新的编程技术_php361.com...