国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 詳解Spring Boot 2.0.2+Ajax解決跨域請求的問題

詳解Spring Boot 2.0.2+Ajax解決跨域請求的問題

2021-07-19 08:59wencst Java教程

這篇文章主要介紹了詳解Spring Boot 2.0.2+Ajax解決跨域請求的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

問題描述

后端域名為a.abc.com,前端域名為b.abc.com。瀏覽器在訪問時,會出現(xiàn)跨域訪問。瀏覽器對于javascript的同源策略的限制。

http請求時,請求本身會返回200,但是返回結(jié)果不會走success,并且會在瀏覽器console中提示:

已攔截跨源請求:同源策略禁止讀取位于 https://www.baidu.com/ 的遠程資源。(原因:cors 頭缺少 ‘access-control-allow-origin')。

解決方案

1.jsonp

2.引用a站的js

3.nginx做a站的反向代理

4.后端服務(wù)放開跨域請求

其中,以最后兩種見常。

詳細方案

本文主要描述第四種解決方案:后端服務(wù)放開跨域請求。

spring boot中放開跨域請求很簡單。

1.增加一個configuration類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 wencst
 * @creation 2017年8月18日
 */
@configuration
public class customcorsconfiguration {
 private corsconfiguration buildconfig() {
 corsconfiguration corsconfiguration = new corsconfiguration();
 corsconfiguration.addallowedorigin("*");
 corsconfiguration.addallowedheader("*");
 corsconfiguration.addallowedmethod("*");
 return corsconfiguration;
 }
 @bean
 public corsfilter corsfilter() {
 urlbasedcorsconfigurationsource source = new urlbasedcorsconfigurationsource();
 source.registercorsconfiguration("/**", buildconfig());
 return new corsfilter(source);
 }
}

增加此類以后,非同源http訪問可以正常進行了,但是會不會有什么問題呢?

對于大部分網(wǎng)站依然需要使用cookie作為前后端傳輸數(shù)據(jù)的媒介,然而默認非同源請求是不攜帶cookie信息的。

2.服務(wù)端允許跨域攜帶cookie信息

在spring boot2.0.2中,允許跨域設(shè)置比較簡單,只需增加一個configuration類即可。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 wencst
 * @creation 2017年8月18日
 */
@configuration
public class customcorsconfiguration {
 private corsconfiguration buildconfig() {
 corsconfiguration corsconfiguration = new corsconfiguration();
 corsconfiguration.addallowedorigin("*");
 corsconfiguration.addallowedheader("*");
 corsconfiguration.addallowedmethod("*");
 corsconfiguration.addexposedheader("content-type");
 corsconfiguration.addexposedheader( "x-requested-with");
 corsconfiguration.addexposedheader("accept");
 corsconfiguration.addexposedheader("origin");
 corsconfiguration.addexposedheader( "access-control-request-method");
 corsconfiguration.addexposedheader("access-control-request-headers");
 corsconfiguration.setallowcredentials(true);
 return corsconfiguration;
 }
 @bean
 public corsfilter corsfilter() {
 urlbasedcorsconfigurationsource source = new urlbasedcorsconfigurationsource();
 source.registercorsconfiguration("/**", buildconfig());
 return new corsfilter(source);
 }
}

增加信息后,在前端依然需要調(diào)整ajax請求,才能在非同源請求中攜帶cookie信息。

3.前端調(diào)整

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$.ajax({
 url: 'http://beta.roboming.com/api.php?s=/public/adminlogin.html',
 type: 'post',
 async:true,
 xhrfields:{
  withcredentials:true
 },
 data: {
  username:username,
  password:pwd
 },
 success: function(respon){
  console.log(respon);
  var res=eval(respon);
 },
 error: function(){
  alert('服務(wù)器發(fā)生錯誤!');
 }
});

此時,當前端向后端服務(wù)做跨域請求時,增加

?
1
2
3
xhrfields:{
  withcredentials:true
},

就會帶上cookie信息了,同理會帶上token/sessionid等等內(nèi)容。

測試方法

spring boot中增加一個controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@controller
public class logincontroller {
 @requestmapping(value = "setstring")
 @responsebody
 public string setstring(httpservletrequest request, httpservletresponse response,@requestparam string value) {
 request.getsession().setattribute("username", value);
 return "ok";
 }
 @requestmapping(value = "getstring")
 @responsebody
 public string getstring(httpservletrequest request, httpservletresponse response) {
 string username = (string)request.getsession().getattribute("username");
 return username;
 }
}

增加一個index.html,來訪問跨域訪問。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<html>
<head>
<meta charset="utf-8">
<title>跨域請求</title>
<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<button onclick="set()">set</button>
<br><br>
<button onclick="get()">get</button>
<script>
function set(){
 $.ajax({
 url:'http://wencst.vicp.net/setstring?value=10',
 xhrfields:{
  withcredentials:true
 },
 success:function(result){
 alert(result);
 }
 });
}
function get(){
 $.ajax({
 url:'http://wencst.vicp.net/getstring',
 xhrfields:{
  withcredentials:true
 },
 success:function(result){
 alert(result);
 }
 });
}
</script>
</body>
</html>

html文件可以單獨本地訪問即可出現(xiàn)效果,并不一定要形成服務(wù)訪問。

當服務(wù)端不允許跨域訪問時,html文件訪問均報錯,并調(diào)用失敗。

當服務(wù)端允許跨域訪問時,html請求訪問成功。

當服務(wù)端開啟cookie傳遞,并在html文件中增加 xhrfields參數(shù)時,session生效

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://www.wencst.com/archives/1635

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品视频网 | 色8888www视频在线观看 | 最新日韩av | 一级电影免费在线观看 | 成人精品久久久 | 精品久久久久久久久久久久久久 | 男女免费观看在线爽爽爽视频 | 欧美成人高清视频 | 久久中文字幕一区二区 | 日韩毛片在线观看 | 美女爽到呻吟久久久久 | 日韩电影中文字幕 | 男人天堂v | 玖玖爱视频在线 | 亚洲免费成人 | 国产综合精品一区二区三区 | 黑人巨大精品欧美一区免费视频 | 91精品国产91久久综合 | 99re6在线视频精品免费 | 欧美在线免费视频 | 国产999精品久久久久久 | 男人天堂网站 | 久在线视频 | 91精品久久久久久久久 | 亚洲精品电影在线观看 | 色综合久久久久 | 久久久精品蜜桃 | 国产高清不卡在线 | 曰韩在线| 99re在线观看 | 91极品视频在线观看 | 性视屏| 国产精品视频导航 | 国产精品久久久久桃色tv | 亚洲精品一区二区三区在线 | 69免费视频 | 国产精品99久久免费观看 | 久久一区二区视频 | 依人在线免费视频 | 91久久夜色精品国产网站 | 夜夜操天天操 |