0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【SpringBoot】SpringSecurityでCORSの設定

Last updated at Posted at 2022-05-24

SpirngSecurityでCORSの許可方法は以下の処理になります。

WebSecurityConfig
	/**
	 * HTTP Security設定
	 */
	@Override
	protected void configure(HttpSecurity http) throws Exception {
        //そのほか記述については省略してます

		http.cors();
	}


	/**
	 * CORS設定
	 */
	@Bean
	CorsConfigurationSource corsConfigurationSource() {
		
		CorsConfiguration configuration = new CorsConfiguration();
		
		//CORSで許可する値を設定
		//許可するURIを設定 レスポンス側のURI
        configuration.setAllowedOrigins(List.of("http://localhost:3000"));
        
        //許可するメソッドを設定 GET,POST,PUT,DELETE
		configuration.setAllowedMethods(Arrays.asList("GET", "POST"));

		//許可するヘッダーを設定 Content-Type等、、、
		configuration.setAllowedHeaders(Arrays.asList("*"));
		
		//cookieの取得を許可
		configuration.setAllowCredentials(true);

		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		source.registerCorsConfiguration("/**", configuration);
		return source;
	
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?