1. 程式人生 > >Spring Security 配置多個標籤與HttpSecurity對應關係

Spring Security 配置多個標籤與HttpSecurity對應關係

在把以前的xml配置改到java配置,找了半天沒找到…於是試出來以後才在官方文件搜尋到

引用一句話:

        http擁有一個匹配URL的pattern(對應.antMatcher()),未指定時表示匹配所有的請求,其下的子元素intercept-url也有一個匹配URL的pattern(對應.antMatchers()),該pattern是在http元素對應pattern基礎上的,也就是說一個請求必須先滿足http對應的pattern才有可能滿足其下intercept-url對應的pattern

java配置

參見spring官方文件5.9 Multiple HttpSecurity,我這裡大概類似於這樣


  
  1. @Configuration
  2. @Order( 1)
  3. public class
    RestSecurityConfig extends WebSecurityConfigurerAdapter
    {
  4. @Override
  5. protected void configure(HttpSecurity http) throws Exception {
  6. http
  7. .antMatcher( "/rest/**")
  8. .addFilterAt(rsFilter(), BasicAuthenticationFilter.class)
  9. .exceptionHandling()
  10. .authenticationEntryPoint(digestEntryPoint())
  11. .and()
  12. .csrf().disable()
  13. .authorizeRequests()
  14. .antMatchers( "/**")
  15. .hasRole( "RSCLIENT")
  16. }
  17. }

原XML配置

參見14.6 Advanced Namespace Configuration,我這裡類似


  
  1. <http pattern="/rest/**" entry-point-ref="digestEntryPoint">
  2. <intercept-url pattern='/**' access="hasRole('RSCLIENT')" />
  3. <custom-filter ref="digestFilter" position="BASIC_AUTH_FILTER" />
  4. <csrf disable="true" />
  5. </http>