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

CORS
CORS(Cross-origin resource sharing-跨源資源共享)允許網(wǎng)頁從其他域向瀏覽器請求額外的資源
SpringBoot解決跨域方案
1.使用@CrossOrigin注解
該注解添加在你想要讓某接口允許跨域的的,類上面,或者實現(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配置!
具體實現(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") // 只有指定域名可以訪問該類下所有接口
public class CorsTestController {
@GetMapping("/sayHello")
public String sayHello(){
return "Hello world";
}
}
1.2運行結(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運行結(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("*"); //同源配置,*表示任何請求都視為同源,若需指定ip和端口可以改為如“l(fā)ocalhost:8080”,多個以“,”分隔;
corsConfiguration.addAllowedHeader("*");//header,允許哪些header,本案中使用的是token,此處可將*替換為token;
corsConfiguration.addAllowedMethod("*"); //允許的請求方法,PSOT、GET等
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration);
return source;
}
}
2.4.3運行結(jié)果


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

