2121import java .util .Map ;
2222import java .util .Objects ;
2323
24+ import lombok .extern .slf4j .Slf4j ;
2425import org .javatuples .Pair ;
2526import org .json .JSONObject ;
2627
2930import io .jsonwebtoken .io .Encoders ;
3031import io .jsonwebtoken .security .Keys ;
3132
33+ @ Slf4j
3234public class JWTUtil {
35+ private static Map <String , Object > algMap ;
36+ static {
37+ algMap = new HashMap <>();
38+ algMap .put ("HS256" , SignatureAlgorithm .HS256 );
39+ algMap .put ("HS384" , SignatureAlgorithm .HS384 );
40+ algMap .put ("HS512" , SignatureAlgorithm .HS512 );
41+ algMap .put ("RS256" , SignatureAlgorithm .RS256 );
42+ algMap .put ("RS384" , SignatureAlgorithm .RS384 );
43+ algMap .put ("RS512" , SignatureAlgorithm .RS512 );
44+ algMap .put ("ES256" , SignatureAlgorithm .ES256 );
45+ algMap .put ("ES384" , SignatureAlgorithm .ES384 );
46+ algMap .put ("ES512" , SignatureAlgorithm .ES512 );
47+ algMap .put ("PS256" , SignatureAlgorithm .PS256 );
48+ algMap .put ("PS384" , SignatureAlgorithm .PS384 );
49+ algMap .put ("PS512" , SignatureAlgorithm .PS512 );
50+ algMap .put ("" , SignatureAlgorithm .HS256 );
51+ }
52+
3353 public static Pair <String , String > createJWT (String typ , String alg , String userInput ,
3454 String iss , String sub , String aud , boolean iat , long exp ) {
3555
@@ -72,34 +92,8 @@ public static Pair<String, String> createJWT(String typ, String alg, String user
7292 }
7393
7494 private static Key signingKey (String algorithm ) {
75- switch (algorithm ) {
76- case "HS256" :
77- return Keys .secretKeyFor (SignatureAlgorithm .HS256 );
78- case "HS384" :
79- return Keys .secretKeyFor (SignatureAlgorithm .HS384 );
80- case "HS512" :
81- return Keys .secretKeyFor (SignatureAlgorithm .HS512 );
82- case "RS256" :
83- return Keys .secretKeyFor (SignatureAlgorithm .RS256 );
84- case "RS384" :
85- return Keys .secretKeyFor (SignatureAlgorithm .RS384 );
86- case "RS512" :
87- return Keys .secretKeyFor (SignatureAlgorithm .RS512 );
88- case "ES256" :
89- return Keys .secretKeyFor (SignatureAlgorithm .ES256 );
90- case "ES384" :
91- return Keys .secretKeyFor (SignatureAlgorithm .ES384 );
92- case "ES512" :
93- return Keys .secretKeyFor (SignatureAlgorithm .ES512 );
94- case "PS256" :
95- return Keys .secretKeyFor (SignatureAlgorithm .PS256 );
96- case "PS384" :
97- return Keys .secretKeyFor (SignatureAlgorithm .PS384 );
98- case "PS512" :
99- return Keys .secretKeyFor (SignatureAlgorithm .PS512 );
100- default :
101- throw new IllegalArgumentException ("Unsupported algorithm: " + algorithm );
102- }
95+ SignatureAlgorithm signatureAlgorithm = (SignatureAlgorithm ) algMap .get (algorithm );
96+ return Keys .secretKeyFor (signatureAlgorithm );
10397 }
10498
10599 public static Pair <String , String > decodeJWT (String jws , String key ) {
@@ -115,12 +109,12 @@ public static void main(String[] args) {
115109 Pair <String , String > jwtAndKey = createJWT ("JWT" , "HS512" , userInput ,
116110 "rrajesh1979" , "JWT Encoder" , "Hello JWT" , false , 0 );
117111
118- System . out . println ("JWT :" + jwtAndKey .getValue0 ());
119- System . out . println ("Secret Key :" + jwtAndKey .getValue1 ());
112+ log . info ("JWT :{}" , jwtAndKey .getValue0 ());
113+ log . info ("Secret Key :{}" , jwtAndKey .getValue1 ());
120114
121115 Pair <String , String > decodedJwtAndKey = decodeJWT (jwtAndKey .getValue0 (), jwtAndKey .getValue1 ());
122- System . out . println ("Decoded JWT Header:" + decodedJwtAndKey .getValue0 ());
123- System . out . println ("Decoded JWT Key :" + decodedJwtAndKey .getValue1 ());
116+ log . info ("Decoded JWT Header :{}" , decodedJwtAndKey .getValue0 ());
117+ log . info ("Decoded JWT Key :{}" , decodedJwtAndKey .getValue1 ());
124118 }
125119
126120}
0 commit comments