我可以使用Spring Security的AbstractAuthenticationProcessingFilter来处理身份验证,作为/ oauth / authorize请求处理的一部分吗?

时间:2017-06-14 21:15:22

标签: java spring authentication spring-security oauth-2.0

在我们的应用程序中,我想使用Spring Security将身份验证过滤作为/ oauth / authorize请求的一部分来实现。我添加了过滤器和提供程序 - 扩展org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter - 以提供自定义身份验证。 但是我不知道在实现AuthenticationSuccessHandler#onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)时该怎么做,以便过滤请求不会导致对浏览器的重定向响应 - 而是程序继续到org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint#authorize(...)打算处理请求。

1 个答案:

答案 0 :(得分:0)

是的,这似乎是在到达Spring自己的/ oauth / authorize请求处理程序之前在过滤器链中进行身份验证的有效方法。

在我们的案例中,早期的工作已经设置了AuthorizationServerEndpointsConfigurer'端点映射干扰了我的身份验证成功处理程序尝试转发请求。

因此,我的实现包括一个自定义身份验证过滤器,它扩展了AbstractAuthenticationProcessingFilter,通过WebSecurityConfigurerAdapter集成到Spring安全过滤器链中,

@Override
protected void configure(HttpSecurity http) throws Exception {
http
// ... 
    .addFilterBefore(
        passwordLessAuthenticationFilter(authenticationManager()),
        UsernamePasswordAuthenticationFilter.class)
// ...

自定义PasswordLessAuthenticationFilter使用蚂蚁路径匹配器值构建,例如: " /富/ OAuth的/授权&#34 ;. 自定义过滤器被定义为存储对实例Spring的ForwardAuthenticationSuccessHandler的引用,其前向URL值为" oauth / authorize"。