退出登录实现(Security)
由于登录流程中的一个认证过滤器中是根据请求头中的token解析获取userId,然后通过redis中的key获取到用户信息,然后把它存到SecurityContextHolder
中的,所以你的退出登录请求头中必须有token否则,你请求头中没有token或者redis中必须有用户信息否则直接放行了,就根本存入不到SecurityContextHolder
中,自然也无法在退出登录中从SecurityContextHolder
获取用户信息,也就没法删除redis中的用户信息进行退出登录
JwtAuthenticationTokenFilter
package com.mrs.filter;
import com.alibaba.fastjson.JSON;
import com.mrs.common.ResponseResult;
import com.mrs.common.enums.AppHttpCodeEnum;
import com.mrs.entity.LoginUser;
import com.mrs.utils.JwtUtil;
import com.mrs.utils.RedisCache;
import com.mrs.utils.WebUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;
/**
* description: JwtAuthenticationTokenFilter
* date: 2022/8/8 12:15
* author: MR.孙
*/
@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
@Autowired
private RedisCache redisCache;
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//获取请求头中的token
String token = request.getHeader("token");
if(!StringUtils.hasText(token)){
//如果请求头中没有token,说明该接口不需要登录,直接放行,让可以执行后面的过滤器
filterChain.doFilter(request,response);
return ;
}
//从token中解析获取userid
Claims claims =null;
try {
claims = JwtUtil.parseJWT(token);
} catch (Exception e) {
e.printStackTrace();
//出现异常说明token超时或者token被篡改(非法)
//抛出异常信息,响应告诉前端需要重新登录
ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
WebUtils.renderString(response, JSON.toJSONString(result));
return ;
}
//从redis中获取用户信息
String userId = claims.getSubject();
LoginUser loginUser = redisCache.getCacheObject("bloglogin:" + userId);
//判断redis中是否有用户信息
if(Objects.isNull(loginUser)){
//没有获取到说明登录过期,提示重新登录
ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
WebUtils.renderString(response, JSON.toJSONString(result));
return ;
}
//存入SecurityContextHolder
UsernamePasswordAuthenticationToken authenticationToken =
new UsernamePasswordAuthenticationToken(loginUser,null,null);
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
//放行
filterChain.doFilter(request,response);
}
}
SecurityConfig
@Override
protected void configure(HttpSecurity http) throws Exception {
http
//关闭csrf
.csrf().disable()
//不通过Session获取SecurityContext
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
// 对于登录接口 允许匿名访问
.antMatchers("/login").anonymous()
//注销接口需要认证才能访问
.antMatchers("/logout").authenticated()
//jwt过滤器测试用,如果测试没有问题吧这里删除了
.antMatchers("/link/getAllLink").authenticated()
// 除上面外的所有请求全部不需要认证即可访问
.anyRequest().permitAll();
//配置异常处理器
http.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.accessDeniedHandler(accessDeniedHandler);
//关闭默认的注销功能
http.logout().disable();
//把jwtAuthenticationTokenFilter添加到SpringSecurity的过滤器链中
http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
//允许跨域
http.cors();
}
logout
@Override
public ResponseResult logout() {
//获取token 解析获取userid
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
//获取userid
Long userId = loginUser.getUser().getId();
//删除redis中的用户信息
redisCache.deleteObject("bloglogin:"+userId);
return ResponseResult.okResult();
}
测试
- 登录
- 获取友链信息,必须是认证过的也就是携带token
- 退出登录
退出登录后在获取友链是不行的
为什么删除redis中的信息就实现退出登录了呢?
login
@Override
public ResponseResult login(User user) {
UsernamePasswordAuthenticationToken authenticationToken=
new UsernamePasswordAuthenticationToken(user.getUserName(),user.getPassword());
//因为AuthenticationManager会通过UserDetailsService去比对用户密码,UserDetailsService比对完后会
//AuthenticationManager.authenticate会有一个返回值,里面封装了UserDetails等信息
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);
//把用户信息存入到redis中
redisCache.setCacheObject("bloglogin:"+userId,loginUser);
//把token和userInfo封装返回
UserInfoVo userInfo = BeanCopyUtils.copyBean(loginUser.getUser(),UserInfoVo.class);
BlogUserLoginVo blogUserLoginVo = new BlogUserLoginVo(jwt, userInfo);
return ResponseResult.okResult(blogUserLoginVo);
}
当认证通过后,它就会生成token,因为我们的token是根据userid+一组字符串生成的redis的key,key对应的值是用户信息,上面是登录的代码实现只要你登录了redis中就会有用户信息。
然后就是在登录认证过滤器JwtAuthenticationTokenFilter
中会进行redis的一个检验,校验redis是否有用户信息,没有则说明没有登录,因为只要进入了登录接口就会存入redis,所以我们只需要删除redis中的用户信息即可实现退出登录。
补充知识点:因为SecurityConfig的configure
方法它有一个默认的退出功能请求地址/logout与我们的退出登录接口相冲突,所以禁用掉这个功能
并且logou接口也必须认证过才能访问,也就是携带token和必须是登录状态