@@ -13,6 +13,7 @@ This repository is an open-source Java library for fast and convenient using of
1313* [ How to use] ( #how-to-use )
1414 * [ Instantiate a service] ( #instantiate-a-service )
1515 * [ Persist tokens] ( #persist-tokens )
16+ * [ Invalidate token] ( #invalidate-jwt-token )
1617 * [ Create token] ( #create-jwt-token )
1718 * [ If token is expired] ( #check-if-jwt-token-is-expired )
1819 * [ If token has claim] ( #check-if-jwt-token-has-claim )
@@ -31,7 +32,7 @@ With Maven add dependency to your `pom.xml`.
3132<dependency >
3233 <groupId >io.github.javacoded78</groupId >
3334 <artifactId >jwt-humble</artifactId >
34- <version >0.1 .0</version >
35+ <version >0.2 .0</version >
3536</dependency >
3637```
3738
@@ -55,7 +56,7 @@ After, you can call available methods and use library.
5556
5657### Persist tokens
5758
58- Library supports ` PersistentTokenServiceImpl ` implementation with saving tokens to ` TokenStorage ` .
59+ Library supports ` PersistentTokenService ` implementation with saving tokens to ` TokenStorage ` .
5960
6061With such approach you can store tokens in Redis or in-memory Map and create new one if no specified tokens exist,
6162otherwise, stored JWT token would be returned.
@@ -76,18 +77,17 @@ public class Main {
7677
7778With Redis you need to pass ` RedisTokenStorageImpl ` object to constructor.
7879
79- To create ` RedisTokenStorageImpl ` you need to pass JedisPool
80+ To create ` RedisTokenStorageImpl ` you need to pass ` JedisPool ` / host and port / host, port, username and password.
8081
8182``` java
8283public class Main {
83- public static void main (String [] args ) {
84- String secret = " aGZiYmtiYWllYmNpZWFpZWJsZWNldWNlY2xhZWNhaWJlbGNhZWN3Q0VCV0VXSUM=" ;
85- JedisPoolConfig config = new JedisPoolConfig ();
86- config. setJmxEnabled(false );
87- JedisPool jedisPool = new JedisPool (config, " localhost" , 6379 );
88- TokenStorage tokenStorage = new RedisTokenStorageImpl (jedisPool);
89- TokenService tokenService = new PersistentTokenServiceImpl (secret, tokenStorage);
90- }
84+ public static void main (String [] args ) {
85+ String secret = " aGZiYmtiYWllYmNpZWFpZWJsZWNldWNlY2xhZWNhaWJlbGNhZWN3Q0VCV0VXSUM=" ;
86+ String host = " localhost" ;
87+ int port = 6379 ;
88+ TokenStorage tokenStorage = new RedisTokenStorageImpl (host, port);
89+ PersistentTokenService tokenService = new PersistentTokenServiceImpl (secret, tokenStorage);
90+ }
9191}
9292```
9393
@@ -96,6 +96,39 @@ Just pass it as argument in `RedisTokenStorageImpl` constructor.
9696
9797By default, library uses key ` "tokens:" + subject + ":" + type ` .
9898
99+ ### Invalidate JWT token
100+
101+ With ` PersistentTokenService ` you can invalidate token by token itself or by subject and token type.
102+ If first option is chosen, all keys with such token values will be deleted.
103+
104+ If token will be deleted from storage you receive ` true ` .
105+
106+ ``` java
107+ public class Main {
108+ public static void main (String [] args ) {
109+ String token = " eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhbmRyb3Nvcjk5QGdtYWlsLmNvbSIsImlkIjozLCJyb2xlcyI6WyJST0xFX1VTRVIiXSwiZXhwIjoxNzE3MTQ1MDIxfQ.w8ZFLFsKf7Qs9_dNb0WzdoyAIpWtfeEyqLfNI_G16_6NHbGwCRbeVVm_a_DzckytsyGYHTWRlZdi_gWK-HjrXg" ;
110+ boolean deleted = persistentTokenService. invalidate(token);
111+ System . out. println(deleted);
112+ }
113+ }
114+ ```
115+
116+ ``` java
117+ public class Main {
118+ public static void main (String [] args ) {
119+ boolean deleted = persistentTokenService. invalidate(
120+ TokenParameters . builder(
121+ 122+ " access" ,
123+ Duration . of(1 , ChronoUnit . HOURS )
124+ )
125+ .build()
126+ );
127+ System . out. println(deleted);
128+ }
129+ }
130+ ```
131+
99132### Create JWT token
100133
101134To create token call method ` create(TokenParameters params) ` on ` TokenService ` object.
@@ -104,7 +137,7 @@ To create token call method `create(TokenParameters params)` on `TokenService` o
104137public class Main {
105138 public static void main (String [] args ) {
106139 String token = tokenService. create(
107- TokenParameters . builder(
" [email protected] " ,
Duration . of(
1 ,
ChronoUnit . HOURS))
140+ TokenParameters . builder(
" [email protected] " ,
" access " , Duration . of(
1 ,
ChronoUnit . HOURS))
108141 .build()
109142 );
110143 System . out. println(token);
@@ -135,6 +168,18 @@ class Main {
135168 }
136169}
137170```
171+ You can also check expiration with any other date.
172+
173+ ``` java
174+ class Main {
175+ public static void main (String [] args ) {
176+ String token = " eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhbmRyb3Nvcjk5QGdtYWlsLmNvbSIsImlkIjozLCJyb2xlcyI6WyJST0xFX1VTRVIiXSwiZXhwIjoxNzE3MTQ1MDIxfQ.w8ZFLFsKf7Qs9_dNb0WzdoyAIpWtfeEyqLfNI_G16_6NHbGwCRbeVVm_a_DzckytsyGYHTWRlZdi_gWK-HjrXg" ;
177+ Date date = new Date (1705911211182 );
178+ boolean expired = tokenService. isExpired(token, date);
179+ System . out. println(expired);
180+ }
181+ }
182+ ```
138183
139184### Check if JWT token has claim
140185
0 commit comments