Jaeyoon Lee
Jaeyoon Lee

Reputation: 33

How to fix Https CORS Error with Spring boot

I deployed server on AWS with https. React developer told me that there is CORS issue. but I can't fix it..

before set https, they can accept spring server but now they can't accept.

package server.yogoyogu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("https://link~~.com"); // e.g. /**, http://domain1.com
        config.addAllowedHeader(CorsConfiguration.ALL);
        config.addAllowedMethod(HttpMethod.GET);
        config.addAllowedMethod(HttpMethod.POST);
        config.addAllowedMethod(HttpMethod.HEAD);
        config.addAllowedMethod(HttpMethod.PUT);
        config.addAllowedMethod(HttpMethod.DELETE);
        config.addAllowedMethod(HttpMethod.TRACE);
        config.addAllowedMethod(HttpMethod.OPTIONS);
        config.setAllowCredentials(true);
        config.setMaxAge(3600L);

        source.registerCorsConfiguration("https://link~~.com", config); // "/**"
        return new CorsFilter(source);
    }

}

Upvotes: 1

Views: 993

Answers (2)

Shyam Patel
Shyam Patel

Reputation: 419

you can refer to this documentation.

also, you can add cross origin on top of your controller

@CrossOrigin(origins = "*", allowedHeaders = "*")
@Controller
public class HomeController 
{
  @GetMapping(path="/")
  public String homeInit(Model model) {
    return "home";
  }
}

Upvotes: 1

Jaeyoon Lee
Jaeyoon Lee

Reputation: 33

I solved this problem like this because of security policy

@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("^https?:\\/\\/link~~~.com$"); // e.g. /**, http://domain1.com
        config.addAllowedHeader(CorsConfiguration.ALL);
        config.addAllowedMethod(HttpMethod.GET);
        config.addAllowedMethod(HttpMethod.POST);
        config.addAllowedMethod(HttpMethod.HEAD);
        config.addAllowedMethod(HttpMethod.PUT);
        config.addAllowedMethod(HttpMethod.DELETE);
        config.addAllowedMethod(HttpMethod.TRACE);
        config.addAllowedMethod(HttpMethod.OPTIONS);
        config.setAllowCredentials(true);
        config.setMaxAge(3600L);

        source.registerCorsConfiguration("^https?:\\/\\/link~~~.com$", config); // "/**"
        return new CorsFilter(source);
    }

}

Upvotes: 2

Related Questions