私は、Spring Security Oauth を使用して Restful Service を保護することに取り組んできました。SSL を使用して /oauth/token エンドポイントを保護し、POST 呼び出しのみを許可しようとして頭を悩ませてきました。
私は @EnableAuthorizationServer を使用しています
DispatcherServlet コンテキストである必要がある現在のアプリケーション コンテキストで認可サーバー (つまり、AuthorizationEndpoint と TokenEndpoint) を有効にするための便利な注釈。サーバーの多くの機能は、タイプ AuthorizationServerConfigurer の @Beans を使用してカスタマイズできます (たとえば、AuthorizationServerConfigurerAdapter を拡張することによって)。ユーザーは、通常の Spring Security 機能 (@EnableWebSecurity など) を使用して認可エンドポイント (/oauth/authorize) を保護する責任がありますが、トークン エンドポイント (/oauth/token) は、クライアントの資格情報で HTTP 基本認証を使用して自動的に保護されます。1 つ以上の AuthorizationServerConfigurers を介して ClientDetailsService を提供することにより、クライアントを登録する必要があります。
これは素晴らしいことですが、intercept-url xml 構文のように、トークン エンドポイント部分をオーバーライドしたり、POST のみの呼び出しを強制したりすることはできないようです。
@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 を使用する Authorization Servers TokenEndpoint セキュリティ用の同様のビルダー構文はありますか?