Rust actix-web框架跨域請(qǐng)求配置
在做Web服務(wù)時(shí)使用的是與主站配置的是fb.net
, 另外個(gè)成員列表服務(wù)是m1.fb.net
,這會(huì)造成一個(gè)跨域問(wèn)題屯远。在瀏覽器下使用XML Http Request
或者fetch
發(fā)出一個(gè)HTTP請(qǐng)求苫昌,假如這個(gè)HTTP的協(xié)議颤绕、主機(jī)名或者端口任意一個(gè)與當(dāng)前網(wǎng)頁(yè)地址有不一致時(shí),為了安全瀏覽器會(huì)限制響應(yīng)結(jié)果祟身,通常這類問(wèn)題就是所謂的跨域問(wèn)題奥务。
可以參考:https://segmentfault.com/a/1190000012550346
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CORS
解決跨域問(wèn)題的方式有很多,比如jsonp
月而,iframe
等等。但在這里议纯,我使用HTTP協(xié)議里約定的字段來(lái)解決這個(gè)問(wèn)題父款,這也是最干凈完美的解決方案。為了處理有跨域請(qǐng)求的特殊場(chǎng)景瞻凤,HTTP協(xié)議里有一個(gè)特殊的響應(yīng)頭字段Access-Control-Allow-Origin
憨攒,意思允許訪問(wèn)的Origin
,值可以是通配符*
阀参,允許所有肝集,或者寫上一個(gè)具體的Origin
值。
在actix-web
里蛛壳, 我們需要配合actix_cors
來(lái)處理關(guān)于跨域請(qǐng)求的配置杏瞻,以下是一個(gè)例子
Cargo.toml
[package]
name = "guser"
version = "1.0.0"
authors = ["Zhiyong <lizhiyong5653@gmail.com>"]
edition = "2021"
[dependencies]
actix-rt = "1.0.0"
actix-web = "2.0.0"
actix-cors = "0.2.0"
futures = "0.3"
main.rs
HttpServer::new(move || {
let state = AppState { gs: svc.clone() };
App::new()
.app_data(web::FormConfig::default().limit(1024 * 16))
.data(state)
.wrap(middleware::Compress::new(ContentEncoding::Gzip))
.wrap(Cors::new()
.allowed_origin("https://xxx")
.allowed_origin("http://xxx")
.allowed_origin("https://xxx")
.allowed_origin("http://xxx")
.allowed_origin("http://local.xxx")
.allowed_origin("https://local.xxx")
//.send_wildcard()
.allowed_methods(vec!["GET", "POST", "DELETE", "OPTIONS"])
.allowed_headers(vec!["Access-Control-Allow-Headers",
"Authorization", "authorization", "X-Requested-With",
"Content-Type", "content-type", "Origin", "Client-id",
"user-agent", "User-Agent", "Accept", "Referer","referer",
"Nonce", "signature", "Timestamp","AppKey","x-super-properties",
"X-Super-Properties"])
.max_age(3600)
.finish())
.configure(routes)
})
.keep_alive(KeepAlive::Timeout(90))
//.workers(16)
.bind(http_addr)
.and_then(|server| {
info!("bind server to address {}", http_addr);
Ok(server)
})
.unwrap_or_else(|_err| {
error!("could not bind server to address {}", http_addr);
error!("error : {}", _err.to_string());
exit(-1)
})
.run()
.await
.expect("Could not run server")
測(cè)試一下
lizhiyong@lizhiyongdeMacBook-Pro ~ % curl -iv -H 'Origin:https://xxx' http://127.0.0.1:80
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.64.1
> Accept: */*
> Origin:https://xxx
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< content-length: 12
content-length: 12
< vary: Origin
vary: Origin
< content-type: text/plain; charset=utf-8
content-type: text/plain; charset=utf-8
< access-control-allow-origin: https://xxx
access-control-allow-origin: https://xxx
< date: Mon, 26 Apr 2021 08:42:03 GMT
date: Mon, 26 Apr 2021 08:42:03 GMT
<
* Connection #0 to host 127.0.0.1 left intact
Hello world!* Closing connection 0
lizhiyong@lizhiyongdeMacBook-Pro ~ %
如果我們把Origin換成另一個(gè)域名,則會(huì)報(bào)錯(cuò)
lizhiyong@lizhiyongdeMacBook-Pro ~ % curl -iv -H 'Origin: https://www.qttcd.net' http://127.0.0.1:80
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.64.1
> Accept: */*
> Origin: https://www.qttcd.net
>
< HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
< content-length: 42
content-length: 42
< date: Mon, 26 Apr 2021 08:41:22 GMT
date: Mon, 26 Apr 2021 08:41:22 GMT
<
* Connection #0 to host 127.0.0.1 left intact
Origin is not allowed to make this request* Closing connection 0
lizhiyong@lizhiyongdeMacBook-Pro ~ %
400出錯(cuò)了衙荐,提示
Origin is not allowed to make this request
不允許的Origin請(qǐng)求捞挥,另外http和https視為不同的origin,都需要添加支持忧吟。
allowed_origin("http://www.qttc.net")
allowed_origin("https://www.qttc.net")
如果你需要允許所有的Origin
砌函,也就是不做限制的話,那么使用*
號(hào)做通配符
allowed_origin("*")
通常來(lái)說(shuō)不建議這么干溜族,
當(dāng)然如果有nginx做負(fù)載的話讹俊,也可以在nginx上部署,此時(shí)后端服務(wù)可以不用如此實(shí)現(xiàn)了煌抒,否則可能會(huì)出現(xiàn):