教你用Java SpringBoot如何解決跨域
跨域
什么是跨域
請(qǐng)求url的協(xié)議,域名,端口三者之間任意一個(gè)與當(dāng)前頁(yè)面url不同的即為跨域。

CORS
CORS(Cross-origin resource sharing-跨源資源共享)允許網(wǎng)頁(yè)從其他域向?yàn)g覽器請(qǐng)求額外的資源
SpringBoot解決跨域方案
1.使用@CrossOrigin注解
該注解添加在你想要讓某接口允許跨域的的,類(lèi)上面,或者實(shí)現(xiàn)方法上面。
該注解包含的屬性:orgins,allowdHeaders,methods,exposedHeaders,allowCreden,maxAge。

2.Spring框架全局配置CORS配置
2.1Spring MVC CORS 使用WebMvcConfigurerAdapter配置!
2.2Spring Boot CORS 使用WebMvcConfigurer配置!
2.3CORS 使用Spring Security配置!
具體實(shí)現(xiàn)
1.使用@CrossOrigin注解
1.1目錄結(jié)構(gòu)

DemoApplication.java
package com.example.crossdomain.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
CorsTestController.java
package com.example.crossdomain.demo.controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/demo")
@RestController
@CrossOrigin("https://blog.csdn.net") // 只有指定域名可以訪問(wèn)該類(lèi)下所有接口
public class CorsTestController {
@GetMapping("/sayHello")
public String sayHello(){
return "Hello world";
}
}
1.2運(yùn)行結(jié)果


2.使用@CrossOrigin注解
2.1Spring MVC CORS 使用WebMvcConfigurerAdapter配置!
2.1.1目錄結(jié)構(gòu)

2.2.2添加CorsConfiguration.java
package com.example.crossdomain.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class CorsConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("GET", "POST");
}
}
2.2.3運(yùn)行結(jié)果


2.3Spring Boot CORS 使用WebMvcConfigurer配置
package com.example.crossdomain.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
@Configuration
public class CorsConfiguration
{
@Bean
public WebMvcConfigurer corsConfigurer()
{
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
};
}
}
2.4CORS 使用Spring Security配置
2.4.1目錄結(jié)構(gòu)

2.4.2添加WebSecurityConfig.java
package com.example.crossdomain.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import java.util.Arrays;
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors();
}
@Bean
CorsConfigurationSource corsConfigurationSource()
{
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*"); //同源配置,*表示任何請(qǐng)求都視為同源,若需指定ip和端口可以改為如“l(fā)ocalhost:8080”,多個(gè)以“,”分隔;
corsConfiguration.addAllowedHeader("*");//header,允許哪些header,本案中使用的是token,此處可將*替換為token;
corsConfiguration.addAllowedMethod("*"); //允許的請(qǐng)求方法,PSOT、GET等
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration);
return source;
}
}
2.4.3運(yùn)行結(jié)果


代碼獲取
參考鏈接
Spring boot 入門(mén)之CORS 跨域配置詳解
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
java 中設(shè)計(jì)模式(裝飾設(shè)計(jì)模式)的實(shí)例詳解
這篇文章主要介紹了java 中設(shè)計(jì)模式(裝飾設(shè)計(jì)模式)的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09
如何剔除eureka無(wú)效和down狀態(tài)的問(wèn)題
這篇文章主要介紹了如何剔除eureka無(wú)效和down狀態(tài)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
解決springboot項(xiàng)目不配置數(shù)據(jù)源啟動(dòng)報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了解決springboot項(xiàng)目不配置數(shù)據(jù)源啟動(dòng)報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
解決 IDEA 創(chuàng)建 Gradle 項(xiàng)目沒(méi)有src目錄問(wèn)題
這篇文章主要介紹了解決 IDEA 創(chuàng)建 Gradle 項(xiàng)目沒(méi)有src目錄問(wèn)題,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
使用GSON庫(kù)將Java中的map鍵值對(duì)應(yīng)結(jié)構(gòu)對(duì)象轉(zhuǎn)換為JSON
GSON是由Google開(kāi)發(fā)并開(kāi)源的實(shí)現(xiàn)Java對(duì)象與JSON之間相互轉(zhuǎn)換功能的類(lèi)庫(kù),這里我們來(lái)看一下使用GSON庫(kù)將Java中的map鍵值對(duì)應(yīng)結(jié)構(gòu)對(duì)象轉(zhuǎn)換為JSON的示例:2016-06-06
解析Neatbeans(常見(jiàn)錯(cuò)誤) build-impl.xml:305: Compile failed
本篇文章是對(duì)Neatbeans(常見(jiàn)錯(cuò)誤) build-impl.xml:305: Compile failed的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07
MyBatis-Plus MetaObjectHandler的原理及使用
MyBatis-Plus的MetaObjectHandler接口允許開(kāi)發(fā)者自動(dòng)填充實(shí)體類(lèi)字段,如創(chuàng)建時(shí)間、更新時(shí)間等公共字段,減少代碼重復(fù),提高數(shù)據(jù)一致性和完整性,感興趣的可以了解一下2024-10-10
JPA @Query時(shí),無(wú)法使用limit函數(shù)的問(wèn)題及解決
這篇文章主要介紹了JPA @Query時(shí),無(wú)法使用limit函數(shù)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

