【问题标题】:Spring intercept http request for authorization endpointSpring拦截授权端点的http请求
【发布时间】:2020-01-17 13:31:50
【问题描述】:

/oauth/authorize 上的http 请求之前我需要一个回调,我尝试在WebMvcConfigurerAdapter 上使用标准拦截器模式,如下所示:

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserAuthorizationInterceptor())
            .addPathPatterns("/oauth/authorize");
    }

// .... 

private class UserAuthorizationInterceptor implements HandlerInterceptor {

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println(); // breakpoint
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println(); // breakpoint
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true; // breakpoint
    }

由于某种原因,这不会触发 /oauth/** 的任何模式,但是如果我将路径匹配器替换为 .addPathPatterns("/**"); 之类的东西,它仍然会在大多数 http 请求 EXCEPT 上触发以oauth 开头。我怎样才能实现这个处理程序?

编辑

我尝试用全局过滤方法解决这个问题

@Component
@Order(1)
public class Foo implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        chain.doFilter(request,response); // breakpoint
    }

}

不过,对于每个 http 请求 EXCEPT /oauth/**,都会再次调用此过滤器。在我看来,oauth 请求根本没有传递到过滤器链

【问题讨论】:

  • 你检查我的答案了吗?

标签: java spring spring-security oauth interceptor


【解决方案1】:

可以通过Spring SecurityAuthorizationServerConfigurerAdapter添加拦截器:

public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

  @Override
  public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
    endpoints.addInterceptor(new CustomInterceptor());
  }

  private class CustomInterceptor implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) {
      ...
    }
  }
}

这样,您可以添加HandlerInterceptorWebRequestInterceptor,如果您愿意,也可以同时添加。

【讨论】:

    【解决方案2】:

    听起来你可能有一个.excludePathPatterns("/oauth/**").excludePathPatterns("/oauth/**"),因为当.addPathPatterns("/**"); 时你无法使用UserAuthorizationInterceptor 访问它

    【讨论】:

    • 不,不是那样的。
    猜你喜欢
    • 1970-01-01
    • 2018-07-11
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    相关资源
    最近更新 更多