21 个版本 (7 个稳定版)
1.2.4 | 2024 年 8 月 5 日 |
---|---|
1.2.3 | 2024 年 6 月 1 日 |
1.2.2 | 2024 年 5 月 25 日 |
1.2.0 | 2023 年 12 月 25 日 |
0.3.1 | 2021 年 11 月 24 日 |
#84 在 HTTP 服务器
每月 228 次下载
43KB
824 行
与 Actix 4 兼容的 JWT 认证
为了使用此 crate,您可以将它添加到您的 Cargo.toml 文件中
此 crate 是使用 actix-4 构建的。
actix-4-jwt-auth = "1.2.0"
或者当您想使用 GitHub 上找到的最新版本时
actix-4-jwt-auth = {git = "https://github.com/spectare/actix-4-jwt-auth", branch = "main"}
与提取器兼容
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct FoundClaims {
pub iss: String,
pub sub: String,
pub aud: String,
pub name: String,
pub email: Option<String>,
pub email_verified: Option<bool>,
}
#[get("/authenticated_user")]
async fn authenticated_user(user: AuthenticatedUser<FoundClaims>) -> String {
format!("Welcome {}!", user.claims.name)
}
是 Actix 端点 URL,它从基于 JWT 的 Authorization Bearer 标头中提取 AuthenticatedUser。
您可以将应用程序连接如下
let authority = "https://a.valid.openid-connect.idp/".to_string();
let oidc = Oidc::new(OidcConfig::Issuer(authority.clone().into())).await.unwrap();
let biscuit_validator = OidcBiscuitValidator { options: ValidationOptions {
issuer: Validation::Validate(authority),
..ValidationOptions::default()
}
};
HttpServer::new(move || {
App::new()
.app_data(oidc.clone())
.wrap(biscuit_validator.clone())
// .wrap(OidcBiscuitValidator::default()) //without issuer verification
.service(authenticated_user),
})
.bind("0.0.0.0:8080".to_string())?
.run()
.await
这将从如果您使用 Oidc::new
时,Authorization
标头的值中找到令牌
您可以通过导入 TokenLookup
枚举来覆盖令牌查找位置(自定义标头或 cookie)
use actix_4_jwt_auth::{Oidc, OidcConfig, TokenLookup};
如果您想使用自定义标头
let token_lookup = TokenLookup::Header("x-custom-auth-header".into());
或使用自定义 cookie
let token_lookup = TokenLookup::Cookie("x-custom-auth-cookie".into());
并将 token_lookup
作为 Oidc::new_with_token_lookup
的第二个参数传递
let oidc = Oidc::new_with_token_lookup(OidcConfig::Issuer(authority.clone().into()), token_lookup).await.unwrap();
更多文档可以在 docs.rs 上找到
依赖项
~23–35MB
~720K SLoC