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

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

相关推荐
Imnobody4 分钟前
吴恩达 Prompt 工程课精讲③:三次迭代,解决 Prompt 的三大致命问题(JupyterLab 实战)
后端
青鱼入云15 分钟前
Java 11对集合类做了哪些增强?
java
千桐科技21 分钟前
qKnow 知识平台【开源版】安装与部署全指南
人工智能·后端
qq_124987075323 分钟前
基于Spring Boot的高校实习实践管理系统(源码+论文+部署+安装)
java·spring boot·后端·毕业设计
韩宁羽23 分钟前
SpringBoot开发双11商品服务系统[完结19章]
spring boot
作伴25 分钟前
结合Apollo手动实现一个动态线程池jar包
后端·架构
该用户已不存在25 分钟前
7个没听过但绝对好用的工具
前端·后端
Java水解26 分钟前
KingbaseES SQL性能调优方案分享
后端·sql
用户40993225021228 分钟前
PostgreSQL连接的那些弯弯绕:笛卡尔积、外连接和LATERAL你都理明白没?
后端·ai编程·trae
oak隔壁找我28 分钟前
SpringBoot + MyBatis 配置详解
java·数据库·后端