2017-10-09 59 views
0

我開發一個彈簧引導應用程序和問題,我有如下:春季安全的Oauth 2重定向行爲對/的OAuth /令牌

默認情況下,我的控制器每個請求重定向到/登錄,所以我不能用這種方式。

我可以通過在application.properties設置

security.oauth2.resource.filter-order = 3 

禁用/登錄。

但我想知道如何禁用它只適用於非瀏覽器,所以我仍然可以使用登錄。

編輯:

這將是我的做法:從什麼/ API運行

http.antMatcher("/api/**") 
       .authorizeRequests().anyRequest().authenticated(); 

     http.antMatcher("/**") 
       .formLogin(); 

一切/是休息和只能由端Oauth處理。 其餘應通過登錄處理。

但它仍然沒有工作,我仍然得到重定向。

沒關係,修復它。感謝userWithLongNumber!

就我而言,事實證明我的WebSecurity配置錯誤。 這現在工作:

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http. 
      formLogin().permitAll() 
      .and() 
      .authorizeRequests().anyRequest().authenticated(); 
} 

我RessourceServerConfig看起來是這樣的:

@Override 
public void configure(HttpSecurity http) throws Exception { 

     http 
       .exceptionHandling() 
       .authenticationEntryPoint(customAuthenticationEntryPoint) 
       .and() 
       .logout() 
       .logoutUrl("/oauth/logout") 
       .logoutSuccessHandler(customLogoutSuccessHandler) 
       .and() 
       .headers() 
       .frameOptions().disable() 
       .and() 
       .sessionManagement() 
       .sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
       .and() 
       .authorizeRequests() 
       .antMatchers("/api/**").authenticated(); 

    } 

} 

回答

1

您需要降低主要應用安全過濾器的優先級。

正如Spring Boot教程中所解釋的。

的@EnableResourceServer註釋創建了@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER-1)默認情況下,保安過濾器,所以通過移動主要應用安全@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER),我們可以確保對OAuth的規則資源優先。

設置

security.oauth2.resource.filter階= 3

不會禁用主安全過濾器。它只是將其優先級改爲較低的值。 (負值具有最高優先級,-3具有高於3的優先級)

據我所知,您的問題是在ResourceServerConfiguration中沒有正確設置匹配器。使其更具體如下。

http.antMatcher("/helloOAuth") 
      .authorizeRequests().anyRequest().authenticated(); 

https://github.com/lanka-guide/simple-oauth2-server有一個示例實現。可能有幫助。