回复
     OAuth2.0实战!玩转认证、资源服务异常自定义这些骚操作!(五)
love374
 发布于 2022-7-11 11:42
 浏览
 0收藏
5、测试
直接输入错误的秘钥,结果如下:
 
6、源码追踪
1、OAuthServerAuthenticationEntryPoint在何时调用?
OAuthServerAuthenticationEntryPoint这个过滤器继承了 AbstractAuthenticationProcessingFilter 这个抽象类,一切的逻辑都在 doFilter() 中,陈某简化了其中的关键代码如下:
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
   throws IOException, ServletException {
    try {
         //调用子类的attemptAuthentication方法,获取参数并且认证
   authResult = attemptAuthentication(request, response);
  }
  catch (InternalAuthenticationServiceException failed) {
            //一旦认证异常,则调用unsuccessfulAuthentication方法,通过failureHandler处理
   unsuccessfulAuthentication(request, response, failed);
   return;
  }
  catch (AuthenticationException failed) {
            //一旦认证异常,则调用unsuccessfulAuthentication方法,通过failureHandler处理
   unsuccessfulAuthentication(request, response, failed);
   return;
  }
  //认证成功,则调用successHandler处理
  successfulAuthentication(request, response, chain, authResult);
}
关键代码在 unsuccessfulAuthentication() 这个方法中,代码如下:
 
2、自定义的过滤器如何生效的?
这个就要看 AuthorizationServerSecurityConfigurer#configure() 这个方法了,其中有一段代码如下:
 
这段代码就是遍历添加的过滤器将其添加到过滤器链中,在BasicAuthenticationFilter这个过滤器之前。
添加到security的过滤器链中,这个过滤器自然会生效了。
3、为什么不能加.allowFormAuthenticationForClients()?
还是在 AuthorizationServerSecurityConfigurer#configure() 这个方法中,一旦设置了 allowFormAuthenticationForClients 为true,则会创建 ClientCredentialsTokenEndpointFilter,此时自定义的自然失效了。
 
文章转自公众号:码猿技术专栏
标签 
  已于2022-7-11 11:42:42修改
 
        赞
        
 
        收藏 
      
 回复
  相关推荐
 



















