WebConfig.java
package cz.vsb.crm.config;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.lang.NonNull;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;
import java.io.IOException;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(@NonNull CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("http://localhost:5173")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*");
registry.addMapping("/v3/**")
.allowedOrigins("http://localhost:5173")
.allowedMethods("GET");
}
@Override
public void addResourceHandlers(@NonNull ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/")
.resourceChain(true)
.addResolver(new PathResourceResolver() {
@Override
protected Resource getResource(@NonNull String resourcePath, @NonNull Resource location)
throws IOException {
Resource requested = location.createRelative(resourcePath);
if (requested.exists() && requested.isReadable()) {
return requested;
}
if (resourcePath.startsWith("api")
|| resourcePath.startsWith("v3")
|| resourcePath.startsWith("swagger-ui")
|| resourcePath.startsWith("h2-console")
|| resourcePath.startsWith("webjars")
|| resourcePath.contains(".")) {
return null;
}
Resource index = new ClassPathResource("/static/index.html");
return index.exists() ? index : null;
}
});
}
}