首页 > 其他分享 >springboot高版本(2.5以上)解决跨域问题

springboot高版本(2.5以上)解决跨域问题

时间:2023-11-08 13:33:52浏览次数:36  
标签:跨域 Work list springframework org import public 2.5 springboot

版本说明

springboot 2.7.17
  • 原来代码
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class AppConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigin("*")
                .allowCredentials(true);
    }
}
  • controller层代码

@RestController
public class SendController {
    @RequestMapping("/send")
    public ArrayList<Work> send() {
        ArrayList<Work> list = new ArrayList<>();
        Work work1 = new Work(1, "骆驼祥子", "老舍", "文学", "无");
        Work work2 = new Work(2, "红楼梦", "曹雪芹", "文学", "无");
        Work work3 = new Work(3, "活着", "余华", "文学", "无");
        Work work4 = new Work(4, "西游记", "施耐庵", "文学", "无");
        list.add(work1);
        list.add(work2);
        list.add(work3);
        list.add(work4);
        return list;
    }
}
  • 前端代码(部分)
<script setup>
import { ref} from "vue";
import axios, { Axios } from "axios";
const list = ref([]);
const title = "hello world";
const send = function() {
    console.log('hello');
    axios.get('http://localhost:8080/send')
    .then((res=>{
        console.log(res.data);
        list.value = res.data;
        console.log(list.value);
    }))
    .catch((err=>{
        console.log(err);
    }))
}
</script>

<template>
  <button @click="send">点击发送axios请求</button>
  <table border="1" v-for="(item) in list" :key="item.id">
    <tr>
      <th style="color: brown;">序号</th>
      <th>文章</th>
      <th>作者</th>
      <th>类型</th>
      <th>备注</th>
    </tr>
    <tr>
      <td>{{ item.id }}</td>
      <td>{{ item.title }}</td>
      <td>{{ item.author }}</td>
      <td>{{ item.type }}</td>
      <td>{{ item.other }}</td>
    </tr>
  </table>
</template>
  • 现象

    • 前端
    • QQ截图20231108125958.png
    • 前端点击click发生跨域问题
    • QQ截图20231108130040.png
    • 后端抛出异常
    • QQ截图20231108130154.png
  • 查看异常

java.lang.IllegalArgumentException: 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.
  • 解析异常
When allowCredentials is true,allowedOrigins cannot contain the special value "*"
To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
  • 解决
    • 使用allowedOriginPatterns代替allowedOrigins
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class AppConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*") // 这里
                .allowCredentials(true);
    }
}

  • 重启服务,再次点击button,得到数据 QQ截图20231108130718.png

标签:跨域,Work,list,springframework,org,import,public,2.5,springboot
From: https://blog.51cto.com/u_15093262/8249708

相关文章

  • 运行Springboot测试类查询数据库数据显示白网页
    问题运行Springboot测试类时,查询数据库里面数据显示如下白网页程序报如下错误 解决方案 SpringBoot应用未能启动的原因是它没有找到合适的数据库配置具体来说,它需要一个数据源(DataSource),但未能在你的配置中找出,也没有找到任何嵌入式数据库(H2,HSQL或Derby)以下是几个......
  • SpringBoot集成文件 - 大文件的上传(异步,分片,断点续传和秒传)
    1.知识准备大文件的上传技术手段和普通文件上传是有差异的,主要通过基于分片的断点续传和秒传和异步上传解决。#1.1大文件面临的问题上传速度慢--应对: 分块上传上传文件到一半中断后,继续上传却只能重头开始上传--应对: 断点续传相同文件未修改再次上传,却只能重......
  • 基于springboot+vue开发的教师工作量管理系
    教师工作量管理系springboot31摘要随着信息技术在管理上越来越深入而广泛的应用,管理信息系统的实施在技术上已逐步成熟。本文介绍了教师工作量管理系统的开发全过程。通过分析教师工作量管理系统管理的不足,创建了一个计算机管理教师工作量管理系统的方案。文章介绍了教师工作量......
  • springboot项目基于pom.xml中的maven实现多环境配置
    在SpringBoot项目中,我们可以通过在pom.xml中配置Maven插件,结合Spring的Profile实现多环境配置。下面是一种可能的实现方式:首先,在pom.xml中添加Maven插件,该插件可以用于编译、测试和打包项目。为了能够支持多环境配置,我们可以在profiles标签内定义不同的profile,然后在build标签内的......
  • idea系列---【上一次打开springboot项目还好好的,现在打开突然无法启动了】
    问题昨天走的时候项目还能正常启动,今天来了之后突然报下面的错误:Error:Kotlin:ModulewascompiledwithanincompatibleversionofKotlin.Thebinaryversionofitsmetadatais1.7.1,expectedversionis1.1.16.解决方案点击idea:Build>RebuildProject重新编译即可......
  • springboot nacos使用yaml配置list方式
    方式一配置项:app:demo:list1:xiaohong,xiaominglist2:>xiaohong,xiaominglist1和list2看起来是2种风格,其实都是同一种写法,以逗号分隔java代码:@Data@ComponentpublicclassAppConfig1{@Value("${app.demo.list1}")privateList<Strin......
  • vs2015编译glib2.5.0
    1、首先安装vs2015,以及下载一个glib2.50的源码包2、编译glib的问题①首先解决提示找不到libintl.h的问题需要编译安装libintl模块,可通过下载到glib仓库下载gettext-runtime-dev_0.18.1.1-1_win32包,里面提供了libintl.h和intl.lib②解决找不到pcre.h的问题到官网下载pcre安装......
  • SpringBoot获取配置文件-@Value、@ConfigurationProperties方式
    配置文件yml#phantomjs的位置地址phantomjs:binPath:windows:binPath-winlinux:binPath-linuxjsPath:windows:jsPath-winlinux:jsPath-linuximagePath:windows:imagePath-winlinux:imagePath-linuxphantomjs2:binPath2:I‘......
  • SpringBoot 单元测试
    1、什么是单元测试?单元测试(UnitTesting)是一种软件测试方法,用于验证和确认代码中的各个单元(通常是函数、方法或类)是否按照预期工作。单元测试旨在检测代码中的小部分,以确保其功能的正确性。2、单元测试有哪些好处?在单元测试中使用模拟对象来替代实际的数据库访问操作,不会实际修改数......
  • Spring Boot 解决跨域问题
    在SpringBoot中解决跨域问题可以采用以下两种方式:使用@CrossOrigin注解在控制器类或方法上添加@CrossOrigin注解即可实现跨域访问控制。例如:@CrossOrigin(origins="http://localhost:8080")@RestControllerpublicclassMyController{@GetMapping("/hello")......