2014-09-19 15:00:25 -04:00
[![Build Status ](https://travis-ci.org/jwtk/jjwt.svg?branch=master )](https://travis-ci.org/jwtk/jjwt)
2016-07-04 15:16:16 -04:00
[![Coverage Status ](https://coveralls.io/repos/github/jwtk/jjwt/badge.svg?branch=master )](https://coveralls.io/github/jwtk/jjwt?branch=master)
2019-07-11 19:19:50 -04:00
[![Gitter ](https://badges.gitter.im/jwtk/jjwt.svg )](https://gitter.im/jwtk/jjwt?utm_source=badge& utm_medium=badge& utm_campaign=pr-badge)
2014-09-12 21:06:24 -04:00
2016-06-17 18:11:08 -04:00
## Java JWT: JSON Web Token for Java and Android
2014-09-18 22:14:22 -04:00
2018-07-30 19:07:17 -04:00
JJWT aims to be the easiest to use and understand library for creating and verifying JSON Web Tokens (JWTs) on the JVM
and Android.
2014-09-19 23:22:49 -04:00
2021-05-30 22:32:47 -04:00
JJWT is a pure Java implementation based
exclusively on the [JWT ](https://tools.ietf.org/html/rfc7519 ),
[JWS ](https://tools.ietf.org/html/rfc7515 ), [JWE ](https://tools.ietf.org/html/rfc7516 ),
[JWK ](https://tools.ietf.org/html/rfc7517 ) and [JWA ](https://tools.ietf.org/html/rfc7518 ) RFC specifications and
2018-07-30 19:07:17 -04:00
open source under the terms of the [Apache 2.0 License ](http://www.apache.org/licenses/LICENSE-2.0 ).
2016-06-17 18:11:08 -04:00
2021-06-23 12:24:29 -04:00
The library was created by [Les Hazlewood ](https://github.com/lhazlewood )
2018-07-30 19:07:17 -04:00
and is supported and maintained by a [community ](https://github.com/jwtk/jjwt/graphs/contributors ) of contributors.
2016-06-17 18:11:08 -04:00
2021-05-30 22:32:47 -04:00
We've also added some convenience extensions that are not part of the specification, such as JWS compression and claim
2018-07-30 19:07:17 -04:00
enforcement.
## Table of Contents
2021-05-30 22:32:47 -04:00
* [Features ](#features )
* [Currently Unsupported Features ](#features-unsupported )
* [Community ](#community )
* [Getting Help ](#help )
* [Questions ](#help-questions )
* [Bugs and Feature Requests ](#help-issues )
* [Contributing ](#contributing )
* [Pull Requests ](#contributing-pull-requests )
* [Help Wanted ](#contributing-help-wanted )
* [What is a JSON Web Token? ](#overview )
* [Installation ](#install )
* [JDK Projects ](#install-jdk )
* [Maven ](#install-jdk-maven )
* [Gradle ](#install-jdk-gradle )
* [Android Projects ](#install-android )
* [Dependencies ](#install-android-dependencies )
* [Proguard Exclusions ](#install-android-proguard )
* [Understanding JJWT Dependencies ](#install-understandingdependencies )
* [Quickstart ](#quickstart )
* [Signed JWTs ](#jws )
* [Signature Algorithm Keys ](#jws-key )
* [HMAC-SHA ](#jws-key-hmacsha )
* [RSA ](#jws-key-rsa )
* [Elliptic Curve ](#jws-key-ecdsa )
* [Creating Safe Keys ](#jws-key-create )
* [Secret Keys ](#jws-key-create-secret )
* [Asymetric Keys ](#jws-key-create-asym )
* [Create a JWS ](#jws-create )
* [Header ](#jws-create-header )
* [Instance ](#jws-create-header-instance )
* [Map ](#jws-create-header-map )
* [Claims ](#jws-create-claims )
* [Standard Claims ](#jws-create-claims-standard )
* [Custom Claims ](#jws-create-claims-custom )
* [Claims Instance ](#jws-create-claims-instance )
* [Claims Map ](#jws-create-claims-map )
* [Signing Key ](#jws-create-key )
* [SecretKey Formats ](#jws-create-key-secret )
* [Signature Algorithm Override ](#jws-create-key-algoverride )
* [Compression ](#jws-create-compression )
* [Read a JWS ](#jws-read )
* [Verification Key ](#jws-read-key )
* [Find the Verification Key at Runtime ](#jws-read-key-resolver )
* [Claims Assertions ](#jws-read-claims )
* [Accounting for Clock Skew ](#jws-read-clock )
* [Custom Clock ](#jws-read-clock-custom )
* [Decompression ](#jws-read-decompression )
2018-07-30 19:07:17 -04:00
<!-- * [Error Handling ](#jws-read-errors ) -->
2021-05-30 22:32:47 -04:00
* [Compression ](#compression )
* [Custom Compression Codec ](#compression-custom )
* [JSON Processor ](#json )
* [Custom JSON Processor ](#json-custom )
* [Jackson ObjectMapper ](#json-jackson )
* [Custom Claim Types ](#json-jackson-custom-types )
* [Gson ](#json-gson )
* [Base64 Support ](#base64 )
* [Base64 in Security Contexts ](#base64-security )
* [Base64 is not Encryption ](#base64-not-encryption )
* [Changing Base64 Characters ](#base64-changing-characters )
* [Custom Base64 Codec ](#base64-custom )
2018-07-30 19:07:17 -04:00
< a name = "features" > < / a >
## Features
2021-05-30 22:32:47 -04:00
* Fully functional on all JDKs and Android
* Automatic security best practices and assertions
* Easy to learn and read API
* Convenient and readable [fluent ](http://en.wikipedia.org/wiki/Fluent_interface ) interfaces, great for IDE auto-completion to write code quickly
* Fully RFC specification compliant on all implemented functionality, tested against RFC-specified test vectors
* Stable implementation with enforced 100% test code coverage. Literally every single method, statement and
conditional branch variant in the entire codebase is tested and required to pass on every build.
* Creating, parsing and verifying digitally signed compact JWTs (aka JWSs) with all standard JWS algorithms:
* HS256: HMAC using SHA-256
* HS384: HMAC using SHA-384
* HS512: HMAC using SHA-512
* ES256: ECDSA using P-256 and SHA-256
* ES384: ECDSA using P-384 and SHA-384
* ES512: ECDSA using P-521 and SHA-512
* RS256: RSASSA-PKCS-v1_5 using SHA-256
* RS384: RSASSA-PKCS-v1_5 using SHA-384
* RS512: RSASSA-PKCS-v1_5 using SHA-512
* PS256: RSASSA-PSS using SHA-256 and MGF1 with SHA-256< sup > 1</ sup >
* PS384: RSASSA-PSS using SHA-384 and MGF1 with SHA-384< sup > 1</ sup >
* PS512: RSASSA-PSS using SHA-512 and MGF1 with SHA-512< sup > 1</ sup >
< sup > 1. Requires JDK 11 or a compatible JCA Provider (like BouncyCastle) in the runtime classpath.< / sup >
* Convenience enhancements beyond the specification such as
* Body compression for any large JWT, not just JWEs
* Claims assertions (requiring specific values)
* Claim POJO marshaling and unmarshaling when using a compatible JSON parser (e.g. Jackson)
* Secure Key generation based on desired JWA algorithms
* and more...
2018-07-30 19:07:17 -04:00
< a name = "features-unsupported" > < / a >
### Currently Unsupported Features
2021-05-30 22:32:47 -04:00
* [Non-compact ](https://tools.ietf.org/html/rfc7515#section-7.2 ) serialization and parsing.
* JWE (Encryption for JWT)
2014-09-25 16:49:59 -04:00
2021-05-30 22:32:47 -04:00
These features will be implemented in a future release. Community contributions are welcome!
2016-07-02 17:09:22 -04:00
2019-07-11 19:19:50 -04:00
< a name = "community" > < / a >
## Community
< a name = "help" > < / a >
### Getting Help
2021-05-30 22:32:47 -04:00
If you have trouble using JJWT, please first read the documentation on this page before asking questions. We try
2019-07-11 19:19:50 -04:00
very hard to ensure JJWT's documentation is robust, categorized with a table of contents, and up to date for each release.
< a name = "help-questions" > < / a >
#### Questions
If the documentation or the API JavaDoc isn't sufficient, and you either have usability questions or are confused
about something, please [ask your question here ](https://stackoverflow.com/questions/ask?tags=jjwt&guided=false ).
After asking your question, you may wish to join our [Slack ](https://jwtk.slack.com/messages/CBNACTN3A ) or
[Gittr ](https://gitter.im/jwtk/jjwt ) chat rooms, but note that they may not always be attended. You will usually
2021-05-30 22:32:47 -04:00
have a better chance of having your question answered by
2019-07-11 19:19:50 -04:00
[asking your question here ](https://stackoverflow.com/questions/ask?tags=jjwt&guided=false ).
2021-05-30 22:32:47 -04:00
If you believe you have found a bug or would like to suggest a feature enhancement, please create a new GitHub issue,
2019-07-11 19:19:50 -04:00
however:
2021-05-30 22:32:47 -04:00
**Please do not create a GitHub issue to ask a question.**
2019-07-11 19:19:50 -04:00
2021-05-30 22:32:47 -04:00
We use GitHub Issues to track actionable work that requires changes to JJWT's design and/or codebase. If you have a
usability question, instead please
[ask your question here ](https://stackoverflow.com/questions/ask?tags=jjwt&guided=false ), or try Slack or Gittr as
2019-07-11 19:19:50 -04:00
described above.
**If a GitHub Issue is created that does not represent actionable work for JJWT's codebase, it will be promptly closed.**
< a name = "help-issues" > < / a >
#### Bugs and Feature Requests
2021-05-30 22:32:47 -04:00
If you do not have a usability question and believe you have a legitimate bug or feature request,
2019-07-11 19:19:50 -04:00
please do [create a new JJWT issue ](https://github.com/jwtk/jjwt/issues/new ).
2021-05-30 22:32:47 -04:00
If you feel like you'd like to help fix a bug or implement the new feature yourself, please read the Contributing
2019-07-11 19:19:50 -04:00
section next before starting any work.
< a name = "contributing" > < / a >
### Contributing
< a name = "contributing-pull-requests" > < / a >
#### Pull Requests
2021-05-30 22:32:47 -04:00
Simple Pull Requests that fix anything other than JJWT core code (documentation, JavaDoc, typos, test cases, etc) are
2019-07-11 19:19:50 -04:00
always appreciated and have a high likelihood of being merged quickly. Please send them!
2021-05-30 22:32:47 -04:00
However, if you want or feel the need to change JJWT's functionality or core code, please do not issue a pull request
without [creating a new JJWT issue ](https://github.com/jwtk/jjwt/issues/new ) and discussing your desired
2019-07-11 19:19:50 -04:00
changes **first** , _before you start working on it_ .
2021-05-30 22:32:47 -04:00
It would be a shame to reject your earnest and genuinely appreciated pull request if it might not align with the
project's goals, design expectations or planned functionality. We've sadly had to reject large PRs in the past because
they were out of sync with project or design expectations - all because the PR author didn't first check in with
2019-07-11 19:19:50 -04:00
the team first before working on a solution.
So, please [create a new JJWT issue ](https://github.com/jwtk/jjwt/issues/new ) first to discuss, and then we can see if
2021-05-30 22:32:47 -04:00
(or how) a PR is warranted. Thank you!
2019-07-11 19:19:50 -04:00
< a name = "contributing-help-wanted" > < / a >
#### Help Wanted
2021-05-30 22:32:47 -04:00
If you would like to help, but don't know where to start, please visit the
[Help Wanted Issues ](https://github.com/jwtk/jjwt/labels/help%20wanted ) page and pick any of the
2019-07-11 19:19:50 -04:00
ones there, and we'll be happy to discuss and answer questions in the issue comments.
2021-05-30 22:32:47 -04:00
If any of those don't appeal to you, no worries! Any help you would like to offer would be
2019-07-11 19:19:50 -04:00
appreciated based on the above caveats concerning [contributing pull reqeuests ](#contributing-pull-requests ). Feel free
to discuss or ask questions first if you're not sure. :)
2018-07-30 19:07:17 -04:00
< a name = "overview" > < / a >
## What is a JSON Web Token?
Don't know what a JSON Web Token is? Read on. Otherwise, jump on down to the [Installation ](#Installation ) section.
2016-07-02 17:09:22 -04:00
JWT is a means of transmitting information between two parties in a compact, verifiable form.
The bits of information encoded in the body of a JWT are called `claims` . The expanded form of the JWT is in a JSON format, so each `claim` is a key in the JSON object.
2021-05-30 22:32:47 -04:00
2016-07-02 17:09:22 -04:00
JWTs can be cryptographically signed (making it a [JWS ](https://tools.ietf.org/html/rfc7515 )) or encrypted (making it a [JWE ](https://tools.ietf.org/html/rfc7516 )).
This adds a powerful layer of verifiability to the user of JWTs. The receiver has a high degree of confidence that the JWT has not been tampered with by verifying the signature, for instance.
2018-07-30 19:07:17 -04:00
The compact representation of a signed JWT is a string that has three parts, each separated by a `.` :
2016-07-02 17:09:22 -04:00
```
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY
```
2021-05-30 22:32:47 -04:00
Each part is [Base64URL ](https://en.wikipedia.org/wiki/Base64 )-encoded. The first part is the header, which at a
minimum needs to specify the algorithm used to sign the JWT. The second part is the body. This part has all
the claims of this JWT encoded in it. The final part is the signature. It's computed by passing a combination of
2018-07-30 19:07:17 -04:00
the header and body through the algorithm specified in the header.
2021-05-30 22:32:47 -04:00
If you pass the first two parts through a base 64 url decoder, you'll get the following (formatting added for
2018-07-30 19:07:17 -04:00
clarity):
2016-07-02 17:09:22 -04:00
`header`
```
{
"alg": "HS256"
}
```
`body`
```
{
"sub": "Joe"
}
```
2021-05-30 22:32:47 -04:00
In this case, the information we have is that the HMAC using SHA-256 algorithm was used to sign the JWT. And, the
2018-07-30 19:07:17 -04:00
body has a single claim, `sub` with value `Joe` .
2016-07-02 17:09:22 -04:00
2021-05-30 22:32:47 -04:00
There are a number of standard claims, called [Registered Claims ](https://tools.ietf.org/html/rfc7519#section-4.1 ),
2018-07-30 19:07:17 -04:00
in the specification and `sub` (for subject) is one of them.
2016-07-02 17:09:22 -04:00
2018-07-30 19:07:17 -04:00
To compute the signature, you need a secret key to sign it. We'll cover keys and algorithms later.
2016-07-02 17:09:22 -04:00
2018-07-30 19:07:17 -04:00
< a name = "install" > < / a >
2014-09-19 23:22:49 -04:00
## Installation
2018-07-30 19:07:17 -04:00
Use your favorite Maven-compatible build tool to pull the dependencies from Maven Central.
2021-05-30 22:32:47 -04:00
The dependencies could differ slightly if you are working with a [JDK project ](#install-jdk ) or an
2018-07-30 19:07:17 -04:00
[Android project ](#install-android ).
2014-11-19 22:33:29 -05:00
2018-07-30 19:07:17 -04:00
< a name = "install-jdk" > < / a >
### JDK Projects
If you're building a (non-Android) JDK project, you will want to define the following dependencies:
< a name = "install-jdk-maven" > < / a >
#### Maven
2014-09-19 23:22:49 -04:00
```xml
< dependency >
< groupId > io.jsonwebtoken< / groupId >
2018-07-30 19:07:17 -04:00
< artifactId > jjwt-api< / artifactId >
2020-06-11 14:24:23 -04:00
< version > 0.11.2< / version >
2014-09-19 23:22:49 -04:00
< / dependency >
2018-07-30 19:07:17 -04:00
< dependency >
< groupId > io.jsonwebtoken< / groupId >
< artifactId > jjwt-impl< / artifactId >
2020-06-11 14:24:23 -04:00
< version > 0.11.2< / version >
2018-07-30 19:07:17 -04:00
< scope > runtime< / scope >
< / dependency >
< dependency >
< groupId > io.jsonwebtoken< / groupId >
2020-02-05 15:08:18 -05:00
< artifactId > jjwt-jackson< / artifactId > <!-- or jjwt - gson if Gson is preferred -->
2020-06-11 14:24:23 -04:00
< version > 0.11.2< / version >
2018-07-30 19:07:17 -04:00
< scope > runtime< / scope >
< / dependency >
2021-05-30 22:32:47 -04:00
<!-- Uncomment this next dependency if you are using JDK 10 or earlier and you also want to use
2020-02-05 15:08:18 -05:00
RSASSA-PSS (PS256, PS384, PS512) algorithms. JDK 11 or later does not require it for those algorithms:
2018-07-30 19:07:17 -04:00
< dependency >
< groupId > org.bouncycastle< / groupId >
< artifactId > bcprov-jdk15on< / artifactId >
< version > 1.60< / version >
< scope > runtime< / scope >
< / dependency >
-->
```
< a name = "install-jdk-gradle" > < / a >
#### Gradle
```groovy
dependencies {
2021-05-30 22:57:42 -04:00
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2',
2018-08-02 15:31:13 -04:00
// Uncomment the next line if you want to use RSASSA-PSS (PS256, PS384, PS512) algorithms:
2018-07-30 19:07:17 -04:00
//'org.bouncycastle:bcprov-jdk15on:1.60',
2020-06-11 14:24:23 -04:00
'io.jsonwebtoken:jjwt-jackson:0.11.2' // or 'io.jsonwebtoken:jjwt-gson:0.11.2' for gson
2018-07-30 19:07:17 -04:00
}
2014-09-19 23:22:49 -04:00
```
2014-11-19 22:33:29 -05:00
2018-07-30 19:07:17 -04:00
< a name = "install-android" > < / a >
### Android Projects
Android projects will want to define the following dependencies and Proguard exclusions:
< a name = "install-android-dependencies" > < / a >
#### Dependencies
Add the dependencies to your project:
2014-11-19 22:33:29 -05:00
```groovy
2014-11-19 22:31:37 -05:00
dependencies {
2020-06-11 14:24:23 -04:00
api 'io.jsonwebtoken:jjwt-api:0.11.2'
2021-05-30 22:32:47 -04:00
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2'
2020-06-11 14:24:23 -04:00
runtimeOnly('io.jsonwebtoken:jjwt-orgjson:0.11.2') {
2018-07-30 19:07:17 -04:00
exclude group: 'org.json', module: 'json' //provided by Android natively
}
2018-08-02 15:31:13 -04:00
// Uncomment the next line if you want to use RSASSA-PSS (PS256, PS384, PS512) algorithms:
2018-08-02 17:30:18 -04:00
//runtimeOnly 'org.bouncycastle:bcprov-jdk15on:1.60'
2014-11-19 22:31:37 -05:00
}
```
2014-09-19 23:22:49 -04:00
2018-07-30 19:07:17 -04:00
< a name = "install-android-proguard" > < / a >
#### Proguard
2021-05-30 22:32:47 -04:00
You can use the following [Android Proguard ](https://developer.android.com/studio/build/shrink-code ) exclusion rules:
2018-07-30 19:07:17 -04:00
```
-keepattributes InnerClasses
-keep class io.jsonwebtoken.** { *; }
-keepnames class io.jsonwebtoken.* { *; }
-keepnames interface io.jsonwebtoken.* { *; }
-keep class org.bouncycastle.** { *; }
-keepnames class org.bouncycastle.** { *; }
-dontwarn org.bouncycastle.**
```
< a name = "install-understandingdependencies" > < / a >
### Understanding JJWT Dependencies
2021-05-30 22:32:47 -04:00
Notice the above dependency declarations all have only one compile-time dependency and the rest are declared as
2018-07-30 19:07:17 -04:00
_runtime_ dependencies.
This is because JJWT is designed so you only depend on the APIs that are explicitly designed for you to use in
your applications and all other internal implementation details - that can change without warning - are relegated to
2021-05-30 22:32:47 -04:00
runtime-only dependencies. This is an extremely important point if you want to ensure stable JJWT usage and
2018-07-30 19:07:17 -04:00
upgrades over time:
2021-05-30 22:32:47 -04:00
**JJWT guarantees semantic versioning compatibility for all of its artifacts _except_ the `jjwt-impl` .jar. No such
guarantee is made for the `jjwt-impl` .jar and internal changes in that .jar can happen at any time. Never add the
2018-07-30 19:07:17 -04:00
`jjwt-impl` .jar to your project with `compile` scope - always declare it with `runtime` scope.**
2014-09-26 16:32:19 -04:00
2018-07-30 19:07:17 -04:00
This is done to benefit you: great care goes into curating the `jjwt-api` .jar and ensuring it contains what you need
2021-05-30 22:32:47 -04:00
and remains backwards compatible as much as is possible so you can depend on that safely with compile scope. The
runtime `jjwt-impl` .jar strategy affords the JJWT developers the flexibility to change the internal packages and
implementations whenever and however necessary. This helps us implement features, fix bugs, and ship new releases to
2018-07-30 19:07:17 -04:00
you more quickly and efficiently.
< a name = "quickstart" > < / a >
2016-06-17 18:11:08 -04:00
## Quickstart
2014-09-19 23:22:49 -04:00
2021-05-30 22:32:47 -04:00
Most complexity is hidden behind a convenient and readable builder-based [fluent interface ](http://en.wikipedia.org/wiki/Fluent_interface ), great for relying on IDE auto-completion to write code quickly. Here's an example:
2014-09-18 22:14:22 -04:00
2014-09-19 17:47:01 -04:00
```java
2014-09-19 23:23:29 -04:00
import io.jsonwebtoken.Jwts;
2015-03-14 20:44:56 -04:00
import io.jsonwebtoken.SignatureAlgorithm;
2018-07-30 19:07:17 -04:00
import io.jsonwebtoken.security.Keys;
2015-05-12 21:55:15 -04:00
import java.security.Key;
2014-09-19 23:22:49 -04:00
2015-03-14 20:44:56 -04:00
// We need a signing key, so we'll create one just for this example. Usually
// the key would be read from your application configuration instead.
2018-07-30 19:07:17 -04:00
Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
2014-09-18 22:14:22 -04:00
2018-07-30 19:07:17 -04:00
String jws = Jwts.builder().setSubject("Joe").signWith(key).compact();
2014-09-19 17:47:01 -04:00
```
2014-09-18 22:14:22 -04:00
How easy was that!?
2018-07-30 19:07:17 -04:00
In this case, we are:
2021-05-30 22:32:47 -04:00
1. *building* a JWT that will have the
[registered claim ](https://tools.ietf.org/html/rfc7519#section-4.1 ) `sub` (subject) set to `Joe` . We are then
2. *signing* the JWT using a key suitable for the HMAC-SHA-256 algorithm. Finally, we are
3. *compacting* it into its final `String` form. A signed JWT is called a 'JWS'.
2016-06-17 18:11:08 -04:00
2018-07-30 19:07:17 -04:00
The resultant `jws` String looks like this:
2016-06-17 18:11:08 -04:00
```
2018-07-30 19:07:17 -04:00
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.1KP0SsvENi7Uz1oQc07aXTL7kpQG5jBNIybqr60AlD4
2016-06-17 18:11:08 -04:00
```
2014-09-18 22:14:22 -04:00
Now let's verify the JWT (you should always discard JWTs that don't match an expected signature):
2014-09-19 17:47:01 -04:00
```java
2020-02-05 15:08:18 -05:00
assert Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(jws).getBody().getSubject().equals("Joe");
2014-09-19 17:47:01 -04:00
```
2014-09-18 22:14:22 -04:00
2018-07-30 19:07:17 -04:00
**NOTE: Ensure you call the `parseClaimsJws` method** (since there are many similar methods available). You will get an `UnsupportedJwtException` if you parse your JWT with wrong method.
2017-08-12 10:44:44 -04:00
2021-05-30 22:32:47 -04:00
There are two things going on here. The `key` from before is being used to validate the signature of the JWT. If it
fails to verify the JWT, a `SignatureException` (which extends from `JwtException` ) is thrown. Assuming the JWT is
2018-07-30 19:07:17 -04:00
validated, we parse out the claims and assert that that subject is set to `Joe` .
2016-06-17 18:11:08 -04:00
You have to love code one-liners that pack a punch!
2014-09-18 22:14:22 -04:00
2021-05-30 22:32:47 -04:00
But what if parsing or signature validation failed? You can catch `JwtException` and react accordingly:
2014-09-18 22:14:22 -04:00
2014-09-19 17:47:01 -04:00
```java
try {
2014-09-18 22:14:22 -04:00
2020-02-05 15:08:18 -05:00
Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(compactJws);
2014-09-18 22:14:22 -04:00
2014-09-19 17:47:01 -04:00
//OK, we can trust this JWT
2014-09-18 22:14:22 -04:00
2018-07-30 19:07:17 -04:00
} catch (JwtException e) {
2014-09-18 22:14:22 -04:00
2014-09-19 17:47:01 -04:00
//don't trust the JWT!
}
```
2018-01-04 08:09:37 -05:00
2018-07-30 19:07:17 -04:00
< a name = "jws" > < / a >
## Signed JWTs
2018-01-04 08:09:37 -05:00
2021-05-30 22:32:47 -04:00
The JWT specification provides for the ability to
[cryptographically _sign_ ](https://en.wikipedia.org/wiki/Digital_signature ) a JWT. Signing a JWT:
2018-07-30 19:07:17 -04:00
1. guarantees the JWT was created by someone we know (it is authentic) as well as
2. guarantees that no-one has manipulated or changed the JWT after it was created (its integrity is maintained).
2021-05-30 22:32:47 -04:00
These two properties - authenticity and integrity - assure us that a JWT contains information we can trust. If a
2018-07-30 19:07:17 -04:00
JWT fails authenticity or integrity checks, we should always reject that JWT because we can't trust it.
2021-05-30 22:32:47 -04:00
So how is a JWT signed? Let's walk through it with some easy-to-read pseudocode:
2018-07-30 19:07:17 -04:00
1. Assume we have a JWT with a JSON header and body (aka 'Claims') as follows:
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
**header**
```
{
"alg": "HS256"
}
```
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
**body**
```
{
"sub": "Joe"
}
```
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
2. Remove all unnecessary whitespace in the JSON:
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
```groovy
String header = '{"alg":"HS256"}'
String claims = '{"sub":"Joe"}'
```
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
3. Get the UTF-8 bytes and Base64URL-encode each:
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
```groovy
String encodedHeader = base64URLEncode( header.getBytes("UTF-8") )
String encodedClaims = base64URLEncode( claims.getBytes("UTF-8") )
```
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
4. Concatenate the encoded header and claims with a period character between them:
```groovy
String concatenated = encodedHeader + '.' + encodedClaims
```
2021-05-30 22:32:47 -04:00
5. Use a sufficiently-strong cryptographic secret or private key, along with a signing algorithm of your choice
(we'll use HMAC-SHA-256 here), and sign the concatenated string:
```groovy
Key key = getMySecretKey()
byte[] signature = hmacSha256( concatenated, key )
```
6. Because signatures are always byte arrays, Base64URL-encode the signature and append a period character '.' and it
2018-07-30 19:07:17 -04:00
to the concatenated string:
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
```groovy
String jws = concatenated + '.' + base64URLEncode( signature )
```
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
And there you have it, the final `jws` String looks like this:
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
```
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.1KP0SsvENi7Uz1oQc07aXTL7kpQG5jBNIybqr60AlD4
2018-01-04 08:09:37 -05:00
```
2018-07-30 19:07:17 -04:00
This is called a 'JWS' - short for _signed_ JWT.
2018-01-04 08:09:37 -05:00
2021-05-30 22:32:47 -04:00
Of course, no one would want to do this manually in code, and worse, if you get anything wrong, you could cause
security problems or weaknesses. As a result, JJWT was created to handle all of this for you: JJWT completely
2018-07-30 19:07:17 -04:00
automates both the creation of JWSs as well as the parsing and verification of JWSs for you.
2021-05-30 22:32:47 -04:00
But before we dig in to showing you how to create a JWS using JJWT, let's briefly discuss Signature Algorithms and
Keys, specifically as they relate to the JWT specifications. Understanding them is critical to being able to create a
2018-07-30 19:07:17 -04:00
JWS properly.
< a name = "jws-key" > < / a >
### Signature Algorithms Keys
2021-05-30 22:32:47 -04:00
The JWT specification identifies 12 standard signature algorithms - 3 secret key algorithms and 9 asymmetric
2018-07-30 19:07:17 -04:00
key algorithms - identified by the following names:
2021-05-30 22:32:47 -04:00
* `HS256` : HMAC using SHA-256
* `HS384` : HMAC using SHA-384
* `HS512` : HMAC using SHA-512
* `ES256` : ECDSA using P-256 and SHA-256
* `ES384` : ECDSA using P-384 and SHA-384
* `ES512` : ECDSA using P-521 and SHA-512
* `RS256` : RSASSA-PKCS-v1_5 using SHA-256
* `RS384` : RSASSA-PKCS-v1_5 using SHA-384
* `RS512` : RSASSA-PKCS-v1_5 using SHA-512
* `PS256` : RSASSA-PSS using SHA-256 and MGF1 with SHA-256
* `PS384` : RSASSA-PSS using SHA-384 and MGF1 with SHA-384
* `PS512` : RSASSA-PSS using SHA-512 and MGF1 with SHA-512
2018-07-30 19:07:17 -04:00
These are all represented in the `io.jsonwebtoken.SignatureAlgorithm` enum.
What's really important about these algorithms - other than their security properties - is that the JWT specification
[RFC 7518, Sections 3.2 through 3.5 ](https://tools.ietf.org/html/rfc7518#section-3 )
_requires_ (mandates) that you MUST use keys that are sufficiently strong for a chosen algorithm.
This means that JJWT - a specification-compliant library - will also enforce that you use sufficiently strong keys
2021-05-30 22:32:47 -04:00
for the algorithms you choose. If you provide a weak key for a given algorithm, JJWT will reject it and throw an
2018-07-30 19:07:17 -04:00
exception.
2021-05-30 22:32:47 -04:00
This is not because we want to make your life difficult, we promise! The reason why the JWT specification, and
consequently JJWT, mandates key lengths is that the security model of a particular algorithm can completely break
down if you don't adhere to the mandatory key properties of the algorithm, effectively having no security at all. No
one wants completely insecure JWTs, right? Neither would we.
2018-07-30 19:07:17 -04:00
So what are the requirements?
2018-01-04 08:09:37 -05:00
2018-07-30 19:07:17 -04:00
< a name = "jws-key-hmacsha" > < / a >
#### HMAC-SHA
2018-01-04 08:09:37 -05:00
2018-07-30 19:07:17 -04:00
JWT HMAC-SHA signature algorithms `HS256` , `HS384` , and `HS512` require a secret key that is _at least_ as many bits as
2021-05-30 22:32:47 -04:00
the algorithm's signature (digest) length per [RFC 7512 Section 3.2 ](https://tools.ietf.org/html/rfc7518#section-3.2 ).
2018-07-30 19:07:17 -04:00
This means:
2021-05-30 22:32:47 -04:00
* `HS256` is HMAC-SHA-256, and that produces digests that are 256 bits (32 bytes) long, so `HS256` _requires_ that you
2018-07-30 19:07:17 -04:00
use a secret key that is at least 32 bytes long.
2021-05-30 22:32:47 -04:00
* `HS384` is HMAC-SHA-384, and that produces digests that are 384 bits (48 bytes) long, so `HS384` _requires_ that you
use a secret key that is at least 48 bytes long.
2018-07-30 19:07:17 -04:00
2021-05-30 22:32:47 -04:00
* `HS512` is HMAC-SHA-512, and that produces digests that are 512 bits (64 bytes) long, so `HS512` _requires_ that you
use a secret key that is at least 64 bytes long.
2018-07-30 19:07:17 -04:00
< a name = "jws-key-rsa" > < / a >
#### RSA
JWT RSA signature algorithms `RS256` , `RS384` , `RS512` , `PS256` , `PS384` and `PS512` all require a minimum key length
2021-05-30 22:32:47 -04:00
(aka an RSA modulus bit length) of `2048` bits per RFC 7512 Sections
[3.3 ](https://tools.ietf.org/html/rfc7518#section-3.3 ) and [3.5 ](https://tools.ietf.org/html/rfc7518#section-3.5 ).
2018-07-30 19:07:17 -04:00
Anything smaller than this (such as 1024 bits) will be rejected with an `InvalidKeyException` .
2021-05-30 22:32:47 -04:00
That said, in keeping with best practices and increasing key lengths for security longevity, JJWT
2021-09-25 11:22:40 -04:00
recommends that you use:
2018-07-30 19:07:17 -04:00
2021-05-30 22:32:47 -04:00
* at least 2048 bit keys with `RS256` and `PS256`
* at least 3072 bit keys with `RS384` and `PS384`
* at least 4096 bit keys with `RS512` and `PS512`
2018-07-30 19:07:17 -04:00
These are only JJWT suggestions and not requirements. JJWT only enforces JWT specification requirements and
for any RSA key, the requirement is the RSA key (modulus) length in bits MUST be >= 2048 bits.
< a name = "jws-key-ecdsa" > < / a >
#### Elliptic Curve
JWT Elliptic Curve signature algorithms `ES256` , `ES384` , and `ES512` all require a minimum key length
2021-05-30 22:32:47 -04:00
(aka an Elliptic Curve order bit length) that is _at least_ as many bits as the algorithm signature's individual
`R` and `S` components per [RFC 7512 Section 3.4 ](https://tools.ietf.org/html/rfc7518#section-3.4 ). This means:
2018-07-30 19:07:17 -04:00
2021-05-30 22:32:47 -04:00
* `ES256` requires that you use a private key that is at least 256 bits (32 bytes) long.
* `ES384` requires that you use a private key that is at least 384 bits (48 bytes) long.
2018-07-30 19:07:17 -04:00
2021-05-30 22:32:47 -04:00
* `ES512` requires that you use a private key that is at least 512 bits (64 bytes) long.
2018-07-30 19:07:17 -04:00
< a name = "jws-key-create" > < / a >
#### Creating Safe Keys
If you don't want to think about bit length requirements or just want to make your life easier, JJWT has
provided the `io.jsonwebtoken.security.Keys` utility class that can generate sufficiently secure keys for any given
JWT signature algorithm you might want to use.
< a name = "jws-key-create-secret" > < / a >
##### Secret Keys
2021-05-30 22:32:47 -04:00
If you want to generate a sufficiently strong `SecretKey` for use with the JWT HMAC-SHA algorithms, use the
2018-07-30 19:07:17 -04:00
`Keys.secretKeyFor(SignatureAlgorithm)` helper method:
```java
SecretKey key = Keys.secretKeyFor(SignatureAlgorithm.HS256); //or HS384 or HS512
2018-01-04 08:09:37 -05:00
```
2014-09-18 22:14:22 -04:00
2018-07-30 19:07:17 -04:00
Under the hood, JJWT uses the JCA provider's `KeyGenerator` to create a secure-random key with the correct minimum
length for the given algorithm.
2014-09-18 22:14:22 -04:00
2020-02-05 01:50:56 -05:00
If you need to save this new `SecretKey` , you can Base64 (or Base64URL) encode it:
2016-06-17 18:11:08 -04:00
2018-07-30 19:07:17 -04:00
```java
2020-02-05 01:50:56 -05:00
String secretString = Encoders.BASE64.encode(key.getEncoded());
2018-07-30 19:07:17 -04:00
```
2014-09-18 22:14:22 -04:00
2021-05-30 22:32:47 -04:00
Ensure you save the resulting `secretString` somewhere safe -
[Base64-encoding is not encryption ](#base64-not-encryption ), so it's still considered sensitive information. You can
2020-02-05 01:50:56 -05:00
further encrypt it, etc, before saving to disk (for example).
2018-07-30 19:07:17 -04:00
< a name = "jws-key-create-asym" > < / a >
##### Asymmetric Keys
If you want to generate sufficiently strong Elliptic Curve or RSA asymmetric key pairs for use with JWT ECDSA or RSA
algorithms, use the `Keys.keyPairFor(SignatureAlgorithm)` helper method:
2016-07-02 17:09:22 -04:00
2018-07-30 19:07:17 -04:00
```java
KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.RS256); //or RS384, RS512, PS256, PS384, PS512, ES256, ES384, ES512
```
2021-05-30 22:32:47 -04:00
You use the private key (`keyPair.getPrivate()`) to create a JWS and the public key (`keyPair.getPublic()`) to
2018-07-30 19:07:17 -04:00
parse/verify a JWS.
2021-05-30 22:32:47 -04:00
**NOTE: The `PS256` , `PS384` , and `PS512` algorithms require JDK 11 or a compatible JCA Provider
(like BouncyCastle) in the runtime classpath.** If you are using JDK 10 or earlier and you want to use them, see
the [Installation ](#Installation ) section to see how to enable BouncyCastle. All other algorithms are natively
2018-08-02 15:31:13 -04:00
supported by the JDK.
2018-07-30 19:07:17 -04:00
< a name = "jws-create" > < / a >
### Creating a JWS
You create a JWS as follows:
2021-05-30 22:32:47 -04:00
1. Use the `Jwts.builder()` method to create a `JwtBuilder` instance.
2018-07-30 19:07:17 -04:00
2. Call `JwtBuilder` methods to add header parameters and claims as desired.
3. Specify the `SecretKey` or asymmetric `PrivateKey` you want to use to sign the JWT.
4. Finally, call the `compact()` method to compact and sign, producing the final jws.
For example:
```java
String jws = Jwts.builder() // (1)
2021-05-30 22:32:47 -04:00
.setSubject("Bob") // (2)
2018-07-30 19:07:17 -04:00
.signWith(key) // (3)
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
.compact(); // (4)
```
< a name = "jws-create-header" > < / a >
#### Header Parameters
A JWT Header provides metadata about the contents, format and cryptographic operations relevant to the JWT's Claims.
2021-05-30 22:32:47 -04:00
If you need to set one or more JWT header parameters, such as the `kid`
2018-07-30 19:07:17 -04:00
[(Key ID) header parameter ](https://tools.ietf.org/html/rfc7515#section-4.1.4 ), you can simply call
2019-03-10 12:07:37 -04:00
`JwtBuilder` `setHeaderParam` one or more times as needed:
2018-07-30 19:07:17 -04:00
```java
String jws = Jwts.builder()
2019-03-10 12:07:37 -04:00
.setHeaderParam("kid", "myKeyId")
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// ... etc ...
```
2021-05-30 22:32:47 -04:00
Each time `setHeaderParam` is called, it simply appends the key-value pair to an internal `Header` instance,
2018-07-30 19:07:17 -04:00
potentially overwriting any existing identically-named key/value pair.
**NOTE**: You do not need to set the `alg` or `zip` header parameters as JJWT will set them automatically
depending on the signature algorithm or compression algorithm used.
< a name = "jws-create-header-instance" > < / a >
##### Header Instance
If you want to specify the entire header at once, you can use the `Jwts.header()` method and build up the header
paramters with it:
```java
Header header = Jwts.header();
populate(header); //implement me
String jws = Jwts.builder()
.setHeader(header)
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// ... etc ...
```
2021-05-30 22:32:47 -04:00
**NOTE**: Calling `setHeader` will overwrite any existing header name/value pairs with the same names that might have
already been set. In all cases however, JJWT will still set (and overwrite) any `alg` and `zip` headers regardless
2018-07-30 19:07:17 -04:00
if those are in the specified `header` object or not.
< a name = "jws-create-header-map" > < / a >
##### Header Map
2021-05-30 22:32:47 -04:00
If you want to specify the entire header at once and you don't want to use `Jwts.header()` , you can use `JwtBuilder`
2018-07-30 19:07:17 -04:00
`setHeader(Map)` method instead:
```java
Map< String , Object > header = getMyHeaderMap(); //implement me
String jws = Jwts.builder()
.setHeader(header)
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// ... etc ...
```
2021-05-30 22:32:47 -04:00
**NOTE**: Calling `setHeader` will overwrite any existing header name/value pairs with the same names that might have
already been set. In all cases however, JJWT will still set (and overwrite) any `alg` and `zip` headers regardless
2018-07-30 19:07:17 -04:00
if those are in the specified `header` object or not.
< a name = "jws-create-claims" > < / a >
#### Claims
Claims are a JWT's 'body' and contain the information that the JWT creator wishes to present to the JWT recipient(s).
< a name = "jws-create-claims-standard" > < / a >
##### Standard Claims
2021-05-30 22:32:47 -04:00
The `JwtBuilder` provides convenient setter methods for standard registered Claim names defined in the JWT
specification. They are:
2018-07-30 19:07:17 -04:00
2021-05-30 22:32:47 -04:00
* `setIssuer` : sets the [`iss` (Issuer) Claim ](https://tools.ietf.org/html/rfc7519#section-4.1.1 )
* `setSubject` : sets the [`sub` (Subject) Claim ](https://tools.ietf.org/html/rfc7519#section-4.1.2 )
* `setAudience` : sets the [`aud` (Audience) Claim ](https://tools.ietf.org/html/rfc7519#section-4.1.3 )
* `setExpiration` : sets the [`exp` (Expiration Time) Claim ](https://tools.ietf.org/html/rfc7519#section-4.1.4 )
* `setNotBefore` : sets the [`nbf` (Not Before) Claim ](https://tools.ietf.org/html/rfc7519#section-4.1.5 )
* `setIssuedAt` : sets the [`iat` (Issued At) Claim ](https://tools.ietf.org/html/rfc7519#section-4.1.6 )
* `setId` : sets the [`jti` (JWT ID) Claim ](https://tools.ietf.org/html/rfc7519#section-4.1.7 )
2018-07-30 19:07:17 -04:00
For example:
```java
String jws = Jwts.builder()
.setIssuer("me")
.setSubject("Bob")
.setAudience("you")
.setExpiration(expiration) //a java.util.Date
2021-05-30 22:32:47 -04:00
.setNotBefore(notBefore) //a java.util.Date
2018-07-30 19:07:17 -04:00
.setIssuedAt(new Date()) // for example, now
.setId(UUID.randomUUID()) //just an example id
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
/// ... etc ...
```
< a name = "jws-create-claims-custom" > < / a >
##### Custom Claims
If you need to set one or more custom claims that don't match the standard setter method claims shown above, you
can simply call `JwtBuilder` `claim` one or more times as needed:
```java
String jws = Jwts.builder()
.claim("hello", "world")
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// ... etc ...
```
2021-05-30 22:32:47 -04:00
Each time `claim` is called, it simply appends the key-value pair to an internal `Claims` instance, potentially
2018-07-30 19:07:17 -04:00
overwriting any existing identically-named key/value pair.
2021-05-30 22:32:47 -04:00
Obviously, you do not need to call `claim` for any [standard claim name ](#jws-create-claims-standard ) and it is
2018-07-30 19:07:17 -04:00
recommended instead to call the standard respective setter method as this enhances readability.
< a name = "jws-create-claims-instance" > < / a >
###### Claims Instance
If you want to specify all claims at once, you can use the `Jwts.claims()` method and build up the claims
with it:
```java
Claims claims = Jwts.claims();
populate(claims); //implement me
String jws = Jwts.builder()
.setClaims(claims)
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// ... etc ...
```
2021-05-30 22:32:47 -04:00
**NOTE**: Calling `setClaims` will overwrite any existing claim name/value pairs with the same names that might have
2018-07-30 19:07:17 -04:00
already been set.
< a name = "jws-create-claims-map" > < / a >
###### Claims Map
2021-05-30 22:32:47 -04:00
If you want to specify all claims at once and you don't want to use `Jwts.claims()` , you can use `JwtBuilder`
2018-07-30 19:07:17 -04:00
`setClaims(Map)` method instead:
```java
Map< String , Object > claims = getMyClaimsMap(); //implement me
String jws = Jwts.builder()
.setClaims(claims)
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// ... etc ...
```
2021-05-30 22:32:47 -04:00
**NOTE**: Calling `setClaims` will overwrite any existing claim name/value pairs with the same names that might have
2018-07-30 19:07:17 -04:00
already been set.
< a name = "jws-create-key" > < / a >
#### Signing Key
It is recommended that you specify the signing key by calling call the `JwtBuilder` 's `signWith` method and let JJWT
determine the most secure algorithm allowed for the specified key.:
```java
String jws = Jwts.builder()
// ... etc ...
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
.signWith(key) // < ---
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
.compact();
```
For example, if you call `signWith` with a `SecretKey` that is 256 bits (32 bytes) long, it is not strong enough for
`HS384` or `HS512` , so JJWT will automatically sign the JWT using `HS256` .
2021-05-30 22:32:47 -04:00
When using `signWith` JJWT will also automatically set the required `alg` header with the associated algorithm
2018-07-30 19:07:17 -04:00
identifier.
2019-03-10 12:05:09 -04:00
Similarly, if you called `signWith` with an RSA `PrivateKey` that was 4096 bits long, JJWT will use the `RS512`
2018-07-30 19:07:17 -04:00
algorithm and automatically set the `alg` header to `RS512` .
The same selection logic applies for Elliptic Curve `PrivateKey` s.
**NOTE: You cannot sign JWTs with `PublicKey` s as this is always insecure.** JJWT will reject any specified
2020-02-05 01:50:56 -05:00
`PublicKey` for signing with an `InvalidKeyException` .
< a name = "jws-create-key-secret" > < / a >
##### SecretKey Formats
2021-05-30 22:32:47 -04:00
If you want to sign a JWS using HMAC-SHA algorithms and you have a secret key `String` or
2020-02-05 01:50:56 -05:00
[encoded byte array ](https://docs.oracle.com/javase/8/docs/api/java/security/Key.html#getEncoded-- ), you will need
to convert it into a `SecretKey` instance to use as the `signWith` method argument.
If your secret key is:
2021-05-30 22:32:47 -04:00
* An [encoded byte array ](https://docs.oracle.com/javase/8/docs/api/java/security/Key.html#getEncoded-- ):
2020-02-05 01:50:56 -05:00
```java
SecretKey key = Keys.hmacShaKeyFor(encodedKeyBytes);
```
2021-05-30 22:32:47 -04:00
* A Base64-encoded string:
2020-02-05 01:50:56 -05:00
```java
SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(secretString));
```
2021-05-30 22:32:47 -04:00
* A Base64URL-encoded string:
2020-02-05 01:50:56 -05:00
```java
SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64URL.decode(secretString));
```
2021-05-30 22:32:47 -04:00
* A raw (non-encoded) string (e.g. a password String):
2020-02-05 01:50:56 -05:00
```java
SecretKey key = Keys.hmacShaKeyFor(secretString.getBytes(StandardCharsets.UTF_8));
```
It is always incorrect to call `secretString.getBytes()` (without providing a charset).
2021-05-30 22:32:47 -04:00
However, raw password strings like this, e.g. `correcthorsebatterystaple` should be avoided whenever possible
because they can inevitably result in weak or susceptible keys. Secure-random keys are almost always stronger.
2020-02-05 01:50:56 -05:00
If you are able, prefer creating a [new secure-random secret key ](#jws-key-create-secret ) instead.
2018-07-30 19:07:17 -04:00
< a name = "jws-create-key-algoverride" > < / a >
##### SignatureAlgorithm Override
In some specific cases, you might want to override JJWT's default selected algorithm for a given key.
For example, if you have an RSA `PrivateKey` that is 2048 bits, JJWT would automatically choose the `RS256` algorithm.
If you wanted to use `RS384` or `RS512` instead, you could manually specify it with the overloaded `signWith` method
that accepts the `SignatureAlgorithm` as an additional parameter:
```java
.signWith(privateKey, SignatureAlgorithm.RS512) // < ---
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
.compact();
```
2021-05-30 22:32:47 -04:00
This is allowed because the JWT specification allows any RSA algorithm strength for any RSA key >= 2048 bits. JJWT just
2018-07-30 19:07:17 -04:00
prefers `RS512` for keys >= 4096 bits, followed by `RS384` for keys >= 3072 bits and finally `RS256` for keys >= 2048
bits.
2021-05-30 22:32:47 -04:00
**In all cases however, regardless of your chosen algorithms, JJWT will assert that the specified key is allowed to be
2018-07-30 19:07:17 -04:00
used for that algorithm according to the JWT specification requirements.**
< a name = "jws-create-compression" > < / a >
#### JWS Compression
2021-05-30 22:32:47 -04:00
If your JWT claims set is large (contains a lot of data), and you are certain that JJWT will also be the same library
that reads/parses your JWS, you might want to compress the JWS to reduce its size. Note that this is
*not* a standard feature for JWS and is not likely to be supported by other JWT libraries.
2018-07-30 19:07:17 -04:00
Please see the main [Compression ](#compression ) section to see how to compress and decompress JWTs.
< a name = "jws-read" > < / a >
### Reading a JWS
You read (parse) a JWS as follows:
2021-05-30 22:32:47 -04:00
1. Use the `Jwts.parserBuilder()` method to create a `JwtParserBuilder` instance.
2018-07-30 19:07:17 -04:00
2. Specify the `SecretKey` or asymmetric `PublicKey` you want to use to verify the JWS signature.< sup > 1</ sup >
2020-02-05 15:08:18 -05:00
3. Call the `build()` method on the `JwtParserBuilder` to return a thread-safe `JwtParser` .
4. Finally, call the `parseClaimsJws(String)` method with your jws `String` , producing the original JWS.
2021-05-30 22:32:47 -04:00
5. The entire call is wrapped in a try/catch block in case parsing or signature validation fails. We'll cover
2018-07-30 19:07:17 -04:00
exceptions and causes for failure later.
2021-05-30 22:32:47 -04:00
< sup > 1. If you don't know which key to use at the time of parsing, you can look up the key using a `SigningKeyResolver`
2019-02-07 14:20:18 -05:00
which [we'll cover later ](#jws-read-key-resolver ).</ sup >
2018-07-30 19:07:17 -04:00
For example:
```java
Jws< Claims > jws;
try {
2020-02-05 15:08:18 -05:00
jws = Jwts.parserBuilder() // (1)
2018-07-30 19:07:17 -04:00
.setSigningKey(key) // (2)
2020-02-05 15:08:18 -05:00
.build() // (3)
.parseClaimsJws(jwsString); // (4)
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// we can safely trust the JWT
2021-05-30 22:32:47 -04:00
2020-02-05 15:08:18 -05:00
catch (JwtException ex) { // (5)
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// we *cannot* use the JWT as intended by its creator
}
```
2021-05-30 22:32:47 -04:00
**NOTE: If you are expecting a JWS, always call `JwtParser` 's `parseClaimsJws` method** (and not one of the other similar methods
2018-07-30 19:07:17 -04:00
available) as this guarantees the correct security model for parsing signed JWTs.
< a name = "jws-read-key" > < / a >
#### Verification Key
The most important thing to do when reading a JWS is to specify the key to use to verify the JWS's
2021-05-30 22:32:47 -04:00
cryptographic signature. If signature verification fails, the JWT cannot be safely trusted and should be
2018-07-30 19:07:17 -04:00
discarded.
So which key do we use for verification?
2021-05-30 22:32:47 -04:00
* If the jws was signed with a `SecretKey` , the same `SecretKey` should be specified on the `JwtParserBuilder` . For example:
2018-07-30 19:07:17 -04:00
```java
2020-02-05 15:08:18 -05:00
Jwts.parserBuilder()
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
.setSigningKey(secretKey) // < ----
2021-05-30 22:32:47 -04:00
2020-02-05 15:08:18 -05:00
.build()
2018-07-30 19:07:17 -04:00
.parseClaimsJws(jwsString);
```
2021-05-30 22:32:47 -04:00
* If the jws was signed with a `PrivateKey` , that key's corresponding `PublicKey` (not the `PrivateKey` ) should be
specified on the `JwtParserBuilder` . For example:
2018-07-30 19:07:17 -04:00
```java
2020-02-05 15:08:18 -05:00
Jwts.parserBuilder()
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
.setSigningKey(publicKey) // < ---- publicKey , not privateKey
2021-05-30 22:32:47 -04:00
2020-02-05 15:08:18 -05:00
.build()
2018-07-30 19:07:17 -04:00
.parseClaimsJws(jwsString);
```
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
But you might have noticed something - what if your application doesn't use just a single SecretKey or KeyPair? What
2021-05-30 22:32:47 -04:00
if JWSs can be created with different `SecretKey` s or public/private keys, or a combination of both? How do you
2018-07-30 19:07:17 -04:00
know which key to specify if you can't inspect the JWT first?
2020-02-05 15:08:18 -05:00
In these cases, you can't call the `JwtParserBuilder` 's `setSigningKey` method with a single key - instead, you'll need
2018-07-30 19:07:17 -04:00
to use a `SigningKeyResolver` , covered next.
< a name = "jws-read-key-resolver" > < / a >
##### Signing Key Resolver
If your application expects JWSs that can be signed with different keys, you won't call the `setSigningKey` method.
2021-05-30 22:32:47 -04:00
Instead, you'll need to implement the
2020-02-05 15:08:18 -05:00
`SigningKeyResolver` interface and specify an instance on the `JwtParserBuilder` via the `setSigningKeyResolver` method.
2018-07-30 19:07:17 -04:00
For example:
```java
SigningKeyResolver signingKeyResolver = getMySigningKeyResolver();
2020-02-05 15:08:18 -05:00
Jwts.parserBuilder()
2018-07-30 19:07:17 -04:00
.setSigningKeyResolver(signingKeyResolver) // < ----
2021-05-30 22:32:47 -04:00
2020-02-05 15:08:18 -05:00
.build()
2018-07-30 19:07:17 -04:00
.parseClaimsJws(jwsString);
```
2021-05-30 22:32:47 -04:00
You can simplify things a little by extending from the `SigningKeyResolverAdapter` and implementing the
`resolveSigningKey(JwsHeader, Claims)` method. For example:
2018-07-30 19:07:17 -04:00
```java
public class MySigningKeyResolver extends SigningKeyResolverAdapter {
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
@Override
public Key resolveSigningKey(JwsHeader jwsHeader, Claims claims) {
// implement me
2016-07-02 17:09:22 -04:00
}
2018-07-30 19:07:17 -04:00
}
```
The `JwtParser` will invoke the `resolveSigningKey` method after parsing the JWS JSON, but _before verifying the
2021-05-30 22:32:47 -04:00
jws signature_. This allows you to inspect the `JwsHeader` and `Claims` arguments for any information that can
help you look up the `Key` to use for verifying _that specific jws_ . This is very powerful for applications
2018-07-30 19:07:17 -04:00
with more complex security models that might use different keys at different times or for different users or customers.
Which data might you inspect?
2021-05-30 22:32:47 -04:00
The JWT specification's supported way to do this is to set a `kid` (Key ID) field in the JWS header when the JWS is
2018-07-30 19:07:17 -04:00
being created, for example:
```java
Key signingKey = getSigningKey();
String keyId = getKeyId(signingKey); //any mechanism you have to associate a key with an ID is fine
String jws = Jwts.builder()
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
.setHeaderParam(JwsHeader.KEY_ID, keyId) // 1
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
.signWith(signingKey) // 2
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
.compact();
```
Then during parsing, your `SigningKeyResolver` can inspect the `JwsHeader` to get the `kid` and then use that value
2021-05-30 22:32:47 -04:00
to look up the key from somewhere, like a database. For example:
2018-07-30 19:07:17 -04:00
```java
public class MySigningKeyResolver extends SigningKeyResolverAdapter {
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
@Override
public Key resolveSigningKey(JwsHeader jwsHeader, Claims claims) {
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
//inspect the header or claims, lookup and return the signing key
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
String keyId = jwsHeader.getKeyId(); //or any other field that you need to inspect
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
Key key = lookupVerificationKey(keyId); //implement me
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
return key;
}
}
```
2021-05-30 22:32:47 -04:00
Note that inspecting the `jwsHeader.getKeyId()` is just the most common approach to look up a key - you could
inspect any number of header fields or claims to determine how to lookup the verification key. It is all based on
2018-07-30 19:07:17 -04:00
how the JWS was created.
2021-05-30 22:32:47 -04:00
Finally remember that for HMAC algorithms, the returned verification key should be a `SecretKey` , and for asymmetric
2018-07-30 19:07:17 -04:00
algorithms, the key returned should be a `PublicKey` (not a `PrivateKey` ).
< a name = "jws-read-claims" > < / a >
#### Claim Assertions
2021-05-30 22:32:47 -04:00
You can enforce that the JWS you are parsing conforms to expectations that you require and are important for your
2018-07-30 19:07:17 -04:00
application.
For example, let's say that you require that the JWS you are parsing has a specific `sub` (subject) value,
2021-05-30 22:32:47 -04:00
otherwise you may not trust the token. You can do that by using one of the various `require` * methods on the
2020-02-05 15:08:18 -05:00
`JwtParserBuilder` :
2018-07-30 19:07:17 -04:00
```java
try {
2020-02-05 15:08:18 -05:00
Jwts.parserBuilder().requireSubject("jsmith").setSigningKey(key).build().parseClaimsJws(s);
2018-07-30 19:07:17 -04:00
} catch(InvalidClaimException ice) {
// the sub field was missing or did not have a 'jsmith' value
}
```
2021-05-30 22:32:47 -04:00
If it is important to react to a missing vs an incorrect value, instead of catching `InvalidClaimException` ,
2018-07-30 19:07:17 -04:00
you can catch either `MissingClaimException` or `IncorrectClaimException` :
```java
try {
2020-02-05 15:08:18 -05:00
Jwts.parserBuilder().requireSubject("jsmith").setSigningKey(key).build().parseClaimsJws(s);
2018-07-30 19:07:17 -04:00
} catch(MissingClaimException mce) {
// the parsed JWT did not have the sub field
} catch(IncorrectClaimException ice) {
// the parsed JWT had a sub field, but its value was not equal to 'jsmith'
}
```
You can also require custom fields by using the `require(fieldName, requiredFieldValue)` method - for example:
```java
try {
2020-02-05 15:08:18 -05:00
Jwts.parserBuilder().require("myfield", "myRequiredValue").setSigningKey(key).build().parseClaimsJws(s);
2018-07-30 19:07:17 -04:00
} catch(InvalidClaimException ice) {
// the 'myfield' field was missing or did not have a 'myRequiredValue' value
}
```
(or, again, you could catch either `MissingClaimException` or `IncorrectClaimException` instead).
2021-05-30 22:32:47 -04:00
Please see the `JwtParserBuilder` class and/or JavaDoc for a full list of the various `require` * methods you may use for claims
2018-07-30 19:07:17 -04:00
assertions.
< a name = "jws-read-clock" > < / a >
#### Accounting for Clock Skew
2021-05-30 22:32:47 -04:00
When parsing a JWT, you might find that `exp` or `nbf` claim assertions fail (throw exceptions) because the clock on
the parsing machine is not perfectly in sync with the clock on the machine that created the JWT. This can cause
2018-07-30 19:07:17 -04:00
obvious problems since `exp` and `nbf` are time-based assertions, and clock times need to be reliably in sync for shared
assertions.
2020-02-05 15:08:18 -05:00
You can account for these differences (usually no more than a few minutes) when parsing using the `JwtParserBuilder` 's
2021-05-30 22:32:47 -04:00
`setAllowedClockSkewSeconds` . For example:
2018-07-30 19:07:17 -04:00
```java
long seconds = 3 * 60; //3 minutes
2020-02-05 15:08:18 -05:00
Jwts.parserBuilder()
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
.setAllowedClockSkewSeconds(seconds) // < ----
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// ... etc ...
2020-02-05 15:08:18 -05:00
.build()
2018-07-30 19:07:17 -04:00
.parseClaimsJws(jwt);
```
2021-05-30 22:32:47 -04:00
This ensures that clock differences between the machines can be ignored. Two or three minutes should be more than
enough; it would be fairly strange if a production machine's clock was more than 5 minutes difference from most
2018-07-30 19:07:17 -04:00
atomic clocks around the world.
< a name = "jws-read-clock-custom" > < / a >
##### Custom Clock Support
If the above `setAllowedClockSkewSeconds` isn't sufficient for your needs, the timestamps created
2021-05-30 22:32:47 -04:00
during parsing for timestamp comparisons can be obtained via a custom time source. Call the `JwtParserBuilder` 's `setClock`
method with an implementation of the `io.jsonwebtoken.Clock` interface. For example:
```java
2018-07-30 19:07:17 -04:00
Clock clock = new MyClock();
2020-02-05 15:08:18 -05:00
Jwts.parserBuilder().setClock(myClock) //... etc ...
2021-05-30 22:32:47 -04:00
```
2018-07-30 19:07:17 -04:00
2021-05-30 22:32:47 -04:00
The `JwtParser` 's default `Clock` implementation simply returns `new Date()` to reflect the time when parsing occurs,
as most would expect. However, supplying your own clock could be useful, especially when writing test cases to
2018-07-30 19:07:17 -04:00
guarantee deterministic behavior.
< a name = "jws-read-decompression" > < / a >
#### JWS Decompression
2020-02-05 15:08:18 -05:00
If you used JJWT to compress a JWS and you used a custom compression algorithm, you will need to tell the `JwtParserBuilder`
2018-07-30 19:07:17 -04:00
how to resolve your `CompressionCodec` to decompress the JWT.
Please see the [Compression ](#compression ) section below to see how to decompress JWTs during parsing.
<!-- TODO: ## Encrypted JWTs -->
< a name = "compression" > < / a >
## Compression
**The JWT specification only standardizes this feature for JWEs (Encrypted JWTs) and not JWSs (Signed JWTs), however
2021-05-30 22:32:47 -04:00
JJWT supports both**. If you are positive that a JWS you create with JJWT will _also_ be parsed with JJWT, you
can use this feature with JWSs, otherwise it is best to only use it for JWEs.
2018-07-30 19:07:17 -04:00
2021-05-30 22:32:47 -04:00
If a JWT's Claims set is sufficiently large - that is, it contains a lot of name/value pairs, or individual values are
2018-07-30 19:07:17 -04:00
very large or verbose - you can reduce the size of the created JWS by compressing the claims body.
2021-05-30 22:32:47 -04:00
This might be important to you if the resulting JWS is used in a URL for example, since URLs are best kept under
4096 characters due to browser, user mail agent, or HTTP gateway compatibility issues. Smaller JWTs also help reduce
2018-07-30 19:07:17 -04:00
bandwidth utilization, which may or may not be important depending on your application's volume or needs.
2021-05-30 22:32:47 -04:00
If you want to compress your JWT, you can use the `JwtBuilder` 's `compressWith(CompressionAlgorithm)` method. For
2018-07-30 19:07:17 -04:00
example:
```java
Jwts.builder()
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
.compressWith(CompressionCodecs.DEFLATE) // or CompressionCodecs.GZIP
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// .. etc ...
```
2021-05-30 22:32:47 -04:00
If you use the `DEFLATE` or `GZIP` Compression Codecs - that's it, you're done. You don't have to do anything during
2018-07-30 19:07:17 -04:00
parsing or configure the `JwtParser` for compression - JJWT will automatically decompress the body as expected.
< a name = "compression-custom" > < / a >
### Custom Compression Codec
If however, you used your own custom compression codec when creating the JWT (via `JwtBuilder` `compressWith` ), then
2021-05-30 22:32:47 -04:00
you need to supply the codec to the `JwtParserBuilder` using the `setCompressionCodecResolver` method. For example:
2018-07-30 19:07:17 -04:00
```java
CompressionCodecResolver ccr = new MyCompressionCodecResolver();
2020-02-05 15:08:18 -05:00
Jwts.parserBuilder()
2018-07-30 19:07:17 -04:00
.setCompressionCodecResolver(ccr) // < ----
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// .. etc ...
```
Typically a `CompressionCodecResolver` implementation will inspect the `zip` header to find out what algorithm was
2021-05-30 22:32:47 -04:00
used and then return a codec instance that supports that algorithm. For example:
2018-07-30 19:07:17 -04:00
```java
public class MyCompressionCodecResolver implements CompressionCodecResolver {
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
@Override
public CompressionCodec resolveCompressionCodec(Header header) throws CompressionException {
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
String alg = header.getCompressionAlgorithm();
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
CompressionCodec codec = getCompressionCodec(alg); //implement me
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
return codec;
2016-07-02 17:09:22 -04:00
}
2018-07-30 19:07:17 -04:00
}
```
2016-06-17 18:11:08 -04:00
2018-07-30 19:07:17 -04:00
< a name = "json" > < / a >
## JSON Support
2014-09-18 22:14:22 -04:00
2021-05-30 22:32:47 -04:00
A `JwtBuilder` will serialize the `Header` and `Claims` maps (and potentially any Java objects they
contain) to JSON with a `Serializer<Map<String, ?>>` instance. Similarly, a `JwtParser` will
2018-07-30 19:07:17 -04:00
deserialize JSON into the `Header` and `Claims` using a `Deserializer<Map<String, ?>>` instance.
2014-09-18 22:14:22 -04:00
2021-05-30 22:32:47 -04:00
If you don't explicitly configure a `JwtBuilder` 's `Serializer` or a `JwtParserBuilder` 's `Deserializer` , JJWT will
2018-07-30 19:07:17 -04:00
automatically attempt to discover and use the following JSON implementations if found in the runtime classpath.
They are checked in order, and the first one found is used:
2014-09-26 16:32:19 -04:00
2021-05-30 22:32:47 -04:00
1. Jackson: This will automatically be used if you specify `io.jsonwebtoken:jjwt-jackson` as a project runtime
dependency. Jackson supports POJOs as claims with full marshaling/unmarshaling as necessary.
2020-02-05 15:08:18 -05:00
2. Gson: This will automatically be used if you specify `io.jsonwebtoken:jjwt-gson` as a project runtime dependency.
2021-05-30 22:32:47 -04:00
Gson also supports POJOs as claims with full marshaling/unmarshaling as necessary.
3. JSON-Java (`org.json`): This will be used automatically if you specify `io.jsonwebtoken:jjwt-orgjson` as a
2018-07-30 19:07:17 -04:00
project runtime dependency.
2021-05-30 22:32:47 -04:00
**NOTE:** `org.json` APIs are natively enabled in Android environments so this is the recommended JSON processor for
Android applications _unless_ you want to use POJOs as claims. The `org.json` library supports simple
Object-to-JSON marshaling, but it *does not* support JSON-to-Object unmarshalling.
**If you want to use POJOs as claim values, use either the `io.jsonwebtoken:jjwt-jackson` or
`io.jsonwebtoken:jjwt-gson` dependency** (or implement your own Serializer and Deserializer if desired). **But beware** ,
Jackson will force a sizable (> 1 MB) dependency to an Android application thus increasing the app download size for
2020-02-05 15:08:18 -05:00
mobile users.
2018-07-30 19:07:17 -04:00
< a name = "json-custom" > < / a >
### Custom JSON Processor
2021-05-30 22:32:47 -04:00
If you don't want to use JJWT's runtime dependency approach, or just want to customize how JSON serialization and
2018-07-30 19:07:17 -04:00
deserialization works, you can implement the `Serializer` and `Deserializer` interfaces and specify instances of
2021-05-30 22:32:47 -04:00
them on the `JwtBuilder` and `JwtParserBuilder` respectively. For example:
2018-07-30 19:07:17 -04:00
When creating a JWT:
```java
Serializer< Map < String , ? > > serializer = getMySerializer(); //implement me
Jwts.builder()
2015-07-31 20:40:35 -04:00
2018-07-30 19:07:17 -04:00
.serializeToJsonWith(serializer)
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// ... etc ...
```
2016-06-17 17:55:57 -04:00
2018-07-30 19:07:17 -04:00
When reading a JWT:
```java
Deserializer< Map < String , ? > > deserializer = getMyDeserializer(); //implement me
2020-02-05 15:08:18 -05:00
Jwts.parserBuilder()
2018-07-30 19:07:17 -04:00
.deserializeJsonWith(deserializer)
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// ... etc ...
```
< a name = "json-jackson" > < / a >
### Jackson JSON Processor
2020-02-05 15:08:18 -05:00
If you want to use Jackson for JSON processing, just including the `io.jsonwebtoken:jjwt-jackson` dependency as a
runtime dependency is all that is necessary in most projects, since Gradle and Maven will automatically pull in
the necessary Jackson dependencies as well.
2021-05-30 22:32:47 -04:00
After including this dependency, JJWT will automatically find the Jackson implementation on the runtime classpath and
use it internally for JSON parsing. There is nothing else you need to do - JJWT will automatically create a new
2020-02-05 15:08:18 -05:00
Jackson ObjectMapper for its needs as required.
2021-05-30 22:32:47 -04:00
However, if you have an application-wide Jackson `ObjectMapper` (as is typically recommended for most applications),
2020-02-05 15:08:18 -05:00
you can configure JJWT to use your own `ObjectMapper` instead.
2018-07-30 19:07:17 -04:00
2021-05-30 22:32:47 -04:00
You do this by declaring the `io.jsonwebtoken:jjwt-jackson` dependency with **compile** scope (not runtime
scope which is the typical JJWT default). That is:
2018-07-30 19:07:17 -04:00
**Maven**
2016-06-17 17:55:57 -04:00
```xml
< dependency >
2018-07-30 19:07:17 -04:00
< groupId > io.jsonwebtoken< / groupId >
< artifactId > jjwt-jackson< / artifactId >
2020-06-11 14:24:23 -04:00
< version > 0.11.2< / version >
2018-07-30 19:07:17 -04:00
< scope > compile< / scope > <!-- Not runtime -->
2016-06-17 17:55:57 -04:00
< / dependency >
```
2015-07-31 20:40:35 -04:00
2018-08-25 13:22:56 -04:00
**Gradle or Android**
2018-07-30 19:07:17 -04:00
```groovy
dependencies {
2021-05-30 22:57:42 -04:00
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.2'
2018-07-30 19:07:17 -04:00
}
```
And then you can specify the `JacksonSerializer` using your own `ObjectMapper` on the `JwtBuilder` :
```java
ObjectMapper objectMapper = getMyObjectMapper(); //implement me
String jws = Jwts.builder()
.serializeToJsonWith(new JacksonSerializer(objectMapper))
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// ... etc ...
```
2020-02-05 15:08:18 -05:00
and the `JacksonDeserializer` using your `ObjectMapper` on the `JwtParserBuilder` :
2018-07-30 19:07:17 -04:00
```java
ObjectMapper objectMapper = getMyObjectMapper(); //implement me
2020-02-05 15:08:18 -05:00
Jwts.parserBuilder()
2018-07-30 19:07:17 -04:00
.deserializeJsonWith(new JacksonDeserializer(objectMapper))
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// ... etc ...
```
2020-02-05 15:08:18 -05:00
< a name = "json-jackson-custom-types" > < / a >
#### Parsing of Custom Claim Types
2021-05-30 22:32:47 -04:00
By default JJWT will only convert simple claim types: String, Date, Long, Integer, Short and Byte. If you need to deserialize other types you can configure the `JacksonDeserializer` by passing a `Map` of claim names to types in through a constructor. For example:
2020-02-05 15:08:18 -05:00
```java
new JacksonDeserializer(Maps.of("user", User.class).build())
```
2021-05-30 22:32:47 -04:00
This would trigger the value in the `user` claim to be deserialized into the custom type of `User` . Given the claims body of:
2020-02-05 15:08:18 -05:00
```json
{
2021-05-30 22:32:47 -04:00
"issuer": "https://example.com/issuer",
"user": {
"firstName": "Jill",
"lastName": "Coder"
}
2020-02-05 15:08:18 -05:00
}
```
The `User` object could be retrieved from the `user` claim with the following code:
```java
Jwts.parserBuilder()
.deserializeJsonWith(new JacksonDeserializer(Maps.of("user", User.class).build())) // < -----
.build()
.parseClaimsJwt(aJwtString)
.getBody()
2021-05-30 22:32:47 -04:00
2020-02-05 15:08:18 -05:00
.get("user", User.class) // < -----
```
2021-05-30 22:32:47 -04:00
**NOTE:** Using this constructor is mutually exclusive with the `JacksonDeserializer(ObjectMapper)` constructor
[described above ](#json-jackson ). This is because JJWT configures an `ObjectMapper` directly and could have negative
consequences for a shared `ObjectMapper` instance. This should work for most applications, if you need a more advanced
2020-02-05 15:08:18 -05:00
parsing options, [configure the mapper directly ](#json-jackson ).
< a name = "json-gson" > < / a >
### Gson JSON Processor
If you want to use Gson for JSON processing, just including the `io.jsonwebtoken:jjwt-gson` dependency as a
runtime dependency is all that is necessary in most projects, since Gradle and Maven will automatically pull in
the necessary Gson dependencies as well.
2021-05-30 22:32:47 -04:00
After including this dependency, JJWT will automatically find the Gson implementation on the runtime classpath and
use it internally for JSON parsing. There is nothing else you need to do - just declaring the dependency is
2020-02-05 15:08:18 -05:00
all that is required, no code or config is necessary.
If you're curious, JJWT will automatically create an internal default Gson instance for its own needs as follows:
```java
new GsonBuilder().disableHtmlEscaping().create();
```
2021-05-30 22:32:47 -04:00
However, if you prefer to use a different Gson instance instead of JJWT's default, you can configure JJWT to use your
own.
2020-02-05 15:08:18 -05:00
2021-05-30 22:32:47 -04:00
You do this by declaring the `io.jsonwebtoken:jjwt-gson` dependency with **compile** scope (not runtime
scope which is the typical JJWT default). That is:
2020-02-05 15:08:18 -05:00
**Maven**
```xml
< dependency >
< groupId > io.jsonwebtoken< / groupId >
< artifactId > jjwt-gson< / artifactId >
2020-06-11 14:24:23 -04:00
< version > 0.11.2< / version >
2020-02-05 15:08:18 -05:00
< scope > compile< / scope > <!-- Not runtime -->
< / dependency >
```
**Gradle or Android**
```groovy
dependencies {
2021-05-30 22:57:42 -04:00
implementation 'io.jsonwebtoken:jjwt-gson:0.11.2'
2020-02-05 15:08:18 -05:00
}
```
And then you can specify the `GsonSerializer` using your own `Gson` instance on the `JwtBuilder` :
```java
Gson gson = getGson(); //implement me
String jws = Jwts.builder()
.serializeToJsonWith(new GsonSerializer(gson))
2021-05-30 22:32:47 -04:00
2020-02-05 15:08:18 -05:00
// ... etc ...
```
and the `GsonDeserializer` using your `Gson` instance on the `JwtParser` :
```java
Gson gson = getGson(); //implement me
Jwts.parser()
.deserializeJsonWith(new GsonDeserializer(gson))
2021-05-30 22:32:47 -04:00
2020-02-05 15:08:18 -05:00
// ... etc ...
```
2018-07-30 19:07:17 -04:00
< a name = "base64" > < / a >
## Base64 Support
2021-05-30 22:32:47 -04:00
JJWT uses a very fast pure-Java [Base64 ](https://tools.ietf.org/html/rfc4648 ) codec for Base64 and
2018-07-30 19:07:17 -04:00
Base64Url encoding and decoding that is guaranteed to work deterministically in all JDK and Android environments.
2021-05-30 22:32:47 -04:00
You can access JJWT's encoders and decoders using the `io.jsonwebtoken.io.Encoders` and `io.jsonwebtoken.io.Decoders`
2018-07-30 19:07:17 -04:00
utility classes.
`io.jsonwebtoken.io.Encoders` :
2021-05-30 22:32:47 -04:00
* `BASE64` is an RFC 4648 [Base64 ](https://tools.ietf.org/html/rfc4648#section-4 ) encoder
* `BASE64URL` is an RFC 4648 [Base64URL ](https://tools.ietf.org/html/rfc4648#section-5 ) encoder
2018-07-30 19:07:17 -04:00
`io.jsonwebtoken.io.Decoders` :
2021-05-30 22:32:47 -04:00
* `BASE64` is an RFC 4648 [Base64 ](https://tools.ietf.org/html/rfc4648#section-4 ) decoder
* `BASE64URL` is an RFC 4648 [Base64URL ](https://tools.ietf.org/html/rfc4648#section-5 ) decoder
2020-02-04 21:33:36 -05:00
< a name = "base64-security" > < / a >
### Understanding Base64 in Security Contexts
All cryptographic operations, like encryption and message digest calculations, result in binary data - raw byte arrays.
Because raw byte arrays cannot be represented natively in JSON, the JWT
2021-05-30 22:32:47 -04:00
specifications employ the Base64URL encoding scheme to represent these raw byte values in JSON documents or compound
2020-02-04 21:33:36 -05:00
structures like a JWT.
2021-05-30 22:32:47 -04:00
This means that the Base64 and Base64URL algorithms take a raw byte array and converts the bytes into a string suitable
to use in text documents and protocols like HTTP. These algorithms can also convert these strings back
2020-02-04 21:33:36 -05:00
into the original raw byte arrays for decryption or signature verification as necessary.
2021-05-30 22:32:47 -04:00
That's nice and convenient, but there are two very important properties of Base64 (and Base64URL) text strings that
2020-02-04 21:33:36 -05:00
are critical to remember when they are used in security scenarios like with JWTs:
2021-05-30 22:32:47 -04:00
* [Base64 is not encryption ](#base64-not-encryption )
* [Changing Base64 characters ](#base64-changing-characters ) **does not automatically invalidate data** .
2020-02-04 21:33:36 -05:00
< a name = "base64-not-encryption" > < / a >
#### Base64 is not encryption
2021-05-30 22:32:47 -04:00
Base64-encoded text is _not_ encrypted.
2020-02-04 21:33:36 -05:00
2021-05-30 22:32:47 -04:00
While a byte array representation can be converted to text with the Base64 algorithms,
anyone in the world can take Base64-encoded text, decode it with any standard Base64 decoder, and obtain the
underlying raw byte array data. No key or secret is required to decode Base64 text - anyone can do it.
2020-02-04 21:33:36 -05:00
Based on this, when encoding sensitive byte data with Base64 - like a shared or private key - **the resulting
string NOT is safe to expose publicly**.
A base64-encoded key is still sensitive information and must
2021-05-30 22:32:47 -04:00
be kept as secret and as safe as the original thing you got the bytes from (e.g. a Java `PrivateKey` or `SecretKey`
2020-02-04 21:33:36 -05:00
instance).
2021-05-30 22:32:47 -04:00
After Base64-encoding data into a string, it is possible to then encrypt the string to keep it safe from prying
eyes if desired, but this is different. Encryption is not encoding. They are separate concepts.
2020-02-04 21:33:36 -05:00
< a name = "base64-changing-characters" > < / a >
#### Changing Base64 Characters
In an effort to see if signatures or encryption is truly validated correctly, some try to edit a JWT
string - particularly the Base64-encoded signature part - to see if the edited string fails security validations.
This conceptually makes sense: change the signature string, you would assume that signature validation would fail.
_But this doesn't always work. Changing base64 characters is an invalid test_.
Why?
2021-05-30 22:32:47 -04:00
Because of the way the Base64 algorithm works, there are multiple Base64 strings that can represent the same raw byte
2020-02-04 21:33:36 -05:00
array.
2021-05-30 22:32:47 -04:00
Going into the details of the Base64 algorithm is out of scope for this documentation, but there are many good
2020-02-04 21:33:36 -05:00
Stackoverflow [answers ](https://stackoverflow.com/questions/33663113/multiple-strings-base64-decoded-to-same-byte-array?noredirect=1&lq=1 )
and [JJWT issue comments ](https://github.com/jwtk/jjwt/issues/211#issuecomment-283076269 ) that explain this in detail.
Here's one [good answer ](https://stackoverflow.com/questions/29941270/why-do-base64-decode-produce-same-byte-array-for-different-strings ):
2021-05-30 22:32:47 -04:00
> Remember that Base64 encodes each 8 bit entity into 6 bit chars. The resulting string then needs exactly
> 11 * 8 / 6 bytes, or 14 2/3 chars. But you can't write partial characters. Only the first 4 bits (or 2/3 of the
2020-02-04 21:33:36 -05:00
> last char) are significant. The last two bits are not decoded. Thus all of:
>
> dGVzdCBzdHJpbmo
> dGVzdCBzdHJpbmp
> dGVzdCBzdHJpbmq
> dGVzdCBzdHJpbmr
> All decode to the same 11 bytes (116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 106).
2021-05-30 22:32:47 -04:00
As you can see by the above 4 examples, they all decode to the same exact 11 bytes. So just changing one or two
2020-02-04 21:33:36 -05:00
characters at the end of a Base64 string may not work and can often result in an invalid test.
< a name = "base64-invalid-characters" > < / a >
##### Adding Invalid Characters
2021-05-30 22:32:47 -04:00
JJWT's default Base64/Base64URL decoders automatically ignore illegal Base64 characters located in the beginning and
end of an encoded string. Therefore prepending or appending invalid characters like `{` or `]` or similar will also
not fail JJWT's signature checks either. Why?
2020-02-04 21:33:36 -05:00
2021-05-30 22:32:47 -04:00
Because such edits - whether changing a trailing character or two, or appending invalid characters - do not actually
change the _real_ signature, which in cryptographic contexts, is always a byte array. Instead, tests like these
2020-02-04 21:33:36 -05:00
change a text encoding of the byte array, and as we covered above, they are different things.
So JJWT 'cares' more about the real byte array and less about its text encoding because that is what actually matters
2021-05-30 22:32:47 -04:00
in cryptographic operations. In this sense, JJWT follows the [Robustness Principle ](https://en.wikipedia.org/wiki/Robustness_principle )
in being _slightly_ lenient on what is accepted per the rules of Base64, but if anything in the real underlying
2020-02-04 21:33:36 -05:00
byte array is changed, then yes, JJWT's cryptographic assertions will definitely fail.
2021-05-30 22:32:47 -04:00
To help understand JJWT's approach, we have to remember why signatures exist. From our documentation above on
2020-02-04 21:33:36 -05:00
[signing JWTs ](#jws ):
2021-05-30 22:32:47 -04:00
> * guarantees it was created by someone we know (it is authentic), as well as
> * guarantees that no-one has manipulated or changed it after it was created (its integrity is maintained).
2020-02-04 21:33:36 -05:00
2021-05-30 22:32:47 -04:00
Just prepending or appending invalid text to try to 'trick' the algorithm doesn't change the integrity of the
underlying claims or signature byte arrays, nor the authenticity of the claims byte array, because those byte
2020-02-04 21:33:36 -05:00
arrays are still obtained intact.
2021-05-30 22:32:47 -04:00
Please see [JJWT Issue #518 ](https://github.com/jwtk/jjwt/issues/518 ) and its referenced issues and links for more
2020-02-04 21:33:36 -05:00
information.
2018-07-30 19:07:17 -04:00
< a name = "base64-custom" > < / a >
### Custom Base64
If for some reason you want to specify your own Base64Url encoder and decoder, you can use the `JwtBuilder`
`base64UrlEncodeWith` method to set the encoder:
```java
Encoder< byte [ ] , String > base64UrlEncoder = getMyBase64UrlEncoder(); //implement me
String jws = Jwts.builder()
.base64UrlEncodeWith(base64UrlEncoder)
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// ... etc ...
```
2020-02-05 15:08:18 -05:00
and the `JwtParserBuilder` 's `base64UrlDecodeWith` method to set the decoder:
2018-07-30 19:07:17 -04:00
```java
Decoder< String , byte [ ] > base64UrlDecoder = getMyBase64UrlDecoder(); //implement me
2020-02-05 15:08:18 -05:00
Jwts.parserBuilder()
2018-07-30 19:07:17 -04:00
.base64UrlDecodeWith(base64UrlEncoder)
2021-05-30 22:32:47 -04:00
2018-07-30 19:07:17 -04:00
// ... etc ...
```
## Learn More
- [JSON Web Token for Java and Android ](https://stormpath.com/blog/jjwt-how-it-works-why/ )
- [How to Create and Verify JWTs in Java ](https://stormpath.com/blog/jwt-java-create-verify/ )
- [Where to Store Your JWTs - Cookies vs HTML5 Web Storage ](https://stormpath.com/blog/where-to-store-your-jwts-cookies-vs-html5-web-storage/ )
- [Use JWT the Right Way! ](https://stormpath.com/blog/jwt-the-right-way/ )
- [Token Authentication for Java Applications ](https://stormpath.com/blog/token-auth-for-java/ )
- [JJWT Changelog ](CHANGELOG.md )
2016-03-15 19:16:18 -04:00
## Author
2021-06-23 12:24:29 -04:00
Maintained by Les Hazlewood & the community :heart:
2016-03-15 19:16:18 -04:00
2018-07-30 19:07:17 -04:00
< a name = "license" > < / a >
## License
2015-07-31 20:40:35 -04:00
2016-06-17 18:11:08 -04:00
This project is open-source via the [Apache 2.0 License ](http://www.apache.org/licenses/LICENSE-2.0 ).