【发布时间】: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