当前位置:网站首页>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,"退出成功");
}
}
边栏推荐
猜你喜欢
随机推荐
浅谈敏捷开发
Oracle 暴跌,倒下了!
全栈---Proxy
写一个简单的网站步骤
letcode 第20题-有效的括号
优秀论文以及思路分析02
【遥控器开发基础教程4】疯壳·开源编队无人机-SPI(OLED)
线性DP
npm运行项目dependencies were not found: core-js/modules/es6.array.fill
机电设备制造企业,如何借助ERP系统做好客供料管理?
从 npm 切换到 pnpm,真香!
TensorFlow学习记录(一):基本介绍
和睦家私有化后换帅:新风天域吴启楠任CEO 李碧菁靠边站
【QT】自定义工程封装成DLL并如何调用(带ui界面的)
SAP ABAP Gateway Client 里 OData 测试的 PUT, PATCH, MERGE 请求有什么区别
Rasa 3.x study series - Rasa - Issues 4792 socket debug logs clog up debug feed study notes
2022年8月2日——使用idea搭建servlet+jsp项目
向往的生活
nmap: Bad CPU type in executable
全栈---JSONP