0% found this document useful (0 votes)
33 views2 pages

JWT Service Impl

This Java class implements JWT (JSON Web Token) functionality for authentication. It can generate tokens signed with a secret key on login by adding claims like the username and expiration time. It also provides methods to validate tokens, extract claims from tokens, and generate refresh tokens with a separate expiration time. The secret key used for signing tokens is read from configuration and can be refreshed without redeploying the application.

Uploaded by

scribd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views2 pages

JWT Service Impl

This Java class implements JWT (JSON Web Token) functionality for authentication. It can generate tokens signed with a secret key on login by adding claims like the username and expiration time. It also provides methods to validate tokens, extract claims from tokens, and generate refresh tokens with a separate expiration time. The secret key used for signing tokens is read from configuration and can be refreshed without redeploying the application.

Uploaded by

scribd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.rootedin.huntinghero.

security;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import java.security.Key;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

@Service
public class JwtServiceImpl
{
@Value("${application.security.jwt.secret-key: null}")
private String secretKey;
@Value("${application.security.jwt.expiration: 12000}")
private long jwtExpiration;
@Value("${application.security.jwt.refresh-token.expiration: 12000}")
private long refreshExpiration;

public String extractUsername(String token) {


return extractClaim(token, Claims::getSubject);
}

public <T> T extractClaim(String token, Function<Claims, T> claimsResolver) {


final Claims claims = extractAllClaims(token);
return claimsResolver.apply(claims);
}

public String generateToken(UserDetails userDetails) {


return generateToken(new HashMap<>(), userDetails);
}

public String generateToken(


Map<String, Object> extraClaims,
UserDetails userDetails
) {
return buildToken(extraClaims, userDetails, jwtExpiration);
}

public String generateRefreshToken(


UserDetails userDetails
) {
return buildToken(new HashMap<>(), userDetails, refreshExpiration);
}

private String buildToken(


Map<String, Object> extraClaims,
UserDetails userDetails,
long expirationMillis
) {
Key key = Keys.hmacShaKeyFor(secretKey.getBytes());
return Jwts
.builder()
.addClaims(extraClaims)
.setSubject(userDetails.getUsername())
.setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() +
expirationMillis))
.signWith(getSignInKey(), SignatureAlgorithm.HS256)
.compact();
}

public boolean isTokenValid(String token, UserDetails userDetails) {


final String username = extractUsername(token);
return (username.equals(userDetails.getUsername())) && !
isTokenExpired(token);
}

private boolean isTokenExpired(String token) {


return extractExpiration(token).before(new Date());
}

private Date extractExpiration(String token) {


return extractClaim(token, Claims::getExpiration);
}

private Claims extractAllClaims(String token) {


return Jwts.parser()
.setSigningKey(getSignInKey())
.build()
.parseClaimsJws(token)
.getBody();
}

private Key getSignInKey() {


byte[] keyBytes = Decoders.BASE64.decode(secretKey);
return Keys.hmacShaKeyFor(keyBytes);
}
}

You might also like