首頁  >  文章  >  Spring Security 6:cors() 已棄用並標記為刪除

Spring Security 6:cors() 已棄用並標記為刪除

WBOY
WBOY轉載
2024-02-10 23:45:081211瀏覽

php小編魚仔告訴大家一個重要的消息:Spring Security 6版本中,cors()方法已經被棄用並標記為刪除。 cors()方法是用於處理跨域資源共享的配置,然而在新版本中,Spring Security團隊決定刪除該方法,並引入了更強大的跨域解決方案。這個變化對於使用Spring Security的開發者來說是一個重要的改動,需要隨時了解並升級程式碼以適應新版本的變化。

問題內容

我有下面的程式碼:

public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    return http
            .httpBasic().disable()
            .cors().and().csrf().disable()
            .authorizeHttpRequests()
            .requestMatchers("/register")
            .permitAll()
            .and()
            .authorizeHttpRequests()
            .requestMatchers("/users")
            .hasAnyAuthority("USER", "ADMIN")
            .and().formLogin().and().build();
}

請幫助我使此功能正常工作

解決方法

根據遷移指南和另外配置到最新版本, securityfilterchain應該有下一個屍體。

@Bean
  public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    return http.csrf(AbstractHttpConfigurer::disable)
        .cors(AbstractHttpConfigurer::disable)
        .authorizeHttpRequests(request -> {
          request.requestMatchers("/register").permitAll();
          request.requestMatchers("/users")
              .hasAnyAuthority("USER", "ADMIN");
        }).formLogin(Customizer.withDefaults()).build();

  }

也請閱讀/檢查上述文件參考。 順便說一下,這裡關於堆疊溢出的文章有很多關於遷移到最新版本框架的文章。

以上是Spring Security 6:cors() 已棄用並標記為刪除的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除