我一直致力于使用 Spring Security Oauth 保护 Restful 服务。我一直在努力尝试使用 SSL 保护 /oauth/token 端点并且只允许 POST 调用。
我正在使用 @EnableAuthorizationServer 声明
用于在当前应用程序上下文中启用授权服务器(即 AuthorizationEndpoint 和 TokenEndpoint)的便利注释,该上下文必须是 DispatcherServlet 上下文。可以使用 AuthorizationServerConfigurer 类型的 @Beans 自定义服务器的许多功能(例如,通过扩展 AuthorizationServerConfigurerAdapter)。用户负责使用普通 Spring Security 功能(@EnableWebSecurity 等)保护授权端点(/oauth/authorize),但令牌端点(/oauth/token)将使用客户端凭据上的 HTTP 基本身份验证自动保护。必须通过一个或多个 AuthorizationServerConfigurer 提供 ClientDetailsService 来注册客户端。
这很好,但我似乎无法覆盖令牌端点部分或强制执行仅 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 安全性是否有类似的构建器语法?