配置如下:
package com.dtranx.tools.corpora.business.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @Description
* @Author zl
* @Date 2024/5/21 10:07
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
// 跨域配置
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.maxAge(18000L)
.allowCredentials(true);
}
// 路径前缀配置
private String pathPrefix = "/corpora";
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.addPathPrefix(pathPrefix, c -> c.isAnnotationPresent(RestController.class));
}
}