SpringBoot學習筆記(7)-----CORS支持解決跨域問題

在實際應用開發中,跨域是一個比較常見的問題,解決方法能夠用jsonp,frame,cors等,javascript

這裏示例的是SpringBoot對CORS的支持的三種實現方式html

第一種:配置一種全局的支持,這種方式須要新增一個配置類以下:java

 
package com.wangx.boot.config; import org.springframework.context.annotation.Bean; 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 CORSConfiguration { @Bean public WebMvcConfigurer webMvcConfigurer () { return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) {
          //容許全部/api/路徑下的訪問跨域,容許http://localhost:8080的源跨域 registry.addMapping("/api/**").allowedOrigins("http://localhost:8080"); } }; } }
 

  這樣在別的服務中想要訪問該服務的api路徑下的全部資源都會被容許跨域請求,本示例在8080端口的服務下請求8082的服務的資源,請求頁面代碼以下:web

  

 
<body> <div onclick="getLabelsGet()">這是一個事件</div> <p id = "data"></p> </body> <script type="text/javascript"> /* 建立 XMLHttpRequest 對象 */ var xmlHttp; function GetXmlHttpObject(){ if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest(); }else{// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } return xmlhttp; } // -----------ajax方法-----------// function getLabelsGet(){ xmlHttp=GetXmlHttpObject(); if (xmlHttp==null){ alert('您的瀏覽器不支持AJAX!'); return; } var url="http://localhost:8082/api/say?name=jack"; xmlHttp.open("GET",url,true); xmlHttp.onreadystatechange=getOkGet;//發送事件後,收到信息了調用函數  xmlHttp.send(); } function getOkGet(){ if(xmlHttp.readyState==1||xmlHttp.readyState==2||xmlHttp.readyState==3){ // 本地提示:加載中  } if (xmlHttp.readyState==4 && xmlHttp.status==200){ var d= xmlHttp.responseText; // 處理返回結果 var data = document.getElementById("data"); data.innerHTML = d; console.log(d); } } </script>
 

  當點擊」這是一個事件「後,訪問結果以下:ajax

 這樣就實現了從一個服務器頁面訪問另外一個服務器之間容許跨域的問題。除了註冊一個Bean以外,也能夠使用配置類實現WebMvcConfigurer接口來實現跨域,原理同樣,都是經過重寫addCorsMappings方法來實現跨域的,實現接口的代碼以下:spring

 
package com.wangx.boot.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 CORSConfiguration implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**").allowedOrigins("http://localhost:8080"); } }
 

第二種:使用@CrossOrigin註解來實現跨域問題,這是最簡單的也是能夠控制粒度的一種方式,使用註解做用在方法上。則只用該方法容許跨域訪問,做用在類上就只有該類中的全部接口容許訪問,具體代碼以下:json

 @CrossOrigin註解做用到方法上:api

 
package com.wangx.boot.controller; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class ApiController { @CrossOrigin(origins = {"http://localhost:8080", "null"}) @RequestMapping("/say") public String say(String name) { System.out.println(name); return "name:" + name + "hello"; } @RequestMapping("/to") public String to(String name) { System.out.println(name); return "name:" + name + "hello"; } }
 

  當在其餘服務器頁面上訪問方法say()時,成功返回數據,訪問to時報跨域錯誤。跨域

@CrossOrigin做用到類上:瀏覽器

 
package com.wangx.boot.controller; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") @CrossOrigin(origins = {"http://localhost:8080", "null"}) public class ApiController { @RequestMapping("/say") public String say(String name) { System.out.println(name); return "name:" + name + "hello"; } @RequestMapping("/to") public String to(String name) { System.out.println(name); return "name:" + name + "hello"; } }
 

  做用到類上時,類中的全部接口均可以被訪問別成功返回數據。運行結果與方式一相同

原文 SpringBoot學習筆記(7)-----CORS支持解決跨域問題

相關文章
相關標籤/搜索