首页 > 解决方案 > SuccessAuthentication 中的 AbstractAuthenticationProcessingFilter 链

问题描述

您好,我有过滤器可以从 JWT 获取自动化

public class JwtAuthorizationFilter extends AbstractAuthenticationProcessingFilter {


public JwtAuthorizationFilter() {
    super("/**");
}

@Override
public void setAuthenticationSuccessHandler(AuthenticationSuccessHandler successHandler) {
    super.setAuthenticationSuccessHandler(successHandler);
}


@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
    super.successfulAuthentication(request, response, chain, authResult);

    chain.doFilter(request, response);
}

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
    String token = request.getHeader("Authorization");
     //code
    return getAuthenticationManager().authenticate(getAuthentication(token));
}

private UsernamePasswordAuthenticationToken getAuthentication(String token) {
  // code

}

问题是,当我达到successfulAuthentication并执行chain.doFilter时,我在路径[]的上下文中为servlet [dispatcherServlet]收到异常“servlet.service()”抛出异常[请求处理失败;嵌套异常是java.lang.IllegalStateException:无法调用sendError() after response has been commited] with root cause" 而且我无法获得我的端点。我还注意到,即使我只登录一次,authResult 也会生成许多具有相同数据的(对象?)

@Edit我现在注意到,在成功授权后,弹簧试图到达我的控制器几次。第一个返回值,但其他只是抛出错误,我不知道为什么我有这个循环

标签: springspring-security

解决方案


里面做了什么super.successfulAuthentication?如果您对HttpServletResponse对象进行任何修改,例如,通过更改 Http 状态代码,或者ResponseEntity您将无法通过调用继续执行链中的下一个过滤器,chain.doFilter(request, response)因为响应已提交并返回给客户端。


推荐阅读