溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

spring boot security設置忽略地址不生效的解決方法

發布時間:2021-07-19 17:17:52 來源:億速云 閱讀:1481 作者:chen 欄目:開發技術

這篇文章主要講解了“spring boot security設置忽略地址不生效的解決方法”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“spring boot security設置忽略地址不生效的解決方法”吧!

spring boot security設置忽略地址不生效

最近在試下微服務改造,出現這樣一個問題所有請求都經過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

具體的權限控制規則配置。

2、忽略不生效問題
web.ignoring().antMatchers("/pr/v1/prNotifyBack");

如上代碼如果帶上/pr就不會生效,訪問依然會出現401錯誤。/pr是配置的項目路徑。但帶上項目路徑就不生效,這個問題很疑惑。

server:
port: 8089
servlet:
context-path: /pr

SpringBoot SpringSecurity, web.ignore失效

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class CustomSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/api/**").authenticated()
                .and()
                .addFilterBefore(new TokenFilter(), UsernamePasswordAuthenticationFilter.class);
    }
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers("/")
                .antMatchers("/swagger-ui.html")
                .antMatchers("/swagger-resources/**")
                .antMatchers("/webjars/springfox-swagger-ui/**")
                .antMatchers("/v2/api-docs/**");
    }
}

這是修改后正常工作的配置文件

之前使用@component注解, 然后使用@Resource注入進來.

導致過濾器全局生效.

正常配置,應該手動new, 而且過濾器類不能加@Component注解

具體原因,之后有空研究一下.

感謝各位的閱讀,以上就是“spring boot security設置忽略地址不生效的解決方法”的內容了,經過本文的學習后,相信大家對spring boot security設置忽略地址不生效的解決方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女