当前位置:网站首页>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,"退出成功");
}
}
边栏推荐
猜你喜欢

吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第一节:深度学习概论

Auto.js special positioning control method cannot perform blocking operations on the ui thread, please use setTimeout instead

浅谈I2C知识

科捷智能冲刺科创板:年营收12.8亿 顺丰与日日顺是股东

智能合约安全-可重入攻击(SW107-Reentrancy)

稳压电源: 电路图及类型

Visual Studio中vim模拟器

【遥控器开发基础教程5】疯壳·开源编队无人机-SPI(2.4G 双机通信)

PAT甲级 1051 Pop Sequence

Auto.js 特殊定位控件方法 不能在ui线程执行阻塞操作,请使用setTimeout代替
随机推荐
【多线程】Thread类的基本用法
中科磁业IPO过会:年营收5.5亿 吴中平家族持股85%
浅谈敏捷开发
牛客网剑指offer刷题练习之链表中环的入口结点
async-await
麒麟信安邀您抢先看 | openEuler 志高远,开源汇智创未来-开放原子全球开源峰会欧拉分论坛最详细议程出炉
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
谷歌 Chrome 浏览器 104 正式版发布:加快网页加载,蓝牙 API 改进
定了!8月起,网易将为本号粉丝提供数据分析培训,费用全免!
Introduction to resubmit Progressive Anti-Duplicate Submission Framework
Auto.js 特殊定位控件方法 不能在ui线程执行阻塞操作,请使用setTimeout代替
优秀论文以及思路分析01
机电设备制造企业,如何借助ERP系统做好客供料管理?
SAP ABAP OData 服务如何支持修改(Update)操作试读版
文树勋率长沙市人大常委会主任会议成员莅临麒麟信安调研数字经济发展情况
基于rt-thread studio的STM32裸机开发——LED
一套开源的可快速搭建自己的物联网/智能家居系统源码
暴力递归到动态规划 06 (剑指 Offer II 095. 最长公共子序列)
聊聊 Nacos
npm运行项目dependencies were not found: core-js/modules/es6.array.fill