当前位置:网站首页>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,"退出成功");
}
}
边栏推荐
猜你喜欢
随机推荐
【问题征集】向 iPod 之父、iPhone 联合设计者、Google Nest 创始人 Tony Fadell 提问啦
【图像分类】2022-MPViT CVPR
一套开源的可快速搭建自己的物联网/智能家居系统源码
Rasa 3.x study series - Rasa - Issues 4792 socket debug logs clog up debug feed study notes
定了!8月起,网易将为本号粉丝提供数据分析培训,费用全免!
【软考 系统架构设计师】软件架构设计① 软件架构的概念
高并发基石:多线程、守护线程、线程安全、线程同步、互斥锁,一文扫尽!...
1686. 石子游戏 VI
接口流量突增,如何做好性能优化?
电压传感器: 工作原理、类型及电路图
DB2数据库-获取表结构异常:[jcc][t4][1065][12306][4.26.14]CharConvertionException ERRORCODE=-4220,SQLSTATE=null
买了一瓶饮料
Wireshark数据抓包分析之传输层协议(TCP协议)
Go高性能之方法接收器 - 指针vs值
精心整理16条MySQL使用规范,减少80%问题,推荐分享给团队
开源聚力,共创未来 | 麒麟信安祝贺openKylin首个体验版正式发布!
和睦家私有化后换帅:新风天域吴启楠任CEO 李碧菁靠边站
浅谈敏捷开发
C语言:链表
【多线程】Thread类的基本用法









