2016-07-22 1396 views
3

有一個在春季安全的OAuth實現與以下行固定的OAuth端點的普遍做法:在spring安全中使用requestMatchers()。antMatchers()時沒有動詞的原因是什麼?

.requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")

整個設置是這樣的:

http 
    .formLogin().loginPage("/login").permitAll() 
    .and() 
    .requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access") 
    .and() 
    .authorizeRequests().anyRequest().authenticated(); 

能有人給我解釋一下爲什麼需要特定的行,因爲下一行明確指出所有請求都必須經過身份驗證?

回答

0

Spring Security管理着幾個Servlet過濾器的鏈。

在現代彈簧安全(爲V3.2.x及以上)每條鏈由WebSecurityConfigurerAdapter配置並應用基於@Order(...)類註解直到首次報道,它支持HttpServletRequest,其經由.requestMatchers配置()`DSL:

作爲版本3.1,{@code的FilterChainProxy}使用 {@link SecurityFilterChain}的情況下,其中的每一個包含一個{@link RequestMatcher}的列表構造 以及應施加到匹配的過濾器列表要求。大多數應用程序 將只包含一個過濾器鏈,如果您使用的是命名空間,則不需要顯式地設置鏈。如果您需要更細緻的控制,則可以使用命名空間元素 。這定義了一個URI模式 以及應該應用於與模式匹配的 請求的過濾器列表(以逗號分隔的bean名稱)。的示例配置可能是這樣的:施加

public class FilterChainProxy extends GenericFilterBean { 
... 
/** 
* Returns the first filter chain matching the supplied URL. 
* 
* @param request the request to match 
* @return an ordered array of Filters defining the filter chain 
*/ 
private List<Filter> getFilters(HttpServletRequest request) { 
    for (SecurityFilterChain chain : filterChains) { 
     if (chain.matches(request)) { 
      return chain.getFilters(); 
     } 
    } 

    return null; 
} 

然後選定鏈來保護請求。

您的設置僅適用於3個URL,如果沒有配置其他URL,則所有其他URL都不安全。

相關問題