如何使用 Axum 上的 jwt-authorizer Rust 箱提取名为“client_id”的自定义声明?

问题描述 投票:0回答:1

这是我在 axum 项目的 Cargo.toml 上使用的板条箱: jwt-authorizer =“0.14.0”

这是我的方法签名:

pub async fn find_balances(
    Path(account_string): Path<String>,
    query_params: Query<QueryParams>,
    header_map: HeaderMap,
    JwtClaims(user): JwtClaims<RegisteredClaims>,
    State(client): State<Client>,
) -> Result<Json<SaldoDTO>, Error> {

我想访问“client_id”自定义声明。

此声明无法从

JwtClaims(user): JwtClaims<RegisteredClaims>

检索

我怎样才能得到它?

rust jwt rust-axum axum
1个回答
0
投票

您只需要创建自己的派生类型

serde::Deserialize
。您可以使用
RegisteredClaims
#[serde(flatten)]
完成大部分繁重的工作。

#[derive(serde::Deserialize)]
struct Claims {
    #[serde(flatten)]
    pub standard: RegisteredClaims,
    pub client_id: ClientId,
}

由于您没有指定

client_id
字段的类型,因此我在这里使用了占位符
ClientId
。将其替换为字段的实际类型。

© www.soinside.com 2019 - 2024. All rights reserved.