#jwt #axum-middleware #axum

axum-jwt

Axum JWT extractors and middleware

4 releases

Uses new Rust 2024

0.1.3 Sep 24, 2025
0.1.2 Aug 13, 2025
0.1.1 Aug 9, 2025
0.1.0 Aug 1, 2025

#878 in HTTP server

Download history 111/week @ 2025-07-28 106/week @ 2025-08-04 147/week @ 2025-08-11 23/week @ 2025-08-18 5/week @ 2025-08-25 15/week @ 2025-09-08 94/week @ 2025-09-15 334/week @ 2025-09-22 75/week @ 2025-09-29 136/week @ 2025-10-06 18/week @ 2025-10-13 205/week @ 2025-10-20 115/week @ 2025-10-27

479 downloads per month

MIT license

35KB
509 lines

axum-jwt

JSON Web Token extractors and middleware for axum framework

About

The library provides extractors for performing JWT authentication. Under the hood, tokens are parsed using the jsonwebtoken crate. For more details, see the documentation.

Example

In this example, the request token is validated and the username is extracted:

use {
    axum::{Router, routing},
    axum_jwt::{Claims, Decoder, jsonwebtoken::DecodingKey},
    serde::Deserialize,
    std::io::Error,
    tokio::net::TcpListener,
};

#[derive(Deserialize)]
struct User {
    sub: String,
}

async fn hello(Claims(u): Claims<User>) -> String {
    format!("Hello, {}!", u.sub)
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let decoder = Decoder::from_key(DecodingKey::from_secret(b"secret"));

    let app = Router::new()
        .route("/", routing::get(hello))
        .with_state(decoder);

    let listener = TcpListener::bind("0.0.0.0:3000").await?;
    axum::serve(listener, app).await
}

In case of failed authentication, for example if the token is invalid or expired, a 401 Unauthorized status code is returned.

Dependencies

~2–13MB
~136K SLoC