I’m having an issue with configuring CORS in my Spring Boot application. I’ve set a context-path for all exposed endpoints like this:
<code>server:
port: 8081
servlet:
context-path: '/api'
</code>
<code>server:
port: 8081
servlet:
context-path: '/api'
</code>
server:
port: 8081
servlet:
context-path: '/api'
Here is my CORS configuration:
<code>import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
@ConfigurationProperties(prefix = "web.cors")
@Getter
@Setter
public class CorsConfiguration implements WebMvcConfigurer {
private String allowedOrigins;
private String allowedMethods;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/api/**")
.allowedOrigins(allowedOrigins.split(","))
.allowedMethods("*")
.allowedHeaders("*");
}
}
</code>
<code>import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
@ConfigurationProperties(prefix = "web.cors")
@Getter
@Setter
public class CorsConfiguration implements WebMvcConfigurer {
private String allowedOrigins;
private String allowedMethods;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/api/**")
.allowedOrigins(allowedOrigins.split(","))
.allowedMethods("*")
.allowedHeaders("*");
}
}
</code>
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
@ConfigurationProperties(prefix = "web.cors")
@Getter
@Setter
public class CorsConfiguration implements WebMvcConfigurer {
private String allowedOrigins;
private String allowedMethods;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/api/**")
.allowedOrigins(allowedOrigins.split(","))
.allowedMethods("*")
.allowedHeaders("*");
}
}
Here is my application.yaml:
<code>
web:
cors:
allowed-origins: http://localhost:8080, http://localhost
allowed-methods: GET, POST, PATCH, PUT, DELETE, OPTIONS, HEAD
</code>
<code>
web:
cors:
allowed-origins: http://localhost:8080, http://localhost
allowed-methods: GET, POST, PATCH, PUT, DELETE, OPTIONS, HEAD
</code>
web:
cors:
allowed-origins: http://localhost:8080, http://localhost
allowed-methods: GET, POST, PATCH, PUT, DELETE, OPTIONS, HEAD
However, when my frontend (running on http://localhost:8080
) tries to call GET /api/markets
, I get a CORS-related error.
Interestingly, if I change .addMapping("/api/**")
to .addMapping("/**")
in my CORS configuration, it works fine.
Is there anything I am missing in my CORS configuration that affects the context-path?
Any help would be greatly appreciated. Thank you!