簡體   English   中英

Spring Security OAuth2受保護的資源實際上未被保護…過濾器不起作用?

[英]Spring Security OAuth2 Protected Resource not actually protected… Filters Not Working?

從字面上看,它可以訪問端點: http:// localhost:8080 / oauth2-password / helloworld並仍然獲得字符串“ Hello World!”。.在下面查看我的配置,請告訴我原因。 這非常令人沮喪。

授權服務器

@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);
    }

}

網絡安全配置

@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();
    }

}

哇,驚訝的是沒有人能抓住這個。 記錄極少,但經過幾天的搜索后找到了答案。

對於通過這種方式發現他們正在正確配置ResourceServer,AuthorizationServer和WebSecurityConfigurerAdapter的任何人,但您仍然可以完美地擊中端點,就好像嚇壞了的過濾器甚至還沒有運行一樣, 這就是答案:

在實現AbstractSecurityWebApplicationInitializer的類路徑中添加帶@Configuration注釋的類。 調用類SecurityWebAppInitializer或任何您想引起轟動的類。 確保覆蓋所有方法,並將其保留為默認實現。 確保將此類注冊到Spring上下文中(以及其他配置類)。

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

繁榮。 就是這樣。 命中一個端點,並且未經401授權。

此Abstract類的作用是在其他已注冊過濾器之前注冊DelegatingFilterProxy以使用springSecurityFilterChain。 啊。 注冊springSecurityFilterChain時,用XML可以很容易地完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM