@@ -41,25 +41,26 @@ public String createToken(Authentication authentication) {
4141
4242 String username = authentication .getName ();
4343 Collection <? extends GrantedAuthority > authorities = authentication .getAuthorities ();
44- Claims claims = Jwts .claims ().setSubject (username );
44+ var claimsBuilder = Jwts .claims ().subject (username );
4545 if (!authorities .isEmpty ()) {
46- claims . put (AUTHORITIES_KEY , authorities .stream ().map (GrantedAuthority ::getAuthority ).collect (joining ("," )));
46+ claimsBuilder . add (AUTHORITIES_KEY , authorities .stream ().map (GrantedAuthority ::getAuthority ).collect (joining ("," )));
4747 }
48-
48+
49+ var claims = claimsBuilder .build ();
4950 Date now = new Date ();
5051 Date validity = new Date (now .getTime () + this .jwtProperties .getValidityInMs ());
5152
5253 return Jwts .builder ()
53- .setClaims (claims )
54- .setIssuedAt (now )
55- .setExpiration (validity )
56- .signWith (this .secretKey , SignatureAlgorithm .HS256 )
54+ .claims (claims )
55+ .issuedAt (now )
56+ .expiration (validity )
57+ .signWith (this .secretKey , Jwts . SIG .HS256 )
5758 .compact ();
5859
5960 }
6061
6162 public Authentication getAuthentication (String token ) {
62- Claims claims = Jwts .parserBuilder ().setSigningKey (this .secretKey ).build ().parseClaimsJws (token ).getBody ();
63+ Claims claims = Jwts .parser ().verifyWith (this .secretKey ).build ().parseSignedClaims (token ).getPayload ();
6364
6465 Object authoritiesClaim = claims .get (AUTHORITIES_KEY );
6566
@@ -74,10 +75,10 @@ public Authentication getAuthentication(String token) {
7475 public boolean validateToken (String token ) {
7576 try {
7677 Jws <Claims > claims = Jwts
77- .parserBuilder ().setSigningKey (this .secretKey ).build ()
78- .parseClaimsJws (token );
78+ .parser ().verifyWith (this .secretKey ).build ()
79+ .parseSignedClaims (token );
7980 // parseClaimsJws will check expiration date. No need do here.
80- log .info ("expiration date: {}" , claims .getBody ().getExpiration ());
81+ log .info ("expiration date: {}" , claims .getPayload ().getExpiration ());
8182 return true ;
8283 } catch (JwtException | IllegalArgumentException e ) {
8384 log .error ("Invalid JWT token: {}" , e .getMessage ());
0 commit comments