search
LoginSignup
0

posted at

updated at

【SpringBoot】SpringSecurityでCORSの設定

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;
	

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
What you can do with signing up
0