SpringSecurity常見問題解決:設定忽略地址不生效的問題
阿新 • • 發佈:2021-06-13
一、設定忽略地址不生效的問題
最近在試下微服務改造,出現這樣一個問題所有請求都經過spring cloud gateway進行認證授權後再訪問後端資料方服務,但有些需要合作機構回撥,由於進行了security認證,最終的方案是對回撥地址進行忽略auth認證。
最終security主要程式碼如下:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v1/prNotifyBack");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
/**表示所有的訪問都必須進行認證處理後才可以正常進行*/
http.httpBasic().and().authorizeRequests().anyRequest().fullyAuthenticated();
/**所有的Rest服務一定要設定為無狀態,以提升操作效能 */
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.csrf().disable();
}
}
這個過程遇到了幾個問題:
1、繼承WebSecurityConfigurerAdapter 後我們重寫configure方法,這個方法需要注意:他有兩個不同的引數。
HttpSecurity 及WebSecurity 作用是不一樣的:WebSecurity 主要針對的全域性的忽略規則,HttpSecurity主要是許可權控制規則。
所以一開始用HttpSecurity是達不到忽略地址的目的。
protected void configure(HttpSecurity http){.......}
public void configure(WebSecurity web) {.........}
WebSecurity:全域性請求忽略規則配置(比如說靜態檔案,比如說註冊頁面)、全域性HttpFirewall配置、是否debug配置、全域性SecurityFilterChain配置、privilegeEvaluator、expressionHandler、securityInterceptor、......
HttpSecurity:具體的許可權控制規則配置。
原文連結:https://blog.csdn.net/wangchengaihuiming/article/details/100129838