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)

相关推荐
陈平安Java and C4 小时前
MyBatisPlus
java
秋野酱5 小时前
如何在 Spring Boot 中实现自定义属性
java·数据库·spring boot
安的列斯凯奇5 小时前
SpringBoot篇 单元测试 理论篇
spring boot·后端·单元测试
Bunny02125 小时前
SpringMVC笔记
java·redis·笔记
feng_blog66886 小时前
【docker-1】快速入门docker
java·docker·eureka
枫叶落雨2227 小时前
04JavaWeb——Maven-SpringBootWeb入门
java·maven
m0_748232397 小时前
SpringMVC新版本踩坑[已解决]
java
多则惑少则明7 小时前
SSM开发(一)JAVA,javaEE,spring,springmvc,springboot,SSM,SSH等几个概念区别
spring boot·spring·ssh
码农小灰7 小时前
Spring MVC中HandlerInterceptor和Filter的区别
java·spring·mvc
Swift社区7 小时前
【分布式日志篇】从工具选型到实战部署:全面解析日志采集与管理路径
人工智能·spring boot·分布式