当前位置:网站首页>12-security退出.md
12-security退出.md
2022-08-03 00:34:00 【张 邵】
我们只需要定义一个登出接口,然后获取SecurityContextHolder中的认证信息,删除redis中对应的数据即可。
/** * @Author 三更 B站: https://space.bilibili.com/663528522 */
@Service
public class LoginServiceImpl implements LoginServcie {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private RedisCache redisCache;
@Override
public ResponseResult login(User user) {
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getUserName(),user.getPassword());
Authentication authenticate = authenticationManager.authenticate(authenticationToken);
if(Objects.isNull(authenticate)){
throw new RuntimeException("用户名或密码错误");
}
//使用userid生成token
LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
String userId = loginUser.getUser().getId().toString();
String jwt = JwtUtil.createJWT(userId);
//authenticate存入redis
redisCache.setCacheObject("login:"+userId,loginUser);
//把token响应给前端
HashMap<String,String> map = new HashMap<>();
map.put("token",jwt);
return new ResponseResult(200,"登陆成功",map);
}
@Override
public ResponseResult logout() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
Long userid = loginUser.getUser().getId();
redisCache.deleteObject("login:"+userid);
return new ResponseResult(200,"退出成功");
}
}
边栏推荐
- 【图像分类】2022-MPViT CVPR
- Auto.js special positioning control method cannot perform blocking operations on the ui thread, please use setTimeout instead
- 【多线程】线程与进程、以及线程进程的调度
- flutter空安全问题,平时用到的数据一定要注意
- 线上交流丨稀疏神经网络:实践和理论(青源Talk第23期 汪张扬)
- 【Leetcode】305.岛屿数量II(困难)
- 机电设备制造企业,如何借助ERP系统做好客供料管理?
- 流程控制for和while循环语句
- DB2数据库-获取表结构异常:[jcc][t4][1065][12306][4.26.14]CharConvertionException ERRORCODE=-4220,SQLSTATE=null
- 7.31
猜你喜欢
随机推荐
基于rt-thread studio的STM32裸机开发——LED
minio 单机版安装
Heartwarming AI Review (1)
SAP ABAP Gateway Client 里 OData 测试的 PUT, PATCH, MERGE 请求有什么区别
mysql容器数据卷持久化
写一个简单的网站步骤
增删改查这么多年,最后栽在MySQL的架构设计上!
华为防火墙双机热备技术:HRP、VGMP、VRRP,三大技术值得一学!
高并发基石:多线程、守护线程、线程安全、线程同步、互斥锁,一文扫尽!...
Vite教程 安装
线上交流丨稀疏神经网络:实践和理论(青源Talk第23期 汪张扬)
【多线程】Thread类的基本用法
random.nextint()详解
Understand the next hop address in the network topology in seconds
GTK实现水波纹效果
阿南的对话
如何修复 SAP UI5 aggregation with cardinality 0..1 相关的错误消息
matlab常微分方程在传染病建模中的应用
2022年8月2日——使用idea搭建servlet+jsp项目
文树勋率长沙市人大常委会主任会议成员莅临麒麟信安调研数字经济发展情况









