2017-03-06 7 views
0

私は、クライアント証明書の認可タイプを持つoauth2ベースのトークンをバネセッションベースの認証と統合しようとしていました。 oauthトークンと与えられた権限でうまくいきます。oauthトークンベースとセッションベースの認証が春にどのように連携しますか?

両方を組み合わせても機能しません。常にUsernamePasswordAuthenticationFilterを呼び出すが、OAuth2AuthenticationProcessingFilterは呼び出さない。

どのように連携させるか?ここでは、フィルタ・チェーンにおける私のResourceServer構成

@Configuration 
@EnableResourceServer 
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { 

    @Override 
    public void configure(ResourceServerSecurityConfigurer resources) { 
     resources.resourceId(SPARKLR_RESOURCE_ID).stateless(false); 
    } 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     // @formatter:off 
     http 
      // Since we want the protected resources to be accessible in the UI as well we need 
      // session creation to be allowed (it's disabled by default in 2.0.6) 
      .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) 
     .and() 
      .requestMatchers().antMatchers("/api/account/**", "/oauth/users/**", "/oauth/clients/**","/me") 
     .and() 
      .authorizeRequests() 
       .antMatchers("/api/account/**").access("#oauth2.hasScope('read') or (!#oauth2.isOAuth() and hasRole('ROLE_USER'))")     
       .regexMatchers(HttpMethod.DELETE, "/oauth/users/([^/].*?)/tokens/.*") 
        .access("#oauth2.clientHasRole('ROLE_CLIENT') and (hasRole('ROLE_USER') or #oauth2.isClient()) and #oauth2.hasScope('write')") 
       .regexMatchers(HttpMethod.GET, "/oauth/clients/([^/].*?)/users/.*") 
        .access("#oauth2.clientHasRole('ROLE_CLIENT') and (hasRole('ROLE_USER') or #oauth2.isClient()) and #oauth2.hasScope('read')") 
       .regexMatchers(HttpMethod.GET, "/oauth/clients/.*") 
        .access("#oauth2.clientHasRole('ROLE_CLIENT') and #oauth2.isClient() and #oauth2.hasScope('read')"); 
     // @formatter:on 
    } 
} 

問題があり、OAuth2AuthenticationProcessingFilterが呼び出さ取得されていないです。したがって、トークン検証は、残りの呼び出しでは起こりません。以下はフィルターチェーンです。

XNIO-2 task-1] o.s.security.web.FilterChainProxy  : /api/account at position 1 of 12 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter' 
XNIO-2 task-1] o.s.security.web.FilterChainProxy  : /api/account at position 2 of 12 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter' 
XNIO-2 task-1] w.c.HttpSessionSecurityContextRepository : No HttpSession currently exists 
XNIO-2 task-1] w.c.HttpSessionSecurityContextRepository : No SecurityContext was available from the HttpSession: null. A new one will be created. 
XNIO-2 task-1] o.s.security.web.FilterChainProxy  : /api/account at position 3 of 12 in additional filter chain; firing Filter: 'HeaderWriterFilter' 
XNIO-2 task-1] o.s.s.w.header.writers.HstsHeaderWriter : Not injecting HSTS header since it did not match the requestMatcher org.springframework.se[email protected]74d294b6 
XNIO-2 task-1] o.s.security.web.FilterChainProxy  : /api/account at position 4 of 12 in additional filter chain; firing Filter: 'LogoutFilter' 
XNIO-2 task-1] o.s.s.web.util.matcher.OrRequestMatcher : Trying to match using Ant [pattern='/api/logout', GET] 
XNIO-2 task-1] o.s.s.web.util.matcher.OrRequestMatcher : No matches found 
XNIO-2 task-1] o.s.security.web.FilterChainProxy  : /api/account at position 5 of 12 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter' 
XNIO-2 task-1] o.s.s.w.u.matcher.AntPathRequestMatcher : Request 'GET /api/account' doesn't match 'POST /api/authentication 
XNIO-2 task-1] o.s.security.web.FilterChainProxy  : /api/account at position 6 of 12 in additional filter chain; firing Filter: 'DefaultLoginPageGeneratingFilter' 
XNIO-2 task-1] o.s.security.web.FilterChainProxy  : /api/account at position 7 of 12 in additional filter chain; firing Filter: 'RequestCacheAwareFilter' 
XNIO-2 task-1] o.s.security.web.FilterChainProxy  : /api/account at position 8 of 12 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter' 
XNIO-2 task-1] o.s.security.web.FilterChainProxy  : /api/account at position 9 of 12 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter' 
XNIO-2 task-1] o.s.s.w.a.AnonymousAuthenticationFilter : Populated SecurityContextHolder with anonymous token: 'org.sprin[email protected]9055c2bc: Principal: anonymousUser; Credentials 
XNIO-2 task-1] o.s.security.web.FilterChainProxy  : /api/account at position 10 of 12 in additional filter chain; firing Filter: 'SessionManagementFilter' 
XNIO-2 task-1] o.s.security.web.FilterChainProxy  : /api/account at position 11 of 12 in additional filter chain; firing Filter: 'ExceptionTranslationFilter' 
XNIO-2 task-1] o.s.security.web.FilterChainProxy  : /api/account at position 12 of 12 in additional filter chain; firing Filter: 'FilterSecurityInterceptor' 

編集: 私は一緒にこれらの2つのプロジェクトをマージしようとしています。 https://github.com/jhipster/jhipster-sample-apphttps://github.com/spring-projects/spring-security-oauth/tree/master/samples/oauth2/sparklr

+0

メインフィルタチェーンがどのように構成されているかは表示されませんでした(この要求では、そのようになっているように見えます)。どこか完全なサンプルがありますか? –

+0

@Dave - 完全なサンプル - (https://github.com/ibagui/besquare-oauth/blob/master/src/main/java/io/github/jhipster/sample/config/OAuth2ServerConfig.java)をご覧ください。私はこれら2つのプロジェクトを一緒にマージしようとしています。 (https://github.com/jhipster/jhipster-sample-app)および(https://github.com/spring-projects/spring-security-oauth/tree/master/samples/oauth2/sparklr) – bagui

答えて

1

あなたのリソースサーバーのフィルターチェーンは、デフォルトでjhipsterが追加したカスタムフィルターチェーンよりも高い順序になっています。オーダーを変更するか、OAuthリソースがメインフィルターチェーンと一致しないようにパターンマッチャーを変更する必要があります。スプリングブートユーザガイドでは、カスタムフィルタチェーンを特定の順序(SecurityProperties.ACCESS_OVERRIDE_ORDER)で配置することを推奨しています。おそらく、そのアドバイスに従うことをお勧めします。

関連する問題