Spring MVC配置类 实现跨域请求

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 对请求的路径进行修改,以便正确地匹配目标服务器的路由。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值