SpringBoot跨域问题

一、适用场景:

HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议,要比http协议安全。

当我们对外提供服务是需要通过域名访问我们的服务,获取数据,在内网中我们可以通过http访问,前端访问后端也可以通过http,这时候的协议是一致的不会存在跨域问题。

当前端提供的是https 后端提供的确实http,就会出现跨域问题。
我们一般对外的服务都是通过https的,所以需要后端解决跨域访问问题。前端提供出去的域名一定是HTTPS协议。

二、出现问题:

访问接口:
strict-origin-when-cross-origin

访问服务:
When allowCredentials is true, allowedOrigins cannot contain the special value "*"since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.

意思是:当allowCredentials为true时,allowingOrigins不能包含特殊值" *",因为无法在" Access-Control-Allow-Origin"响应标头上设置。要允许凭据具有一组来源,请明确列出它们或考虑改用" allowedOriginPatterns"。

三、解决-两个类建议和启动类同级目录

java 复制代码
@SpringBootConfiguration
public class MyWebConfigurer implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry corsRegistry) {
        /**
         * 所有请求都允许跨域,使用这种配置就不需要
         * 在interceptor中配置header了
         */
        corsRegistry.addMapping("/**")
                .allowCredentials(true)
                .allowedOriginPatterns("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .allowedHeaders("*")
                .maxAge(3600);
    }
}
java 复制代码
public class ProcessInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
        httpServletResponse.setHeader("X-Powered-By", "Jetty");

        String method = httpServletRequest.getMethod();
        if (method.equals("OPTIONS")) {
            httpServletResponse.setStatus(200);
            return false;
        }
        System.out.println(method);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}

每天努力一点,每天都在进步

相关推荐
XIAEE1 分钟前
初次使用 Node + Koa + MongoDB 搭建服务器:从零到第一个 API
java·spring
zed_234 分钟前
Agent 是什么:从「问答」到「会用工具」
后端
m0_587383007 分钟前
无人自助拼豆系统核心功能开发与部署落地全流程实战指南
java·经验分享·后端·struts·架构
phil615 分钟前
打进沙箱的电话都走哪儿?看懂Sandbox的网络设计
后端
雷一言说16 分钟前
Spring AI 2.0:RAG 文档分块大小策略选择
java·spring
XIAEE16 分钟前
Java 世界历险记:从一行 System.out.println 到整个 Spring 宇宙
java·spring
XIAEE18 分钟前
微前端狂想曲:我试图让三个不同门派的前端住进同一栋楼
java·spring
iOS开发上架哦19 分钟前
抓包怎么修改响应?响应头、响应体、脚本注入实操教程
后端·ios
XIAEE19 分钟前
Linux 生存指南:从"终端恐惧症"到"命令行老司机"
java·spring
XIAEE19 分钟前
实战教程:手机端死活调不出摄像头?聊聊安全上下文(Secure Context)
java·spring