mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-30 10:32:55 +01:00
Deploying to gh-pages from @ actix/actix-extras@4adc9f8884 🚀
This commit is contained in:
parent
da0a9406d3
commit
403e56a632
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Session management for Actix Web."><title>actix_session - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-dd39b87e5fcfba68.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="actix_session" data-themes="" data-resource-suffix="" data-rustdoc-version="1.81.0-nightly (f21554f7f 2024-06-08)" data-channel="nightly" data-search-js="search-9d4fa2dd5e5a7e17.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-118b08c4c78b968e.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-20a3ad099b048cf2.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-df360f571f6edeae.css"></noscript><link rel="icon" href="https://actix.rs/favicon.ico"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="../actix_session/index.html"><img src="https://actix.rs/img/logo.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../actix_session/index.html"><img src="https://actix.rs/img/logo.png" alt="logo"></a><h2><a href="../actix_session/index.html">actix_session</a><span class="version">0.9.0</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#traits">Traits</a></li></ul></section></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Crate <a class="mod" href="#">actix_session</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><span class="out-of-band"><a class="src" href="../src/actix_session/lib.rs.html#1-740">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Session management for Actix Web.</p>
|
||||
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Session management for Actix Web."><title>actix_session - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-dd39b87e5fcfba68.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="actix_session" data-themes="" data-resource-suffix="" data-rustdoc-version="1.81.0-nightly (f21554f7f 2024-06-08)" data-channel="nightly" data-search-js="search-9d4fa2dd5e5a7e17.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-118b08c4c78b968e.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-20a3ad099b048cf2.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-df360f571f6edeae.css"></noscript><link rel="icon" href="https://actix.rs/favicon.ico"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="../actix_session/index.html"><img src="https://actix.rs/img/logo.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../actix_session/index.html"><img src="https://actix.rs/img/logo.png" alt="logo"></a><h2><a href="../actix_session/index.html">actix_session</a><span class="version">0.9.0</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#traits">Traits</a></li></ul></section></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Crate <a class="mod" href="#">actix_session</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><span class="out-of-band"><a class="src" href="../src/actix_session/lib.rs.html#1-738">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Session management for Actix Web.</p>
|
||||
<p>The HTTP protocol, at a first glance, is stateless: the client sends a request, the server
|
||||
parses its content, performs some processing and returns a response. The outcome is only
|
||||
influenced by the provided inputs (i.e. the request content) and whatever state the server
|
||||
@ -97,13 +97,13 @@ the <code>redis-session</code> feature flag.</p>
|
||||
# ...
|
||||
actix-session = { version = "...", features = ["redis-session"] }
|
||||
</code></pre></div>
|
||||
<p>Add the <code>redis-session-native-tls</code> feature flag if you want to connect to Redis using a secured
|
||||
<p>Add the <code>redis-session-native-tls</code> feature flag if you want to connect to Redis using a secure
|
||||
connection (via the <code>native-tls</code> crate):</p>
|
||||
<div class="example-wrap"><pre class="language-toml"><code>[dependencies]
|
||||
# ...
|
||||
actix-session = { version = "...", features = ["redis-session-native-tls"] }
|
||||
</code></pre></div>
|
||||
<p>If you instead prefer depending on <code>rustls</code>, use the <code>redis-session-rustls</code> feature flag:</p>
|
||||
<p>If you, instead, prefer depending on <code>rustls</code>, use the <code>redis-session-rustls</code> feature flag:</p>
|
||||
<div class="example-wrap"><pre class="language-toml"><code>[dependencies]
|
||||
# ...
|
||||
actix-session = { version = "...", features = ["redis-session-rustls"] }
|
||||
|
@ -1 +1 @@
|
||||
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Documentation for Rustdoc"><title>Help</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="./static.files/${f}">`).join(""))</script><link rel="stylesheet" href="./static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="./static.files/rustdoc-dd39b87e5fcfba68.css"><meta name="rustdoc-vars" data-root-path="./" data-static-root-path="./static.files/" data-current-crate="actix_limitation" data-themes="" data-resource-suffix="" data-rustdoc-version="1.81.0-nightly (f21554f7f 2024-06-08)" data-channel="nightly" data-search-js="search-9d4fa2dd5e5a7e17.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="./static.files/storage-118b08c4c78b968e.js"></script><script defer src="./static.files/main-20a3ad099b048cf2.js"></script><noscript><link rel="stylesheet" href="./static.files/noscript-df360f571f6edeae.css"></noscript><link rel="icon" href="https://actix.rs/favicon.ico"></head><body class="rustdoc mod sys"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="./index.html"><img class="rust-logo" src="./static.files/rust-logo-151179464ae7ed46.svg" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="./index.html"><img class="rust-logo" src="./static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a><h2><a href="./index.html">Rustdoc</a><span class="version">1.81.0-nightly</span></h2></div><div class="version">(f21554f7f 2024-06-08)</div><h2 class="location">Help</h2><div class="sidebar-elems"></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Rustdoc help</h1><span class="out-of-band"><a id="back" href="javascript:void(0)" onclick="history.back();">Back</a></span></div><noscript><section><p>You need to enable JavaScript to use keyboard commands or search.</p><p>For more information, browse the <a href="https://doc.rust-lang.org/rustdoc/">rustdoc handbook</a>.</p></section></noscript></section></div></main></body></html>
|
||||
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Documentation for Rustdoc"><title>Help</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="./static.files/${f}">`).join(""))</script><link rel="stylesheet" href="./static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="./static.files/rustdoc-dd39b87e5fcfba68.css"><meta name="rustdoc-vars" data-root-path="./" data-static-root-path="./static.files/" data-current-crate="actix_web_httpauth" data-themes="" data-resource-suffix="" data-rustdoc-version="1.81.0-nightly (f21554f7f 2024-06-08)" data-channel="nightly" data-search-js="search-9d4fa2dd5e5a7e17.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="./static.files/storage-118b08c4c78b968e.js"></script><script defer src="./static.files/main-20a3ad099b048cf2.js"></script><noscript><link rel="stylesheet" href="./static.files/noscript-df360f571f6edeae.css"></noscript><link rel="icon" href="https://actix.rs/favicon.ico"></head><body class="rustdoc mod sys"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="./index.html"><img class="rust-logo" src="./static.files/rust-logo-151179464ae7ed46.svg" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="./index.html"><img class="rust-logo" src="./static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a><h2><a href="./index.html">Rustdoc</a><span class="version">1.81.0-nightly</span></h2></div><div class="version">(f21554f7f 2024-06-08)</div><h2 class="location">Help</h2><div class="sidebar-elems"></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Rustdoc help</h1><span class="out-of-band"><a id="back" href="javascript:void(0)" onclick="history.back();">Back</a></span></div><noscript><section><p>You need to enable JavaScript to use keyboard commands or search.</p><p>For more information, browse the <a href="https://doc.rust-lang.org/rustdoc/">rustdoc handbook</a>.</p></section></noscript></section></div></main></body></html>
|
@ -1 +1 @@
|
||||
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Settings of Rustdoc"><title>Settings</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="./static.files/${f}">`).join(""))</script><link rel="stylesheet" href="./static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="./static.files/rustdoc-dd39b87e5fcfba68.css"><meta name="rustdoc-vars" data-root-path="./" data-static-root-path="./static.files/" data-current-crate="actix_limitation" data-themes="" data-resource-suffix="" data-rustdoc-version="1.81.0-nightly (f21554f7f 2024-06-08)" data-channel="nightly" data-search-js="search-9d4fa2dd5e5a7e17.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="./static.files/storage-118b08c4c78b968e.js"></script><script defer src="./static.files/main-20a3ad099b048cf2.js"></script><noscript><link rel="stylesheet" href="./static.files/noscript-df360f571f6edeae.css"></noscript><link rel="icon" href="https://actix.rs/favicon.ico"></head><body class="rustdoc mod sys"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="./index.html"><img class="rust-logo" src="./static.files/rust-logo-151179464ae7ed46.svg" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="./index.html"><img class="rust-logo" src="./static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a><h2><a href="./index.html">Rustdoc</a><span class="version">1.81.0-nightly</span></h2></div><div class="version">(f21554f7f 2024-06-08)</div><h2 class="location">Settings</h2><div class="sidebar-elems"></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Rustdoc settings</h1><span class="out-of-band"><a id="back" href="javascript:void(0)" onclick="history.back();">Back</a></span></div><noscript><section>You need to enable JavaScript be able to update your settings.</section></noscript><script defer src="./static.files/settings-4313503d2e1961c2.js"></script></section></div></main></body></html>
|
||||
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Settings of Rustdoc"><title>Settings</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="./static.files/${f}">`).join(""))</script><link rel="stylesheet" href="./static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="./static.files/rustdoc-dd39b87e5fcfba68.css"><meta name="rustdoc-vars" data-root-path="./" data-static-root-path="./static.files/" data-current-crate="actix_web_httpauth" data-themes="" data-resource-suffix="" data-rustdoc-version="1.81.0-nightly (f21554f7f 2024-06-08)" data-channel="nightly" data-search-js="search-9d4fa2dd5e5a7e17.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="./static.files/storage-118b08c4c78b968e.js"></script><script defer src="./static.files/main-20a3ad099b048cf2.js"></script><noscript><link rel="stylesheet" href="./static.files/noscript-df360f571f6edeae.css"></noscript><link rel="icon" href="https://actix.rs/favicon.ico"></head><body class="rustdoc mod sys"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="./index.html"><img class="rust-logo" src="./static.files/rust-logo-151179464ae7ed46.svg" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="./index.html"><img class="rust-logo" src="./static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a><h2><a href="./index.html">Rustdoc</a><span class="version">1.81.0-nightly</span></h2></div><div class="version">(f21554f7f 2024-06-08)</div><h2 class="location">Settings</h2><div class="sidebar-elems"></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Rustdoc settings</h1><span class="out-of-band"><a id="back" href="javascript:void(0)" onclick="history.back();">Back</a></span></div><noscript><section>You need to enable JavaScript be able to update your settings.</section></noscript><script defer src="./static.files/settings-4313503d2e1961c2.js"></script></section></div></main></body></html>
|
@ -736,149 +736,145 @@
|
||||
<a href="#736" id="736">736</a>
|
||||
<a href="#737" id="737">737</a>
|
||||
<a href="#738" id="738">738</a>
|
||||
<a href="#739" id="739">739</a>
|
||||
<a href="#740" id="740">740</a>
|
||||
</pre></div><pre class="rust"><code><span class="doccomment">/*!
|
||||
Session management for Actix Web.
|
||||
|
||||
The HTTP protocol, at a first glance, is stateless: the client sends a request, the server
|
||||
parses its content, performs some processing and returns a response. The outcome is only
|
||||
influenced by the provided inputs (i.e. the request content) and whatever state the server
|
||||
queries while performing its processing.
|
||||
|
||||
Stateless systems are easier to reason about, but they are not quite as powerful as we need them
|
||||
to be - e.g. how do you authenticate a user? The user would be forced to authenticate **for
|
||||
every single request**. That is, for example, how 'Basic' Authentication works. While it may
|
||||
work for a machine user (i.e. an API client), it is impractical for a person—you do not want a
|
||||
login prompt on every single page you navigate to!
|
||||
|
||||
There is a solution - **sessions**. Using sessions the server can attach state to a set of
|
||||
requests coming from the same client. They are built on top of cookies - the server sets a
|
||||
cookie in the HTTP response (`Set-Cookie` header), the client (e.g. the browser) will store the
|
||||
cookie and play it back to the server when sending new requests (using the `Cookie` header).
|
||||
|
||||
We refer to the cookie used for sessions as a **session cookie**. Its content is called
|
||||
**session key** (or **session ID**), while the state attached to the session is referred to as
|
||||
**session state**.
|
||||
|
||||
`actix-session` provides an easy-to-use framework to manage sessions in applications built on
|
||||
top of Actix Web. [`SessionMiddleware`] is the middleware underpinning the functionality
|
||||
provided by `actix-session`; it takes care of all the session cookie handling and instructs the
|
||||
**storage backend** to create/delete/update the session state based on the operations performed
|
||||
against the active [`Session`].
|
||||
|
||||
`actix-session` provides some built-in storage backends: ([`CookieSessionStore`],
|
||||
[`RedisSessionStore`], and [`RedisActorSessionStore`]) - you can create a custom storage backend
|
||||
by implementing the [`SessionStore`] trait.
|
||||
|
||||
Further reading on sessions:
|
||||
- [RFC 6265](https://datatracker.ietf.org/doc/html/rfc6265);
|
||||
- [OWASP's session management cheat-sheet](https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html).
|
||||
|
||||
# Getting started
|
||||
To start using sessions in your Actix Web application you must register [`SessionMiddleware`]
|
||||
as a middleware on your `App`:
|
||||
|
||||
```no_run
|
||||
use actix_web::{web, App, HttpServer, HttpResponse, Error};
|
||||
use actix_session::{Session, SessionMiddleware, storage::RedisSessionStore};
|
||||
use actix_web::cookie::Key;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
// When using `Key::generate()` it is important to initialize outside of the
|
||||
// `HttpServer::new` closure. When deployed the secret key should be read from a
|
||||
// configuration file or environment variables.
|
||||
let secret_key = Key::generate();
|
||||
|
||||
let redis_store = RedisSessionStore::new("redis://127.0.0.1:6379")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
HttpServer::new(move ||
|
||||
App::new()
|
||||
// Add session management to your application using Redis for session state storage
|
||||
.wrap(
|
||||
SessionMiddleware::new(
|
||||
redis_store.clone(),
|
||||
secret_key.clone(),
|
||||
)
|
||||
)
|
||||
.default_service(web::to(|| HttpResponse::Ok())))
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
```
|
||||
|
||||
The session state can be accessed and modified by your request handlers using the [`Session`]
|
||||
extractor. Note that this doesn't work in the stream of a streaming response.
|
||||
|
||||
```no_run
|
||||
use actix_web::Error;
|
||||
use actix_session::Session;
|
||||
|
||||
fn index(session: Session) -> Result<&'static str, Error> {
|
||||
// access the session state
|
||||
if let Some(count) = session.get::<i32>("counter")? {
|
||||
println!("SESSION value: {}", count);
|
||||
// modify the session state
|
||||
session.insert("counter", count + 1)?;
|
||||
} else {
|
||||
session.insert("counter", 1)?;
|
||||
}
|
||||
|
||||
Ok("Welcome!")
|
||||
}
|
||||
```
|
||||
|
||||
# Choosing A Backend
|
||||
|
||||
By default, `actix-session` does not provide any storage backend to retrieve and save the state
|
||||
attached to your sessions. You can enable:
|
||||
|
||||
- a purely cookie-based "backend", [`CookieSessionStore`], using the `cookie-session` feature
|
||||
flag.
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
# ...
|
||||
actix-session = { version = "...", features = ["cookie-session"] }
|
||||
```
|
||||
|
||||
- a Redis-based backend via [`redis-rs`](https://docs.rs/redis-rs), [`RedisSessionStore`], using
|
||||
the `redis-session` feature flag.
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
# ...
|
||||
actix-session = { version = "...", features = ["redis-session"] }
|
||||
```
|
||||
|
||||
Add the `redis-session-native-tls` feature flag if you want to connect to Redis using a secured
|
||||
connection (via the `native-tls` crate):
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
# ...
|
||||
actix-session = { version = "...", features = ["redis-session-native-tls"] }
|
||||
```
|
||||
|
||||
If you instead prefer depending on `rustls`, use the `redis-session-rustls` feature flag:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
# ...
|
||||
actix-session = { version = "...", features = ["redis-session-rustls"] }
|
||||
```
|
||||
|
||||
You can implement your own session storage backend using the [`SessionStore`] trait.
|
||||
|
||||
[`SessionStore`]: storage::SessionStore
|
||||
[`CookieSessionStore`]: storage::CookieSessionStore
|
||||
[`RedisSessionStore`]: storage::RedisSessionStore
|
||||
[`RedisActorSessionStore`]: storage::RedisActorSessionStore
|
||||
*/
|
||||
</pre></div><pre class="rust"><code><span class="doccomment">//! Session management for Actix Web.
|
||||
//!
|
||||
//! The HTTP protocol, at a first glance, is stateless: the client sends a request, the server
|
||||
//! parses its content, performs some processing and returns a response. The outcome is only
|
||||
//! influenced by the provided inputs (i.e. the request content) and whatever state the server
|
||||
//! queries while performing its processing.
|
||||
//!
|
||||
//! Stateless systems are easier to reason about, but they are not quite as powerful as we need them
|
||||
//! to be - e.g. how do you authenticate a user? The user would be forced to authenticate **for
|
||||
//! every single request**. That is, for example, how 'Basic' Authentication works. While it may
|
||||
//! work for a machine user (i.e. an API client), it is impractical for a person—you do not want a
|
||||
//! login prompt on every single page you navigate to!
|
||||
//!
|
||||
//! There is a solution - **sessions**. Using sessions the server can attach state to a set of
|
||||
//! requests coming from the same client. They are built on top of cookies - the server sets a
|
||||
//! cookie in the HTTP response (`Set-Cookie` header), the client (e.g. the browser) will store the
|
||||
//! cookie and play it back to the server when sending new requests (using the `Cookie` header).
|
||||
//!
|
||||
//! We refer to the cookie used for sessions as a **session cookie**. Its content is called
|
||||
//! **session key** (or **session ID**), while the state attached to the session is referred to as
|
||||
//! **session state**.
|
||||
//!
|
||||
//! `actix-session` provides an easy-to-use framework to manage sessions in applications built on
|
||||
//! top of Actix Web. [`SessionMiddleware`] is the middleware underpinning the functionality
|
||||
//! provided by `actix-session`; it takes care of all the session cookie handling and instructs the
|
||||
//! **storage backend** to create/delete/update the session state based on the operations performed
|
||||
//! against the active [`Session`].
|
||||
//!
|
||||
//! `actix-session` provides some built-in storage backends: ([`CookieSessionStore`],
|
||||
//! [`RedisSessionStore`], and [`RedisActorSessionStore`]) - you can create a custom storage backend
|
||||
//! by implementing the [`SessionStore`] trait.
|
||||
//!
|
||||
//! Further reading on sessions:
|
||||
//! - [RFC 6265](https://datatracker.ietf.org/doc/html/rfc6265);
|
||||
//! - [OWASP's session management cheat-sheet](https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html).
|
||||
//!
|
||||
//! # Getting started
|
||||
//! To start using sessions in your Actix Web application you must register [`SessionMiddleware`]
|
||||
//! as a middleware on your `App`:
|
||||
//!
|
||||
//! ```no_run
|
||||
//! use actix_web::{web, App, HttpServer, HttpResponse, Error};
|
||||
//! use actix_session::{Session, SessionMiddleware, storage::RedisSessionStore};
|
||||
//! use actix_web::cookie::Key;
|
||||
//!
|
||||
//! #[actix_web::main]
|
||||
//! async fn main() -> std::io::Result<()> {
|
||||
//! // When using `Key::generate()` it is important to initialize outside of the
|
||||
//! // `HttpServer::new` closure. When deployed the secret key should be read from a
|
||||
//! // configuration file or environment variables.
|
||||
//! let secret_key = Key::generate();
|
||||
//!
|
||||
//! let redis_store = RedisSessionStore::new("redis://127.0.0.1:6379")
|
||||
//! .await
|
||||
//! .unwrap();
|
||||
//!
|
||||
//! HttpServer::new(move ||
|
||||
//! App::new()
|
||||
//! // Add session management to your application using Redis for session state storage
|
||||
//! .wrap(
|
||||
//! SessionMiddleware::new(
|
||||
//! redis_store.clone(),
|
||||
//! secret_key.clone(),
|
||||
//! )
|
||||
//! )
|
||||
//! .default_service(web::to(|| HttpResponse::Ok())))
|
||||
//! .bind(("127.0.0.1", 8080))?
|
||||
//! .run()
|
||||
//! .await
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! The session state can be accessed and modified by your request handlers using the [`Session`]
|
||||
//! extractor. Note that this doesn't work in the stream of a streaming response.
|
||||
//!
|
||||
//! ```no_run
|
||||
//! use actix_web::Error;
|
||||
//! use actix_session::Session;
|
||||
//!
|
||||
//! fn index(session: Session) -> Result<&'static str, Error> {
|
||||
//! // access the session state
|
||||
//! if let Some(count) = session.get::<i32>("counter")? {
|
||||
//! println!("SESSION value: {}", count);
|
||||
//! // modify the session state
|
||||
//! session.insert("counter", count + 1)?;
|
||||
//! } else {
|
||||
//! session.insert("counter", 1)?;
|
||||
//! }
|
||||
//!
|
||||
//! Ok("Welcome!")
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! # Choosing A Backend
|
||||
//!
|
||||
//! By default, `actix-session` does not provide any storage backend to retrieve and save the state
|
||||
//! attached to your sessions. You can enable:
|
||||
//!
|
||||
//! - a purely cookie-based "backend", [`CookieSessionStore`], using the `cookie-session` feature
|
||||
//! flag.
|
||||
//!
|
||||
//! ```toml
|
||||
//! [dependencies]
|
||||
//! # ...
|
||||
//! actix-session = { version = "...", features = ["cookie-session"] }
|
||||
//! ```
|
||||
//!
|
||||
//! - a Redis-based backend via [`redis-rs`](https://docs.rs/redis-rs), [`RedisSessionStore`], using
|
||||
//! the `redis-session` feature flag.
|
||||
//!
|
||||
//! ```toml
|
||||
//! [dependencies]
|
||||
//! # ...
|
||||
//! actix-session = { version = "...", features = ["redis-session"] }
|
||||
//! ```
|
||||
//!
|
||||
//! Add the `redis-session-native-tls` feature flag if you want to connect to Redis using a secure
|
||||
//! connection (via the `native-tls` crate):
|
||||
//!
|
||||
//! ```toml
|
||||
//! [dependencies]
|
||||
//! # ...
|
||||
//! actix-session = { version = "...", features = ["redis-session-native-tls"] }
|
||||
//! ```
|
||||
//!
|
||||
//! If you, instead, prefer depending on `rustls`, use the `redis-session-rustls` feature flag:
|
||||
//!
|
||||
//! ```toml
|
||||
//! [dependencies]
|
||||
//! # ...
|
||||
//! actix-session = { version = "...", features = ["redis-session-rustls"] }
|
||||
//! ```
|
||||
//!
|
||||
//! You can implement your own session storage backend using the [`SessionStore`] trait.
|
||||
//!
|
||||
//! [`SessionStore`]: storage::SessionStore
|
||||
//! [`CookieSessionStore`]: storage::CookieSessionStore
|
||||
//! [`RedisSessionStore`]: storage::RedisSessionStore
|
||||
//! [`RedisActorSessionStore`]: storage::RedisActorSessionStore
|
||||
|
||||
</span><span class="attr">#![forbid(unsafe_code)]
|
||||
#![deny(rust_2018_idioms, nonstandard_style)]
|
||||
|
@ -1,5 +1,5 @@
|
||||
(function() {var implementors = {
|
||||
"actix_identity":[["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><<a class=\"struct\" href=\"actix_identity/error/struct.LostIdentityError.html\" title=\"struct actix_identity::error::LostIdentityError\">LostIdentityError</a>> for <a class=\"enum\" href=\"actix_identity/error/enum.GetIdentityError.html\" title=\"enum actix_identity::error::GetIdentityError\">GetIdentityError</a>"],["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><<a class=\"struct\" href=\"actix_identity/error/struct.MissingIdentityError.html\" title=\"struct actix_identity::error::MissingIdentityError\">MissingIdentityError</a>> for <a class=\"enum\" href=\"actix_identity/error/enum.GetIdentityError.html\" title=\"enum actix_identity::error::GetIdentityError\">GetIdentityError</a>"],["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><<a class=\"struct\" href=\"actix_identity/error/struct.SessionExpiryError.html\" title=\"struct actix_identity::error::SessionExpiryError\">SessionExpiryError</a>> for <a class=\"enum\" href=\"actix_identity/error/enum.GetIdentityError.html\" title=\"enum actix_identity::error::GetIdentityError\">GetIdentityError</a>"],["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><SessionGetError> for <a class=\"enum\" href=\"actix_identity/error/enum.GetIdentityError.html\" title=\"enum actix_identity::error::GetIdentityError\">GetIdentityError</a>"],["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><SessionInsertError> for <a class=\"struct\" href=\"actix_identity/error/struct.LoginError.html\" title=\"struct actix_identity::error::LoginError\">LoginError</a>"]],
|
||||
"actix_identity":[["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><<a class=\"struct\" href=\"actix_identity/error/struct.LostIdentityError.html\" title=\"struct actix_identity::error::LostIdentityError\">LostIdentityError</a>> for <a class=\"enum\" href=\"actix_identity/error/enum.GetIdentityError.html\" title=\"enum actix_identity::error::GetIdentityError\">GetIdentityError</a>"],["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><<a class=\"struct\" href=\"actix_identity/error/struct.MissingIdentityError.html\" title=\"struct actix_identity::error::MissingIdentityError\">MissingIdentityError</a>> for <a class=\"enum\" href=\"actix_identity/error/enum.GetIdentityError.html\" title=\"enum actix_identity::error::GetIdentityError\">GetIdentityError</a>"],["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><<a class=\"struct\" href=\"actix_identity/error/struct.SessionExpiryError.html\" title=\"struct actix_identity::error::SessionExpiryError\">SessionExpiryError</a>> for <a class=\"enum\" href=\"actix_identity/error/enum.GetIdentityError.html\" title=\"enum actix_identity::error::GetIdentityError\">GetIdentityError</a>"],["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><<a class=\"struct\" href=\"actix_session/session/struct.SessionGetError.html\" title=\"struct actix_session::session::SessionGetError\">SessionGetError</a>> for <a class=\"enum\" href=\"actix_identity/error/enum.GetIdentityError.html\" title=\"enum actix_identity::error::GetIdentityError\">GetIdentityError</a>"],["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><<a class=\"struct\" href=\"actix_session/session/struct.SessionInsertError.html\" title=\"struct actix_session::session::SessionInsertError\">SessionInsertError</a>> for <a class=\"struct\" href=\"actix_identity/error/struct.LoginError.html\" title=\"struct actix_identity::error::LoginError\">LoginError</a>"]],
|
||||
"actix_limitation":[["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><ComponentRange> for <a class=\"enum\" href=\"actix_limitation/enum.Error.html\" title=\"enum actix_limitation::Error\">Error</a>"],["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><RedisError> for <a class=\"enum\" href=\"actix_limitation/enum.Error.html\" title=\"enum actix_limitation::Error\">Error</a>"]],
|
||||
"actix_protobuf":[["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><<a class=\"struct\" href=\"https://docs.rs/prost/0.12.6/prost/error/struct.DecodeError.html\" title=\"struct prost::error::DecodeError\">DecodeError</a>> for <a class=\"enum\" href=\"actix_protobuf/enum.ProtoBufPayloadError.html\" title=\"enum actix_protobuf::ProtoBufPayloadError\">ProtoBufPayloadError</a>"],["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><PayloadError> for <a class=\"enum\" href=\"actix_protobuf/enum.ProtoBufPayloadError.html\" title=\"enum actix_protobuf::ProtoBufPayloadError\">ProtoBufPayloadError</a>"]],
|
||||
"actix_session":[["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><<a class=\"struct\" href=\"actix_session/config/struct.BrowserSession.html\" title=\"struct actix_session::config::BrowserSession\">BrowserSession</a>> for <a class=\"enum\" href=\"actix_session/config/enum.SessionLifecycle.html\" title=\"enum actix_session::config::SessionLifecycle\">SessionLifecycle</a>"],["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><<a class=\"struct\" href=\"actix_session/config/struct.PersistentSession.html\" title=\"struct actix_session::config::PersistentSession\">PersistentSession</a>> for <a class=\"enum\" href=\"actix_session/config/enum.SessionLifecycle.html\" title=\"enum actix_session::config::SessionLifecycle\">SessionLifecycle</a>"],["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><<a class=\"struct\" href=\"actix_session/storage/struct.SessionKey.html\" title=\"struct actix_session::storage::SessionKey\">SessionKey</a>> for <a class=\"struct\" href=\"https://doc.rust-lang.org/nightly/alloc/string/struct.String.html\" title=\"struct alloc::string::String\">String</a>"],["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><<a class=\"struct\" href=\"https://docs.rs/anyhow/1.0.85/anyhow/struct.Error.html\" title=\"struct anyhow::Error\">Error</a>> for <a class=\"struct\" href=\"actix_session/struct.SessionGetError.html\" title=\"struct actix_session::SessionGetError\">SessionGetError</a>"],["impl <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/convert/trait.From.html\" title=\"trait core::convert::From\">From</a><<a class=\"struct\" href=\"https://docs.rs/anyhow/1.0.85/anyhow/struct.Error.html\" title=\"struct anyhow::Error\">Error</a>> for <a class=\"struct\" href=\"actix_session/struct.SessionInsertError.html\" title=\"struct actix_session::SessionInsertError\">SessionInsertError</a>"]],
|
||||
|
Loading…
Reference in New Issue
Block a user