【发布时间】:2019-02-11 20:49:40
【问题描述】:
我需要一些帮助。
我在 SPRING Tool Suite 上构建了一个 API,它会在发布任何对 API 的操作之前验证 JWT。 那是我的 JWT 过滤器:
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain filterChain
) throws IOException, ServletException {
try {
Authentication authentication = TokenAuthenticationService.getAuthentication((HttpServletRequest) request);
if (authentication == null) {
System.out.println("auth null");
((HttpServletResponse) response).sendError(BackEndUtilities.INVALID_TOKEN, BackEndUtilities.ERROR_MSG_INVALID_TOKEN);
}
SecurityContextHolder.getContext().setAuthentication((Authentication) authentication);
}catch(ExpiredJwtException ex) {
((HttpServletResponse) response).sendError(BackEndUtilities.INVALID_TOKEN);
}
filterChain.doFilter(request, response);
}
当我在身份验证中获得空值时,我发送 HTTP 状态 401 以告知需要登录。
常量 BackEndUtilities.INVALID_TOKEN 是 int 值 401。我在 STS 服务器上运行 API 时得到这个值,但是当我在 TOMCAT 8.5 上部署 war 文件时,我不断得到 HTTP STATUS 403。
在这两种情况下,我都在控制台上获得了字符串“auth null”,它只是在身份验证为空时出现。但为什么 TOMCAT 8.5 正在“改变”状态?
有人知道为什么它一直在发生吗?
【问题讨论】: