0

字面上能夠擊中端點:http://localhost:8080/oauth2-password/helloworld仍然獲得字符串「Hello World!」..請查看我的配置下面,請告訴我爲什麼。這是非常令人沮喪的。Spring Security 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 SECURITY 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正確但你仍然擊中端點完美罰款如果再用過濾器甚至沒有活着,這裏就是答案:

在類路徑中添加一個@Configuration註釋類,該類實現AbstractSecurityWebApplicationInitializer。調用SecurityWebAppInitializer類或任何你想做的事情。確保覆蓋所有的方法,並將它們作爲默認實現。確保你將這個類註冊到你的Spring上下文中(以及其他配置類)。

重新編譯,重新啓動服務器等...

繁榮。就像那樣工作。打一個端點,是擅自用401

什麼這個抽象類確實是註冊的DelegatingFilterProxy任何其他註冊過濾器之前使用springSecurityFilterChain。啊。當您註冊springSecurityFilterChain時,可以使用XML輕鬆完成某些操作。

+0

在Spring Security Reference中已經證明了這一點,請參閱我的[回覆](https://stackoverflow.com/a/39591417/5277820)以獲取類似的問題。 – dur

+0

@dur哈哈很驚訝,你沒有趕上那一個!我知道你必須在XML中註冊DelegatingFilterProxy。簡單。我認爲整個過程當你實現EnableResourceServer時它會被自動註冊。但是,只有當您這樣做時啓用的過濾器是OAuth2AuthenticationProcessingFilter,而不是整個父級DelegatingFilterProxy - > FilterChainProxy委託方案。 –