package cn.tedu.csmall.product.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* Spring MVC配置类
* 可以实现跨域请求
*
*/
@Slf4j
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
public WebMvcConfiguration() {
log.debug("创建配置类对象:WebMvcConfiguration");
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedHeaders("*")
.allowedMethods("*")
.allowedOriginPatterns("*")
.allowCredentials(true)
.maxAge(3600);
}
}
前端跨域访问配置--反向代理
target: 表示目标服务器的地址(URL)。在前端代码中,当通过反向代理发送请求时,请求的 target 应该是代理服务器的地址。
changeOrigin: 当 changeOrigin 设置为 true 时,代理服务器会将请求的 Origin 头设置为代理服务器的地址。在前端代码中,如果需要跨域访问目标服务器,并且目标服务器要求请求的 Origin 头与实际请求的来源地址匹配,可以将 changeOrigin 设置为 true,以确保请求的 Origin 头正确设置。
pathRewrite: 是一个配置选项,用于修改请求的路径。当代理服务器转发请求时,可以使用 pathRewrite 对请求的路径进行修改,以便正确地匹配目标服务器的路由。