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 {
    }
}

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

相关推荐
leon877 小时前
MyBatis-Plus与AOP实现动态数据权限过滤的实践与踩坑记录
java
电气研究所7 小时前
Java + Redis 实现工业设备告警去抖,避免变频器故障重复推送
java·前端
無限進步D7 小时前
Java 代码块
java·开发语言
用户3126874877208 小时前
@Async 加了没生效?Spring Boot 异步编程全链路拆解:从线程池到自调用陷阱
java·spring boot
老孙讲技术8 小时前
【4G IPC 上云】临时点位怎么免布线上云?listDeviceDetailsByPage + 辅码流预览|智慧工地实战
后端·物联网
zfoo-framework8 小时前
安装pi agent 支持duojie和deepseek
java·服务器·前端
铁皮饭盒8 小时前
DeepSeek V4 Pro 0813发布了, 也可以部署到 Codex 了
前端·javascript·后端
君顾19 小时前
代驾跑腿业务系统实战开发指南
java·开发语言·代驾跑腿
苏三说技术9 小时前
推荐一个牛逼的企业智能招聘系统
后端
达达尼昂9 小时前
Flutter AI Harness 如何让 Agent 参与软件开发全流程
android·人工智能·后端