Spring Security 配置多個標籤與HttpSecurity對應關係
阿新 • • 發佈:2018-11-26
在把以前的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,我這裡大概類似於這樣
-
@Configuration
-
@Order(
1)
-
public
class
RestSecurityConfig extends WebSecurityConfigurerAdapter {
-
-
@Override
-
protected void configure(HttpSecurity http) throws Exception {
-
http
-
.antMatcher(
"/rest/**")
-
.addFilterAt(rsFilter(), BasicAuthenticationFilter.class)
-
.exceptionHandling()
-
.authenticationEntryPoint(digestEntryPoint())
-
.and()
-
.csrf().disable()
-
.authorizeRequests()
-
.antMatchers(
"/**")
-
.hasRole(
"RSCLIENT")
-
}
-
}
原XML配置
參見14.6 Advanced Namespace Configuration,我這裡類似
-
<http pattern="/rest/**" entry-point-ref="digestEntryPoint">
-
<intercept-url pattern='/**' access="hasRole('RSCLIENT')" />
-
<custom-filter ref="digestFilter" position="BASIC_AUTH_FILTER" />
-
<csrf disable="true" />
-
</http>