遇见情况如下:
在添加jwtfilter之前,前端能正常接收到数据,响应体如下:
请求头:
响应:
在添加jwtfilter之后就出现没有响应数据的情况了:
请求头:
响应:
JwtFilter代码如下:
@Slf4j
//@Component
public class JwtFilter extends OncePerRequestFilter {
@Autowired
RedisCache redisCache;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String originalHeader=request.getHeader("Origin");
if(ObjectUtils.isNotEmpty(originalHeader)){
response.addHeader("Access-Control-Allow-Origin",originalHeader);
response.addHeader("Access-Control-Allow-Credentials","true");
// response.addHeader("Access-Control-Allow-Methods","POST,GET,OPTIONS,DELETE,PUT");
response.addHeader("Access-Control-Allow-Methods","POST,GET,DELETE,PUT");
response.addHeader("Access-Control-Allow-Headers",request.getHeader("Access-Control-Request-Headers"));
}
String token = request.getHeader("Token");
if (Objects.isNull(token) || token.trim().isEmpty()) {
log.info("无token进入");
filterChain.doFilter(request, response);
return;
}
CacheObject cacheObject = (CacheObject) redisCache.getCacheObject("login:" + token);
if (Objects.isNull(cacheObject)) {
throw new GlobalException("无效jwt");
}
// CacheObject cacheObject1 = (CacheObject) cacheObject;
this.setSecurityContext(cacheObject);
log.info("有token进入");
filterChain.doFilter(request, response);
}
private void setSecurityContext(CacheObject info) {
SecurityContext emptyContext = SecurityContextHolder.createEmptyContext();
emptyContext.setAuthentication(new UsernamePasswordAuthenticationToken(info.getId(),info,info.getRoles().stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList())));
SecurityContextHolder.setContext(emptyContext);
}
}
但是在debug期间发现数据从数据库里面读取出来了,就是返回前端没数据:
并且直接访问和swagger里面有数据:
页面直接访问:
swagger访问: