【问题标题】:Spring Boot OAuth2 with custom security filter带有自定义安全过滤器的 Spring Boot OAuth2
【发布时间】:2017-03-13 18:07:22
【问题描述】:

我有一个带有 OAuth2 授权和资源服务器的 Spring Boot 设置。用户可以通过向/oauth/token 发出 POST 请求来获取令牌。到目前为止,一切顺利。

但是,我不想通过 BASIC auth 而是通过自定义安全过滤器来保护 /oauth/token

我尝试了以下方法,但从未调用过 DemoAuthenticationFilter

@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
    // ...
    @Override
    public void configure(HttpSecurity http) throws Exception {
        // ...
        http.addFilterBefore(new DemoAuthenticationFilter(), BasicAuthenticationFilter.class);
        http.authorizeRequests().antMatchers("/oauth/token").authenticated();
    }
}

另外,如果我尝试将其添加到WebSecurityConfigurerAdapter,则过滤器仅在之后调用,请求通过 OAuth2 进行身份验证:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    // ...
    @Override
    protected void configure(HttpSecurity http) throws Exception {
       // ...
       http.addFilterBefore(new DemoAuthenticationFilter(), BasicAuthenticationFilter.class);
       http.authorizeRequests().antMatchers("/oauth/token").authenticated();
    }
}

一些如何实现这一点的简单示例将非常有帮助。谢谢!

【问题讨论】:

    标签: java spring-security spring-boot oauth-2.0 spring-security-oauth2


    【解决方案1】:
    @Configuration
    @EnableAuthorizationServer
    public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter {
    
        @Override
        public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
            oauthServer.allowFormAuthenticationForClients().addTokenEndpointAuthenticationFilter(new AligenieFilter());
        }
    
        //....
    }
    

    它对我有用。

    【讨论】:

      猜你喜欢
      • 2016-02-10
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      • 2014-08-27
      • 2020-01-10
      相关资源
      最近更新 更多