gpt4 book ai didi

Spring security BasicAuthenticationFilter 返回 403 而不是 401

转载 作者:行者123 更新时间:2023-12-04 15:24:31 30 4
gpt4 key购买 nike

我实现了JWT认证授权。一切正常,除了未经授权的场景

未授权场景:在不提供授权 token 的情况下对路由进行 http 调用。

结果:403 forbidden而不是unauthorized

这是我的代码:

@Override
protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
String header = req.getHeader(HEADER_STRING);

if (header == null || !header.startsWith(TOKEN_PREFIX)) {
chain.doFilter(req, res);
return;
}

UsernamePasswordAuthenticationToken authentication = getAuthentication(req);

SecurityContextHolder.getContext().setAuthentication(authentication);
chain.doFilter(req, res);
}

之后

    if (header == null || !header.startsWith(TOKEN_PREFIX)) {
chain.doFilter(req, res);
return;
}

执行,响应为403

这是我的完整类(class):

public class JWTAuthorizationFilter extends BasicAuthenticationFilter {

public JWTAuthorizationFilter(AuthenticationManager authenticationManager) {
super(authenticationManager);
}

@Override
protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
String header = req.getHeader(HEADER_STRING);

if (header == null || !header.startsWith(TOKEN_PREFIX)) {
chain.doFilter(req, res);
return;
}

UsernamePasswordAuthenticationToken authentication = getAuthentication(req);

SecurityContextHolder.getContext().setAuthentication(authentication);
chain.doFilter(req, res);
}

private UsernamePasswordAuthenticationToken getAuthentication(HttpServletRequest request) {

String token = request.getHeader(HEADER_STRING);
if (token != null) {

// setting the user in the security context
String user = JWT.require(Algorithm.HMAC512(SECRET.getBytes()))
.build()
.verify(token.replace(TOKEN_PREFIX, ""))
.getSubject();

if(user != null){
return new UsernamePasswordAuthenticationToken(user, null, new ArrayList<>());
}

return null;
}

return null;
}
}

备注:

我在 UsernamePasswordAuthenticationFilter 上遇到了同样的问题,我通过覆盖默认的 authenticationFailureHandler 解决了这个问题:

setAuthenticationFailureHandler(new JWTAuthenticationFailureHandler());

如何获得正确的 401 响应代码和正文?

谢谢!

最佳答案

如果您查看在身份验证失败时用 JWTAuthorizationFilter 覆盖的 BasicAuthenticationFilter 执行的操作,它会调用 authenticationEntryPoint.commence(request, response, failed) 发送 401

BasicAuthenticationEntryPoint

    public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException {
response.addHeader("WWW-Authenticate", "Basic realm=\"" + realmName + "\"");
response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
}

但是您有覆盖并返回 null 的行为。因此,请尝试以下操作之一:

  • 在返回 null 的地方抛出 BadCredentialsException
  • 执行 response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());

关于Spring security BasicAuthenticationFilter 返回 403 而不是 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62562633/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com