JWT Authentication
JWT (JSON Web Token) is an open standard (RFC 7519) for securely transmitting information between parties. It is a compact, URL-safe method of representing claims in JSON object format, commonly used for authentication and information exchange.
A JWT consists of three parts:
- Header - Specifies the token type and the signing algorithm used
- Payload - Contains claims, such as user ID, roles, expiration time, etc.
- Signature - Used to verify that the message was not altered during transmission
Typical usage of JWT in authentication flow:
- After user login, the server generates a JWT token
- The token is returned to the client and stored (typically in localStorage or cookies)
- In subsequent requests, the client includes this token in the Authorization header
- The server validates the token's authenticity and grants access
Provides middleware for JWT Auth authentication.
Choosing a crypto provider
Salvo 0.95 provides two alternative JWT features:
jwt-authuses the AWS-LC provider.jwt-auth-ringuses the RustCrypto provider.
Enable only one in normal builds. The separate ring feature selects ring for
Salvo's rustls integration; it does not enable JWT authentication. The example
below uses jwt-auth. To use RustCrypto instead:
If Cargo feature unification enables both JWT providers, call
salvo::jwt_auth::install_crypto_provider() at the very start of main, before
any direct jsonwebtoken operation or JWT middleware setup. The example below
does this so provider selection remains deterministic.
Example Code