JWT Filter
package com.zuijin.vue2blog.component;
import com.zuijin.vue2blog.common.utils.JwtUtil;
import com.zuijin.vue2blog.service.UserComDetailsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
@Value("${jwt.tokenHeader}")
private String tokenHeader;
@Value("${jwt.tokenHead}")
private String tokenHead;
@Resource
private UserComDetailsService userComDetailsService;
@Resource
private JwtUtil jwtUtil;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
System.out.println("请求方法为:" + request.getMethod());
String authorization = request.getHeader(this.tokenHeader);
System.out.println("JwtAuthenticationTokenFilter--Authorization : " + authorization);
if (authorization == null) {
System.out.println("JwtAuthenticationTokenFilter--Authorization的确是为 null");
}
if (authorization != null) {
System.out.println("JwtAuthenticationTokenFilter--Authorization 不是 null, 而是:" + authorization + "类型为: " + authorization.getClass().getTypeName());
String username = jwtUtil.getUsernameFromToken(authorization);
System.out.println("JwtAuthenticationTokenFilter--username: " + username);
if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
UserDetails userDetails = userComDetailsService.loadUserByUsername(username);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}
}
filterChain.doFilter(request, response);
}
}
预检请求时 axios不会经过请求拦截器,所以Authorization字段正常为 null 但是当 真正的登录操作时,请求会经过拦截器,而拦截器中直接这样写
config.headers.Authorization = localStorage.getItem('token');
,所以当 token 为 null 时,后端获取到的就是一个为 “null” 的字符串,而不是空值。 前端应该对 token 进行判空 (同时在登录操作之前应该清除 token)
改写如下:
let authorization = localStorage.getItem('token');
if (authorization !== null) {
config.headers.Authorization = authorization;
}
判断后
|