CORS(Cross-Origin Resource Sharing)"跨域資源共享",是一個W3C標準,它容許瀏覽器向跨域服務器發送Ajax請求,打破了Ajax只能訪問本站內的資源限制,CORS在不少地方都有被使用,微信支付的JS支付就是經過JS向微信服務器發送跨域請求。開放Ajax訪問可被跨域訪問的服務器大大減小了後臺開發的工做,先後臺工做也能夠獲得很好的明確以及分工,下面咱們就看講一下如何讓SpringBoot項目支持CORS跨域。javascript
一、新建項目sc-cors,對應的pom.xml文件以下html
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>spring-cloud</groupId> <artifactId>sc-cors</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>sc-cors</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
二、新建配置類,配置知足什麼的條件的能夠跨域訪問java
package sc.cors.config; 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 CorsConfig implements WebMvcConfigurer{ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/cors/**") .allowedMethods("*") .allowedOrigins("*") .allowedHeaders("*"); } }
三、新建controller,包含一個能夠跨域訪問的資源,一個不能夠跨域訪問的資源jquery
package sc.cors.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import sc.cors.model.User; @RestController public class CorsController { @RequestMapping("/cors/getUserInfo") public Map<String, Object> getUserInfo() { Map<String, Object> resp = new HashMap<String, Object>(); resp.put("code", "success"); resp.put("message", "success"); User user = new User(); user.setId(1); user.setPosition("cto"); user.setUserName("huang jinjin"); resp.put("body", user); return resp; } @RequestMapping("/nocors/listUserInfo") public Map<String, Object> listUserInfo() { Map<String, Object> resp = new HashMap<String, Object>(); resp.put("code", "success"); resp.put("message", "success"); List<User> list = new ArrayList<User>(); User user = new User(); user.setId(1); user.setPosition("cto"); user.setUserName("huang jinjin"); list.add(user); resp.put("body", list); return resp; } }
四、其餘項目文件以下圖
web
五、在新建一個項目sc-cors-web,該項目比較簡單,包含一個比較重要的html文件ajax
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>cors</title> <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script type="text/javascript"> $(function(){ $("#getUserInfo").click(function(){ $.ajax({ url: "http://127.0.0.1:9088/cors/getUserInfo", success: function(data){ console.log(data) alert("getUserInfo"); } }) }); }); $(function(){ $("#listUserInfo").click(function(){ $.ajax({ url: "http://127.0.0.1:9088/nocors/listUserInfo", success: function(data){ console.log(data) alert("listUserInfo"); } }) }); }); </script> </head> <body> <input type="button" id="getUserInfo" value="CORS跨域請求getUserInfo"/><br><br/> <input type="button" id="listUserInfo" value="CORS跨域請求listUserInfo"/> </body> </html>
備註:spring
sc-cors項目對應的端口爲9088 sc-cors-web項目對應的端口爲9087
六、分別啓動項目sc-cors和sc-cors-web
七、驗證跨域請求
訪問http://127.0.0.1:9087/index.html
點擊CORS跨域請求getUserInfo
點擊CORS跨域請求listUserInfo
apache