- JWTHelper:
- public String generateToken(String payload, Key key) {
- return builderFactory
- .createJwtBuilder()
- .setPayload(payload)
- .setHeaderParam("typ", "JWT")
- .setHeaderParam("alg", signatureAlgorithm.getValue())
- .signWith(key, signatureAlgorithm)
- .compact();
- }
- Caller statement: OAuthService
- String strPrivateKey = encryptionUtilities.aesDecrypt(toolConfiguration.getPlatformPrivateKey(),
- aesSecretKey);
- PrivateKey privateKey = encryptionUtilities.string2PrivateKey(strPrivateKey);
- long startTime = System.currentTimeMillis();
- String accessToken = jwtHelper.generateToken(payload, privateKey, tokenExpirationInMilliSeconds);
- LOGGER.info("Time spend in getOauthToken jwtHelper.generateToken {}", System.currentTimeMillis() - startTime);
- EncryptionUtil.java
- public PrivateKey string2PrivateKey(String privateKeyString) {
- long startTime = System.currentTimeMillis();
- try {
- KeyFactory kf = KeyFactory.getInstance(ServiceConstants.RSA);
- PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyString));
- PrivateKey privateKey = kf.generatePrivate(keySpecPKCS8);
- LOGGER.info("Time spend in string2PrivateKey {}", System.currentTimeMillis() - startTime);
- return privateKey;
- } catch (IllegalArgumentException | InvalidKeySpecException | NoSuchAlgorithmException e) {
- LOGGER.error(e.getMessage(), e);
- return null;
- }
- }
- -----------------------
- public Claims readPayloadFromToken(String jwtToken, Key publickey) {
- Claims claims = null;
- try {
- claims = parserFactory
- .createJwtParser()
- .setSigningKey(publickey)
- .parseClaimsJws(jwtToken)
- .getBody();
- } catch (UnsupportedJwtException | MalformedJwtException | ExpiredJwtException | IllegalArgumentException ex) {
- LOGGER.error("Error in JWT/JWS validation", ex);
- }
- return claims;
- }
- ----------------------------
- Gradle Dependency:
- /* JWT Dependency */
- compile "io.jsonwebtoken:jjwt-api:0.10.5"