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>

猜你喜欢

转载自blog.csdn.net/qq_22771739/article/details/84308908