1

私は、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 を介して ClientDetailsS​​ervice を提供することにより、クライアントを登録する必要があります。

これは素晴らしいことですが、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 セキュリティ用の同様のビルダー構文はありますか?

4

1 に答える 1

0

を使用して独自の構成を作成することになりました org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerSecurityConfiguration

私はSpringブートを使用しているので、SecurityPropertiesを自動配線し、OauthエンドポイントのSSLにこの行を追加しました

if (this.security.isRequireSsl()) {
    http.requiresChannel().anyRequest().requiresSecure();
}

そして、POST要件について

http .authorizeRequests() .antMatchers(HttpMethod.POST,tokenEndpointPath).fullyAuthenticated() .antMatchers(HttpMethod.GET,tokenEndpointPath).denyAll()

その後 @EnableAuthorizationServer を削除して、私の設定を使用するようにしました。

于 2014-07-31T18:44:49.863 に答える