SpringBoot跨域问题解决

前端访问后台接口时,浏览器报错,跨域无法访问。

报错信息如下:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

经过一番百度之后,需要在控制器上添加处理跨域的注解,

注解添加后,再次访问接口,又有了新的报错:

Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

根据资料显示,这个是因为浏览器预检请求失败,复杂请求会做预检请求,比如header中有自动以的属性等情况,当前的报错是指OPTION 请求失败,需要后端放行 OPTION 请求。

解决方法是定义个拦截器:

CorsInterceptor.java

java 复制代码
package com.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class CorsInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS");
        response.setHeader("Access-Control-Max-Age", "86400");
        response.setHeader("Access-Control-Allow-Headers", "*");

        // 如果是OPTIONS则结束请求
        if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
            response.setStatus(HttpStatus.NO_CONTENT.value());
            return false;
        }

        return true;
    }
}

然后将拦截器注册到全局拦截器中,使其生效。

WebConfig.java

java 复制代码
package com.config;

import com.interceptor.CorsInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;



@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private CorsInterceptor corsInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //跨域拦截器需放在最上面
        registry.addInterceptor(corsInterceptor);

    }
}

再次访问接口,请求正常返回了

参考链接:

「日常报错」Response to preflight request doesn't pass access control check: It does not have HTTP ok status. - Posase - 博客园 (cnblogs.com)

为什么出现OPTIONS?SpringBoot接口跨域解决方案 - 简书 (jianshu.com)

相关推荐
皮皮林5514 小时前
Java性能调优黑科技!1行代码实现毫秒级耗时追踪,效率飙升300%!
java
冰_河4 小时前
QPS从300到3100:我靠一行代码让接口性能暴涨10倍,系统性能原地起飞!!
java·后端·性能优化
桦说编程7 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅9 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者10 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺10 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart11 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP12 小时前
MyBatis-mybatis入门与增删改查
java
孟陬16 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端