Springboot后台设置允许跨域的方法

Springboot后台设置允许跨域的方法方案一 packagecom example springbootde importorg springframew boot SpringApplic importorg springframew boot autoconfigur SpringBootAp importorg springframew web serv

方案一:

package com.example.springbootdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @SpringBootApplication public class SpringbootdemoApplication extends WebMvcConfigurerAdapter { public static void main(String[] args) { SpringApplication.run(SpringbootdemoApplication.class, args); } @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/") .allowCredentials(true) .allowedHeaders("*") .allowedOrigins("*") .allowedMethods("*"); } }

方案二:

 

//在允许跨域请求的controller中使用@CrossOrigin 注解 import org.springframework.web.bind.annotation.CrossOrigin; @CrossOrigin @RestController public class HelloWorldController { / * * */ }

其他明细参考:

https://blog.csdn.net/Colton_Null/article/details/

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/230657.html原文链接:https://javaforall.net

(0)
上一篇 2026年2月10日 下午4:01
下一篇 2026年2月10日 下午4:22


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号