【发布时间】:2014-09-15 05:18:30
【问题描述】:
我一直致力于使用 Spring Security Oauth 保护 Restful 服务。我一直在努力尝试使用 SSL 保护 /oauth/token 端点并且只允许 POST 调用。
我正在使用@EnableAuthorizationServer 声明
启用授权服务器的便利注释(即 当前应用程序中的 AuthorizationEndpoint 和一个 TokenEndpoint) context,它必须是 DispatcherServlet 上下文。的许多特点 可以使用 @Beans 类型自定义服务器 AuthorizationServerConfigurer(例如,通过扩展 AuthorizationServerConfigurerAdapter)。用户负责 使用普通方法保护授权端点 (/oauth/authorize) Spring Security 特性(@EnableWebSecurity 等),但 Token 端点 (/oauth/token) 将使用 HTTP Basic 自动保护 对客户端凭据进行身份验证。客户必须注册 通过一个或多个提供 ClientDetailsService AuthorizationServerConfigurers。
这很好,但我似乎无法覆盖令牌端点部分或强制执行仅 POST 调用,就像使用 intercept-url xml 语法一样
@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore()
}
@Autowired
AuthenticationManager authenticationManager
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints
.tokenStore(tokenStore())
.authenticationManager(authenticationManager);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient('testApp')
.scopes("read", "write")
.authorities('ROLE_CLIENT')
.authorizedGrantTypes("password","refresh_token")
.secret('secret')
.accessTokenValiditySeconds(7200)
}
}
我用
保护了我的资源服务器@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Autowired
private RestAuthenticationEntryPoint authenticationEntryPoint;
@Override
public void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.requiresChannel().anyRequest().requiresSecure()
.and()
.csrf()
.requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize"))
.disable()
.headers()
.frameOptions().disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/api/**").authenticated()
}
}
对于使用 requiresChannel 的授权服务器 TokenEndpoint 安全性,是否有类似的构建器语法?
【问题讨论】:
标签: spring-security spring-boot spring-security-oauth2