当前位置:网站首页>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,"退出成功");
}
}
边栏推荐
猜你喜欢
随机推荐
[NCTF2019]SQLi-1||SQL注入
2022/8/2 考试总结
牛客网剑指offer刷题练习之链表中环的入口结点
esp32和ros2基础篇草稿-micro-ros-
阿里云增强版实人认证--银行卡要素核验
浅谈敏捷开发
letcode 第20题-有效的括号
10. SAP ABAP OData 服务如何支持修改(Update)操作
【SQL】—数据库操作、表操作
DB2数据库-获取表结构异常:[jcc][t4][1065][12306][4.26.14]CharConvertionException ERRORCODE=-4220,SQLSTATE=null
Vite教程 安装
如何正确地配置入口文件?
TensorFlow学习记录(一):基本介绍
Day117. Shangyitong: Generate registered order module
Go高性能之方法接收器 - 指针vs值
在表格数据上,为什么基于树的模型仍然优于深度学习?
图文详细解决IDEA使用Debug模式启动项目一直转圈圈跑起不来(亲测可以)
.NET深入解析LINQ框架(四:IQueryable、IQueryProvider接口详解)
和睦家私有化后换帅:新风天域吴启楠任CEO 李碧菁靠边站
【遥控器开发基础教程4】疯壳·开源编队无人机-SPI(OLED)