actix-session 将会话数据序列化为 JSON,对 cookie 进行签名并将 cookie的名称设置为 actix-session。
要验证,请运行最小的 cookie-session-example并使用 curl 发出请求:
$ curl -v localhost:8080
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< content-length: 8
< content-type: text/plain; charset=utf-8
< set-cookie: actix-session=ZTe%2Fb%2F085+VQcxL%2FQRKCnldUxzoc%2FNEOQe94PTBGUfc%3D%7B%22counter%22%3A%221%22%7D; HttpOnly; Path=/
< date: Thu, 11 Jul 2019 21:22:38 GMT
解码decodeURIComponent
给出:
> decodeURIComponent("ZTe%2Fb%2F085+VQcxL%2FQRKCnldUxzoc%2FNEOQe94PTBGUfc%3D%7B%22counter%22%3A%221%22%7D")
'ZTe/b/085+VQcxL/QRKCnldUxzoc/NEOQe94PTBGUfc={"counter":"1"}'
据我所知,ZTe/b/085+VQcxL/QRKCnldUxzoc/NEOQe94PTBGUfc=
是签名。
这可能不是您的 PHP 脚本正在执行的操作,因此您可能想HttpRequest::headers
直接使用。例如,通过创建您自己的Session
类型,然后在您的处理程序中使用该类型:
use actix_web::{web, App, Error, HttpServer, HttpRequest, HttpResponse, FromRequest};
use actix_web::dev::Payload;
use actix_web::http::header::{COOKIE, SET_COOKIE};
use actix_web::error::ErrorUnauthorized;
fn main() {
HttpServer::new(|| {
App::new()
.route("/set", web::to(set_cookie))
.route("/get", web::to(get_cookie))
})
.bind("127.0.0.1:8000")
.expect("Cannot bind to port 8000")
.run()
.expect("Unable to run server");
}
fn set_cookie() -> HttpResponse {
HttpResponse::Ok()
.header(SET_COOKIE, Session::cookie("0123456789abcdef"))
.body("cookie set")
}
fn get_cookie(session: Session) -> HttpResponse {
HttpResponse::Ok()
.header(SET_COOKIE, Session::cookie("new_session_value"))
.body(format!("Got cookie {}", &session.0))
}
struct Session(String);
impl Session {
const COOKIE_NAME: &'static str = "my-session";
fn cookie(value: &str) -> String {
String::from(Self::COOKIE_NAME) + "=" + value
}
}
impl FromRequest for Session {
type Error = Error;
type Future = Result<Self, Error>;
type Config = ();
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
for header in req.headers().get_all(COOKIE) {
// check if header is UTF-8
if let Ok(value) = header.to_str() {
// split into cookie values
for c in value.split(';').map(|s| s.trim()) {
// split at '='
if let Some(pos) = c.find('=') {
// is session key?
if Self::COOKIE_NAME == &c[0..pos] {
return Ok(Session(String::from(&c[(pos + 1)..])));
}
}
}
}
}
Err(ErrorUnauthorized("Session cookie missing"))
}
}
结果(为简洁起见,删除了不相关的标题):
$ curl -v localhost:8000/get
< HTTP/1.1 401 Unauthorized
Session cookie missing⏎
$ curl -v localhost:8000/set
< HTTP/1.1 200 OK
< set-cookie: my-session=0123456789abcdef
cookie set⏎
$ curl -v --cookie my-session=0123456789abcdef localhost:8000/get
> Cookie: my-session=0123456789abcdef
>
< HTTP/1.1 200 OK
< set-cookie: my-session=new_session_value
Got cookie 0123456789abcdef⏎
您还可以在浏览器中观察结果,网址为http://localhost:8000/set和http://localhost:8000/get。
这非常简单,但可以让您完全控制会话 cookie。
注意:上述解决方案对保护 cookie 没有任何作用。