Rumah >Java >javaTutorial >Bagaimana untuk Mengintegrasikan Penapis CORS Keselamatan Spring?
Penyatuan Penapis CORS Spring Security
Spring Security memerlukan konfigurasi CORS untuk mengendalikan permintaan silang asal. Untuk menangani isu ini secara khusus, ikuti langkah berikut:
Laksanakan WebMvcConfigurerAdapter:
<code class="java">@Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH"); } }</code>
Konfigurasikan CORS dalam HttpSecurity:
<code class="java">@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.cors().configurationSource(corsConfigurationSource()); } @Bean public CorsConfigurationSource corsConfigurationSource() { final CorsConfiguration configuration = new CorsConfiguration(); configuration.setAllowedOrigins(ImmutableList.of("*")); configuration.setAllowedMethods(ImmutableList.of("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH")); configuration.setAllowCredentials(true); configuration.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type")); final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); return source; } }</code>
Nota Penting:
Atas ialah kandungan terperinci Bagaimana untuk Mengintegrasikan Penapis CORS Keselamatan Spring?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!