0

文字通りエンドポイントにヒットすることができます:http://localhost:8080/oauth2-password/helloworldまだ文字列 "Hello World!"があります。下記の設定をチェックして、理由を教えてください。これは非常にイライラしています。SpringセキュリティOAuth2保護されたリソースは実際には保護されていません...フィルターが機能していませんか?

認証サーバ

@Configuration 
@EnableAuthorizationServer 
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { 

    @Autowired 
    AuthenticationManager authenticationManager; 

    @Primary 
    @Bean 
    InMemoryTokenStore tokenStore() { 
     return new InMemoryTokenStore(); 
    } 

    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
     endpoints.authenticationManager(this.authenticationManager).tokenStore(this.tokenStore()); 
    } 


    @Override 
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { 
     security.checkTokenAccess("isAuthenticated()"); 
    } 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.inMemory() 
     .withClient("client") 
     .resourceIds("app") 
     .authorizedGrantTypes("password") 
     .scopes("read", "write", "trust") 
     .refreshTokenValiditySeconds(20000) 
     .accessTokenValiditySeconds(600); 
    } 

} 

リソースサーバ

@Configuration 
@EnableResourceServer 
public class ResourceServerConfig extends ResourceServerConfigurerAdapter { 

    @Autowired 
    AuthenticationManager authManager; 

    @Autowired 
    TokenStore tokenStore; 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/helloworld/**").authenticated(); 
    } 

    @Override 
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 
     resources.resourceId("app").tokenStore(this.tokenStore).authenticationManager(this.authManager); 
    } 

} 

WebセキュリティCONFIG

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.inMemoryAuthentication().withUser("user").password("password").roles("USER"); 
    } 

    @Bean 
    @Override 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
     return super.authenticationManagerBean(); 
    } 

} 

答えて

0

うわー、誰もこれをキャッチすることができませんでした。非常によく文書化されていませんが、検索の日後に答えが見つかりました。このように来て、彼らはResourceServer、AuthorizationServerを設定している、とひどく神経質フィルタも生きていなかったかのようにWebSecurityConfigurerAdapterが正しくまだあなたはまだ完全に罰金エンドポイントをヒットしている見つけた人のため

ここでは、答えは次のとおりです。

AbstractSecurityWebApplicationInitializerを実装するクラスパスに@Configuration注釈付きクラスを追加します。 SecurityWebAppInitializerクラスを呼び出すか、そのようにしたいと思っているものを呼び出します。すべてのメソッドをオーバーライドして、それらをデフォルト実装のままにしてください。このクラスを(他のconfigクラスと一緒に)Springコンテキストに登録してください。

、再コンパイル、再起動し、サーバ等...

ブーム。作品は、まさにそのようなものです。何この抽象クラスはありませんが、他の登録済みのフィルターの前にspringSecurityFilterChainを使用するようにDelegatingFilterProxyを登録しているエンドポイントをヒットし、401

で不正ました。 UGH。 springSecurityFilterChainを登録すると、XMLで簡単に何かが行われます。

+0

似たような質問については、私の[回答](https://stackoverflow.com/a/39591417/5277820)を参照してください。 – dur

+0

@durハハはあなたがもう一度それを捕まえなかったことに驚いた! DelegatingFilterProxyをXMLに登録しなければならないことは分かっていました。簡単です。私はこれがEnableResourceServerを実装するときに自動的に登録されていると思っていました。ただし、そのときに有効になる唯一のフィルタは、OAuth2AuthenticationProcessingFilterであり、全体的な親DelegatingFilterProxy - > FilterChainProxy委任スキームではありません。 –

関連する問題