[英]Exclude requests to URL from being run through Spring Security filters
我有一個 Spring Boot 應用程序和一個自定義身份驗證過濾器。 該應用程序有一個 URL“/”,我希望避免在其中運行任何 Spring Security 過濾器(身份驗證和授權),包括我的自定義過濾器。
我不想將WebSecurity
配置為忽略此 URL,因為我想應用其他一些 Spring Security 功能,我的理解是使用:
webSecurityBuilder.ignoring().antMatchers("/");
將阻止所有Spring Security 功能在此 URL 上運行。
有沒有辦法為此使用 HttpSecurity?
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthenticationFilter customAuthFilter;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/").
anonymous().and().authorizeRequests()
.antMatchers("/", "/index.html").permitAll()
.anyRequest().authenticated();
http.addFilterBefore(customAuthFilter, UsernamePasswordAuthenticationFilter.class);
}
到目前為止,這就是我所擁有的,但出於某種原因,當我導航到“/”時,我仍然點擊了我的自定義過濾器。
下面是解決方法。 摘要是您應該定義兩個具有不同順序的 WebSecurityConfigurerAdapter。 請試一試。 解決方案
下面的代碼是從那里復制的。
@Configuration
@Order(1)
public class OnlyHeadersConfig extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) {
http
.antMatchers("/special/endpoints/**")
.authorizeRequests((authz) -> authz.anyRequest().permitAll())
.anonymous();
}
}
@Configuration
@Order(2)
public class MainSecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) {
http
.authorizeRequests(authz -> authz.anyRequest().authenticated())
.addFilterAt(new MyCustomFilter(), UsernamePasswordAuthenticationFilter.class);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.