feat: 新增sercurity的LogoutHandler的实现类

This commit is contained in:
weihongyang 2022-06-23 11:51:19 +08:00
rodzic 7fe617df56
commit 47409787a4

Wyświetl plik

@ -0,0 +1,32 @@
package com.cnbm.admin.handler;
import com.cnbm.admin.entity.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @Author weihongyang
* @Date 2022/6/23 11:24 AM
* @Version 1.0
*/
@Component
public class LogoutHandlerImpl implements LogoutHandler {
@Autowired
private RedisTemplate redisTemplate;
@Override
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
//获取SecurityContextHolder中的用户id
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
String userid = loginUser.getSysUserEntity().getId().toString();
//删除redis中的值
redisTemplate.delete("login:"+userid);
}
}