springboot跨域,错误:CORS 头缺少 ‘Access-Control-Allow-Origin‘

错误:已拦截跨源请求:同源策略禁止读取位于 http://localhost:9000/test 的远程资源。(原因:CORS 头缺少 ‘Access-Control-Allow-Origin’)。

在这里插入图片描述

解决方式:因为没有配置跨域所以出现这个错误

具体解决办法:注释掉的也是跨域方法,但是我用的不能解决我的问题,第二种是可以解决我的问题的。
在这里插入图片描述

package com.example.springplay.util;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
//跨域配置类:配置跨域请求
@Configuration //声明全局配置类
public class WebConfig extends WebMvcConfigurerAdapter {
//第一种:
    //  @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        /*
//        * 1:访问路径
//        * 2:请求来源
//        * 3:请求方法
//        * 4:允许携带tocken
//        * 5:最大响应时间
//        *
//        * */
//
//        registry.addMapping("/**")
//                .allowedOrigins("Http:localhost:8080","null")
//                .allowedHeaders("GET","POST","PUT","OPTIONS","DELETE")
//                .allowCredentials(true)
//                .maxAge(3600);
//
//    }
//第二种:
    @Override
    public  void  addCorsMappings(CorsRegistry registry){
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8080")
                .allowedHeaders("*")
                .allowedMethods("*")
                .maxAge(30*1000);
    }

}


在这里插入图片描述

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值