Add the Dependencies
<properties>
<jjwt.version>0.11.5</jjwt.version> <!-- Add this tag as part of the properties -->
</properties>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>${jjwt.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>${jjwt.version}</version>
<scope>runtime</scope>
</dependency>
Implementation Example:
private String secret;
public String createJwtToken(TokenPayload tokenPayload) {
return Jwts.builder()
.setClaims(tokenPayload.getClaims())
.setSubject(tokenPayload.getSubject())
.setIssuedAt(tokenPayload.getIssuedAt())
.setExpiration(tokenPayload.getExpiration())
.signWith(getSignatureKey(), SignatureAlgorithm.HS512)
.compact();
}
private Key getSignatureKey() {
return Keys.hmacShaKeyFor(Decoders.BASE64.decode(secret));
}