pringboot后端解决跨域问题

 2023-09-11 阅读 23 评论 0

摘要:pringboot后端解决跨域问题 目录结构: 只需要编写一个类,实现WebMvcConfigurer接口中的addCorsMappings方法即可: import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegist

pringboot后端解决跨域问题

目录结构:
在这里插入图片描述
只需要编写一个类,实现WebMvcConfigurer接口中的addCorsMappings方法即可:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/*** 前端请求后端的数据,因为前端端口后是8080,后端端口后是8181* 这个类的功能是是解决跨域问题(跨域问题可以在前端解决亦可以在后端解决),这就是在后端解决*/
@Configuration
public class CrosConfig implements WebMvcConfigurer { //implements WebMvcConfigurer重写下面的方法@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS").allowCredentials(true).maxAge(3600).allowedHeaders("*");}
}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/47675.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息