Skip to content

Commit

Permalink
naprawione JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
JanisBe committed Nov 18, 2023
1 parent 5bef2ca commit 4c0677e
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/main/java/pl/janis/komornik/config/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@Service
public class JwtUtil {

private final String SECRET_KEY = "topsecret";
private final String SECRET_KEY = "testestestsecretnytestestestsecretnytestestestsecretny";

public String extractUsername(String token) {
return extractClaim(token, Claims::getSubject);
Expand All @@ -33,8 +33,11 @@ public <T> T extractClaim(String token, Function<Claims, T> claimsResolver) {
}

private Claims extractAllClaims(String token) {
SecretKey secret = Keys.hmacShaKeyFor(Decoders.BASE64.decode(SECRET_KEY));
return Jwts.parser().verifyWith(secret).build().parseSignedClaims(token).getPayload();
return Jwts.parser()
.verifyWith(getSigningKey())
.build()
.parseSignedClaims(token)
.getPayload();
}

private Boolean isTokenExpired(String token) {
Expand All @@ -46,15 +49,19 @@ public String generateToken(UserDetails userDetails) {
return createToken(claims, userDetails.getUsername());
}

private SecretKey getSigningKey() {
byte[] keyBytes = Decoders.BASE64.decode(SECRET_KEY);
return Keys.hmacShaKeyFor(keyBytes);
}

private String createToken(Map<String, Object> claims, String subject) {
SecretKey secret = Keys.hmacShaKeyFor(Decoders.BASE64.decode(SECRET_KEY));

return Jwts.builder()
.claims(claims)
.subject(subject)
.issuedAt(new Date(System.currentTimeMillis()))
.expiration(new Date(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(365)))
.signWith(secret, Jwts.SIG.HS256).compact();
.signWith(getSigningKey(), Jwts.SIG.HS512).compact();
}

public Boolean validateToken(String token, UserDetails userDetails) {
Expand Down

0 comments on commit 4c0677e

Please sign in to comment.