Most backend developers have written some version of this flow: a user logs in, the server calls jwt.sign() with a secret, and protected routes call jwt.verify() before doing any real work. Add a short expiration and it feels nicely contained.
That familiar setup is usually enough for a monolith. The login route and protected routes run inside the same application, use the same environment file, and sit inside the same deployment boundary. One JWT_SECRET is used to create tokens and check them later. There is no extra trust decision to make because it is all one application.
Now keep the JWT flow exactly the same, but split the application into services. Authentication Service handles login. Order Service and Payment Service run somewhere else—perhaps on separate servers, in different Kubernetes clusters, or just as independently deployed applications. A client sends its access token to Order Service, and Order Service still needs a way to answer the same familiar question: “Was this token really issued by our login system?”
The first solution most of us reach for is to copy JWT_SECRET into the environment of every service that needs to verify tokens. It works: Order Service can call jwt.verify(), Payment Service can call jwt.verify(), and requests continue to flow.
The subtle change is that the secret has two jobs. It lets a service verify a token, but it also lets that service create one. Once the same secret is distributed, Order Service and Payment Service are no longer only token consumers; each one has the cryptographic authority to issue a token that every other service will trust.
That is the problem asymmetric JWTs address. They do not replace the normal JWT flow with something completely different. They separate the ability to issue a token from the ability to verify one.
Start With the Cryptography Model
Before JWTs, it helps to separate three ideas that are often bundled together. JWTs are commonly signed. A signature lets a service answer two questions:
- Was this token created by someone I trust?
- Has it changed since it was created?
It does not hide the token payload. We will come back to that distinction later.
Symmetric Cryptography: Alice and Bob Share One Secret
Alice and Bob agree on one secret before they start talking. If Alice encrypts something with that shared secret, Bob can decrypt it with the same secret. If Alice calculates an HMAC over a message with it, Bob can calculate the same HMAC and check whether the message was changed.
The important property is simple: anyone who has the shared secret has the same cryptographic power. Bob can check something Alice produced, but he can also produce something that looks like it came from Alice.
For a symmetric JWT, that shared secret is used with HS256, an HMAC construction using SHA-256. It does not encrypt the JWT; it creates a message-authentication code over the header and payload.
That is wonderfully simple when one application owns both jobs. It becomes more sensitive when the secret has to be copied to several systems.
Asymmetric Cryptography: Alice Signs, Bob Verifies
Now give Alice a key pair instead:
- A private key, which must stay with the issuer.
- A public key, which can be shared with services that need to verify signatures.
Alice keeps the private key. She gives Bob the public key. When Alice signs a message with the private key, Bob can use the public key to verify it. But the public key is not a shortcut to the private key, so Bob cannot create a new valid signature.
That is the same idea as a notary’s seal: anyone can inspect a real seal, but inspecting it does not let you create another one. RS256 applies this model to JWTs using RSA signatures with SHA-256.
Encryption and Signatures Are Different
Here is the distinction in plain terms:
- Encryption hides data. Alice encrypts for Bob, and someone without the decryption key cannot read it.
- An HMAC proves that someone with the shared secret created or approved a message. It is symmetric: every verifier also has signing power.
- A digital signature proves that the private-key holder signed a message. Verifiers can use the public key without gaining signing power.
A normal signed JWT is a JWS (JSON Web Signature), not an encrypted container. Its header and payload are Base64URL encoded, which makes them easy to read. Never put passwords, API keys, or sensitive personal data in a standard JWT payload.
Symmetric JWTs: The Familiar HS256 Flow
With HS256, your application has one strong random secret. It uses that secret to create a signature over the JWT header and payload. When a request arrives, it calculates the expected signature with the same secret and compares it with the token’s signature.
Install the package first:
npm install jsonwebtoken
Then generate the secret instead of inventing one by hand:
openssl rand -base64 48
Store that value in your secret manager or local environment configuration—not in the repository. Here is a realistic Node.js example using jsonwebtoken:
import jwt from 'jsonwebtoken';
const JWT_SECRET = process.env.JWT_SECRET;
if (!JWT_SECRET) {
throw new Error('JWT_SECRET is required');
}
const token = jwt.sign(
{
sub: 'user_123',
role: 'member',
},
JWT_SECRET,
{
algorithm: 'HS256',
expiresIn: '15m',
issuer: 'https://auth.example.com',
audience: 'https://api.example.com',
},
);
console.log(token);
The sub claim identifies the user. The expiration limits how long a stolen token is useful. issuer and audience make the token less reusable in the wrong place: a token issued by another system, or intended for another API, should not be accepted just because its signature happens to be valid.
Verification should validate those claims too:
function verifyAccessToken(token) {
return jwt.verify(token, JWT_SECRET, {
algorithms: ['HS256'],
issuer: 'https://auth.example.com',
audience: 'https://api.example.com',
});
}
Passing algorithms is deliberate. Do not let an untrusted token header choose what verification rules your server will accept. jsonwebtoken checks exp by default when it is present; do not use ignoreExpiration: true for normal request authentication.
Why This Works Well
For a monolith, a backend-for-frontend, or a small application where one trusted deployment owns authentication, symmetric signing is a great fit:
- It is straightforward to implement and operate.
- HMAC verification is fast.
- There is one secret to manage.
The problem begins when several independent services need to verify the same token. Every verifier now needs a copy of JWT_SECRET.
That means every verifier can also sign a new token with it. This is not theoretical. If a low-privilege service leaks its environment variables, an attacker can use the same secret to create a token that looks legitimate to every other service:
// Imagine this runs after a service's JWT_SECRET has been exposed.
const forgedToken = jwt.sign(
{ sub: 'attacker_123', role: 'admin' },
leakedSharedSecret,
{
algorithm: 'HS256',
expiresIn: '15m',
issuer: 'https://auth.example.com',
audience: 'https://api.example.com',
},
);
The token passes verification because, cryptographically, the compromised service and the Authentication Service are peers: both have the signing secret.
That may be acceptable inside one tightly controlled application. It is usually not the trust boundary you want across a growing service architecture.
Asymmetric JWTs: Separate Issuing From Verifying
Asymmetric JWTs solve that distribution problem. Only the authentication service receives the private key. The other services receive the public key.
Generate a key pair with OpenSSL. A 2048-bit RSA key is the minimum commonly accepted by JWT libraries for RS256; 3072 or 4096 bits are reasonable choices when the extra verification cost is acceptable. This example uses 3072 bits:
# Keep this private and out of source control.
openssl genrsa -out jwt-private.pem 3072
# This file can be distributed to token-verifying services.
openssl rsa -in jwt-private.pem -pubout -out jwt-public.pem
In production, private keys should normally live in a managed secret store or a key-management system, not as loose files copied through deployments.
The authentication service signs with the private key:
import fs from 'node:fs';
import jwt from 'jsonwebtoken';
const privateKey = fs.readFileSync('./jwt-private.pem', 'utf8');
const token = jwt.sign(
{
sub: 'user_123',
role: 'member',
},
privateKey,
{
algorithm: 'RS256',
expiresIn: '15m',
issuer: 'https://auth.example.com',
audience: 'https://orders.example.com',
keyid: 'auth-key-2026-01',
},
);
The order service only needs the public key:
import fs from 'node:fs';
import jwt from 'jsonwebtoken';
const publicKey = fs.readFileSync('./jwt-public.pem', 'utf8');
function verifyOrderToken(token) {
return jwt.verify(token, publicKey, {
algorithms: ['RS256'],
issuer: 'https://auth.example.com',
audience: 'https://orders.example.com',
});
}
The public key can confirm that the token was signed by its matching private key. It cannot produce a valid new signature. Passing it to jwt.sign(..., { algorithm: 'RS256' }) fails because RS256 requires a private RSA key; using it as an HMAC secret would also be wrong and must not be allowed by the verifier.
This is the architectural win: verification can be widely distributed without distributing token-issuing authority. A compromised Order Service may still expose its own data or accept a stolen token, but its public key does not let an attacker mint a token that Payment Service will accept.
The Same Microservices, Two Different Trust Models
Suppose an application has four services:
- Authentication Service
- User Service
- Order Service
- Payment Service
With a Shared Secret
The authentication service creates an HS256 token. To validate requests, the user, order, and payment services each receive the same secret.
shared JWT_SECRET
│
┌────────────────────────────┼────────────────────────────┐
│ │ │
Auth Service Order Service Payment Service
sign + verify verify + can sign verify + can sign
The lifecycle is simple:
- A user signs in to the Authentication Service.
- It signs an access token with the shared secret.
- The client sends that token to the Order Service.
- The Order Service verifies it using the shared secret.
It works. But each service now has signing power. If the Payment Service is compromised and its environment variables are exposed, the attacker may be able to issue tokens that every other service accepts.
The diagram is the risk in one line: every box with the shared secret can sign. That may be fine when all boxes are one application under one boundary. It is a different decision once services are independently deployed or owned.
With a Key Pair
With RS256, the Authentication Service holds the private key. User, Order, and Payment receive only the public key.
private key public key
│ │
Auth Service ── signed JWT ──► Client ──► Order Service
sign + verify verify only
└─► Payment Service
verify only
The request lifecycle looks similar from the client’s perspective:
- A user signs in to the Authentication Service.
- It signs an access token with its private key.
- The client sends that token to the Order Service.
- The Order Service verifies it using the public key.
The important difference is at step four: the Order Service can trust the token without being trusted to issue one. If that service is compromised, an attacker can still steal or misuse data that service can access, but they cannot create a new valid token for the rest of the platform from the public key alone.
Public Key Distribution, JWKS, and Rotation
Copying a public key into every service works for a small internal system. At larger scale, identity providers commonly publish a JWKS endpoint (JSON Web Key Set), such as:
https://auth.example.com/.well-known/jwks.json
The endpoint exposes active public keys, usually with a key ID (kid). A JWT includes the matching key ID in its header. A verifier selects that public key, caches it according to the endpoint’s cache policy, and verifies the signature. In practice, cache the JWKS rather than fetching it for every request; on an unknown kid, refresh once with rate limiting before rejecting the token. That keeps rotation responsive without turning an attacker-controlled header into a request storm.
This also makes rotation practical:
- Publish a new public key alongside the existing one.
- Begin signing new tokens with the new private key and
kid. - Continue serving the old public key until all tokens signed with it have expired, including your allowed clock-skew window.
- Remove the old key.
Rotation is still operational work, but it no longer requires placing a new signing secret in every service at exactly the same time.
When Should You Use Each?
Neither approach wins everywhere. Choose based on the trust boundary, not because one algorithm sounds more advanced.
| Question | Symmetric (HS256) | Asymmetric (RS256) |
|---|---|---|
| Who can create tokens? | Every system with the secret | Only the private-key holder |
| Who can verify tokens? | Every system with the secret | Any system with the public key |
| Key management | Simple at first; harder as verifiers grow | More setup; safer distribution for verifiers |
| Verification cost | Very low | Higher, but usually insignificant for API traffic |
| Best fit | Monoliths and tightly controlled services | Microservices, multiple trust zones, external consumers |
| Third-party identity providers | Less common | Very common through JWKS / OIDC |
Use HS256 when one application owns issuance and verification, and you can keep the secret inside that trust boundary. There is no prize for adding RSA keys to a small application that does not need them.
Use RS256 when verification is distributed, when different teams operate services independently, or when you want an identity provider to issue tokens without giving every consumer the ability to mint them. It is also the usual choice when integrating with OAuth or OpenID Connect providers that expose a JWKS endpoint.
Security Checks That Matter Either Way
The signing algorithm is only part of JWT security.
A signed JWT is not encrypted. Anyone who has it can decode its payload. Treat it as public metadata with integrity protection.
Expiration works with both models. exp is a claim, not a feature unique to symmetric or asymmetric signing. A verifier must reject an expired token; allow only a small, explicit clock tolerance if your distributed systems need it. Keep access tokens short-lived.
Asymmetric signatures do not stop token theft. If an attacker steals a valid access token, it is valid until it expires or is otherwise rejected. Use HTTPS, avoid leaking tokens into URLs and logs, and choose storage patterns carefully.
Validate more than the signature. Restrict accepted algorithms, validate iss, validate aud, check expiration, and use the right public key for the token’s kid. Audience is especially important in a multi-service system: the Order Service should not accept a token meant only for the Payment Service. Do not trust a token only because its cryptographic signature verifies.
Protect signing material. A leaked symmetric secret lets an attacker sign tokens. A leaked asymmetric private key does the same. Public keys are intentionally shareable; private keys are not.
The Practical Takeaway
Start with the question: which systems should be allowed to issue identity?
If the answer is “this one application,” a symmetric JWT is simple, fast, and completely reasonable. If the answer is “only the authentication service, while several other services need to verify,” asymmetric JWTs give you a cleaner trust boundary.
The algorithm is not the architecture. The distribution of signing authority is. Once you see that distinction, choosing between HS256 and RS256 becomes much less mysterious.