Creating a Spring Security Key for Signing a JWT Token

This commit is contained in:
michaelin007 2024-03-12 02:45:48 +00:00
parent 1fea1bf611
commit 89963eefd7
3 changed files with 14 additions and 26 deletions

View File

@ -6,6 +6,7 @@ import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts; import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException; import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.UnsupportedJwtException; import io.jsonwebtoken.UnsupportedJwtException;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys; import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SignatureException; import io.jsonwebtoken.security.SignatureException;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -14,6 +15,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.Key; import java.security.Key;
import java.util.Date; import java.util.Date;
@ -28,6 +30,8 @@ public class JwtUtils {
@Value("${baeldung.app.jwtExpirationMs}") @Value("${baeldung.app.jwtExpirationMs}")
private int jwtExpirationMs; private int jwtExpirationMs;
SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(jwtSecret));
public String generateJwtToken(Authentication authentication) { public String generateJwtToken(Authentication authentication) {
UserDetailsImpl userPrincipal = (UserDetailsImpl) authentication.getPrincipal(); UserDetailsImpl userPrincipal = (UserDetailsImpl) authentication.getPrincipal();
@ -36,7 +40,7 @@ public class JwtUtils {
.subject((userPrincipal.getUsername())) .subject((userPrincipal.getUsername()))
.issuedAt(new Date()) .issuedAt(new Date())
.expiration(new Date((new Date()).getTime() + jwtExpirationMs)) .expiration(new Date((new Date()).getTime() + jwtExpirationMs))
.signWith(getSigningKey()) .signWith(key)
.compact(); .compact();
} }
@ -48,7 +52,7 @@ public class JwtUtils {
public String getUserNameFromJwtToken(String token) { public String getUserNameFromJwtToken(String token) {
return Jwts.parser() return Jwts.parser()
.setSigningKey(getSigningKey()) .verifyWith(key)
.build() .build()
.parseSignedClaims(token) .parseSignedClaims(token)
.getPayload() .getPayload()
@ -59,7 +63,7 @@ public class JwtUtils {
public boolean validateJwtToken(String authToken) { public boolean validateJwtToken(String authToken) {
try { try {
Jwts.parser() Jwts.parser()
.setSigningKey(getSigningKey()) . verifyWith(key)
.build() .build()
.parseSignedClaims(authToken); .parseSignedClaims(authToken);
return true; return true;

View File

@ -27,22 +27,6 @@ public class JwtResponse {
this.type = tokenType; this.type = tokenType;
} }
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUsername() { public String getUsername() {
return username; return username;
} }