這篇文章主要介紹了網站跨域有哪些解決方法,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
因為瀏覽器使用了同源策略
undefined
是為了保證用戶的信息安全,防止惡意網站竊取數據,如果網頁之間不滿足同源要求,將不能:
同源策略的非絕對性:
<script></script> <img/> <iframe/> <link/> <video/> <audio/>
等帶有src屬性的標簽可以從不同的域加載和執行資源。其他插件的同源策略:flash、java applet、silverlight、googlegears等瀏覽器加載的第三方插件也有各自的同源策略,只是這些同源策略不屬于瀏覽器原生的同源策略,如果有漏洞則可能被黑客利用,從而留下XSS攻擊的后患
所謂的同源指:域名、網絡協議、端口號相同,三條有一條不同就會產生跨域。 例如:你用瀏覽器打開http://baidu.com,瀏覽器執行JavaScript腳本時發現腳本向http://cloud.baidu.com域名發請求,這時瀏覽器就會報錯,這就是跨域報錯。
$.ajax({
url: 'http://192.168.1.114/yii/demos/test.php', //不同的域
type: 'GET', // jsonp模式只有GET 是合法的
data: {
'action': 'aaron'
},
dataType: 'jsonp', // 數據類型
jsonp: 'backfunc', // 指定回調函數名,與服務器端接收的一致,并回傳回來
})<script>標簽來調用服務器提供的 js腳本。jquery 會在window對象中加載一個全局的函數,當 <script>代碼插入時函數執行,執行完畢后就 <script>會被移除。同時jquery還對非跨域的請求進行了優化,如果這個請求是在同一個域名下那么他就會像正常的 Ajax請求一樣工作。)try {
HttpClient client = HttpClients.createDefault(); //client對象
HttpGet get = new HttpGet("http://localhost:8080/test"); //創建get請求
CloseableHttpResponse response = httpClient.execute(get); //執行get請求
String mes = EntityUtils.toString(response.getEntity()); //將返回體的信息轉換為字符串
System.out.println(mes);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}在SpringBoot2.0 上的跨域 用以下代碼配置 即可完美解決你的前后端跨域請求問題
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
/**
* 實現基本的跨域請求
* @author linhongcun
*
*/
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
final CorsConfiguration corsConfiguration = new CorsConfiguration();
/*是否允許請求帶有驗證信息*/
corsConfiguration.setAllowCredentials(true);
/*允許訪問的客戶端域名*/
corsConfiguration.addAllowedOrigin("*");
/*允許服務端訪問的客戶端請求頭*/
corsConfiguration.addAllowedHeader("*");
/*允許訪問的方法名,GET POST等*/
corsConfiguration.addAllowedMethod("*");
urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
return new CorsFilter(urlBasedCorsConfigurationSource);
}
}服務網關(zuul)又稱路由中心,用來統一訪問所有api接口,維護服務。
Spring Cloud Zuul通過與Spring Cloud Eureka的整合,實現了對服務實例的自動化維護,所以在使用服務路由配置的時候,我們不需要向傳統路由配置方式那樣去指定具體的服務實例地址,只需要通過Ant模式配置文件參數即可
http://a.a.com:81/A中想訪問 http://b.b.com:81/B 那么進行如下配置即可www.my.com/A 里面即可訪問 www.my.com/Bserver {
listen 80;
server_name www.my.com;
location /A {
proxy_pass http://a.a.com:81/A;
index index.html index.htm;
}
location /B {
proxy_pass http://b.b.com:81/B;
index index.html index.htm;
}
}http://b.b.com:80/Api中想訪問 http://b.b.com:81/Api 那么進行如下配置即可server {
listen 80;
server_name b.b.com;
location /Api {
proxy_pass http://b.b.com:81/Api;
index index.html index.htm;
}
}感謝你能夠認真閱讀完這篇文章,希望小編分享網站跨域有哪些解決方法內容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。