-
Hello friends, This is my code: package com.tkg.MasterSystem.services.impl;
import com.tkg.MasterSystem.payload.requests.authentication.SendSignInCodeRequest;
import com.tkg.MasterSystem.repositories.UserRepositoryInterface;
import com.tkg.MasterSystem.services.AuthenticationServiceInterface;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import javax.crypto.SecretKey;
import java.security.Key;
import java.util.Date;
import java.util.Objects;
@Service
public class AuthenticationService implements AuthenticationServiceInterface {
@Autowired
Environment environment;
private final UserRepositoryInterface userRepository;
public Key getSecretKey(){
byte[] keyBytes = Decoders.BASE64.decode(environment.getProperty("JWT_SECRET_KET"));
return Keys.hmacShaKeyFor(keyBytes);
}
@Autowired
public AuthenticationService(UserRepositoryInterface userRepositoryInterface) {
this.userRepository = userRepositoryInterface;
}
@Override
public boolean sendSignUpCode(SendSignInCodeRequest sendSignInCodeRequest) {
return userRepository.existsByEmail(sendSignInCodeRequest.getEmail());
}
@Override
public Claims extractAllClaims(String token) {
return Jwts
.parserBuilder()
.setSigningKey(getSecretKey())
.build()
.parseClaimsJws(token)
.getBody();
}
@Override
public String generateToken( UserDetails userDetails) {
return Jwts
.builder()
.setSubject(userDetails.getUsername())
.setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 24 * 90))
.signWith(getSecretKey(),SignatureAlgorithm.HS256)
.compact();
}
@Override
public boolean isTokenValid(String token, UserDetails userDetails) {
Claims claims = extractAllClaims(token);
final String username = claims.getSubject();
Date tokenExpiration = claims.getExpiration();
return username.equals(userDetails.getUsername()) && tokenExpiration.before(new Date());
}
} When I running my application and send any request, i get an error |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
Are you using the latest stable release using |
Beta Was this translation helpful? Give feedback.
-
Do you have a simple FWIW, in your Date tokenExpiration = claims.getExpiration();
return username.equals(userDetails.getUsername()) && tokenExpiration.before(new Date()); If the token is expired, an |
Beta Was this translation helpful? Give feedback.
-
Without a |
Beta Was this translation helpful? Give feedback.
-
The following test class works without any problems, errors or exceptions no matter how many times I run it, using your Because of this, I suspect your problem is related to your application or environment, mostly likely that the JWT_SECRET_KEY has been changed. In other words (for example):
This is just a guess, because I'm unable to reproduce any errors. See below: 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;
public class JjwtDiscussion821 {
private static final String SECRET_KEY_BASE64 = "671491AE98362741F722202EED3288E8FF2508B35315ADBF75EEB3195A926B40";
public Key getSecretKey() {
byte[] keyBytes = Decoders.BASE64.decode(SECRET_KEY_BASE64);
return Keys.hmacShaKeyFor(keyBytes);
}
public Claims extractAllClaims(String token) {
return Jwts
.parserBuilder()
.setSigningKey(getSecretKey())
.build()
.parseClaimsJws(token)
.getBody();
}
public String generateToken(String username) {
return Jwts
.builder()
.setSubject(username)
.setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 24 * 90))
.signWith(getSecretKey(), SignatureAlgorithm.HS256)
.compact();
}
public boolean isTokenValid(String token, String username) {
Claims claims = extractAllClaims(token);
final String subject = claims.getSubject();
return username.equals(subject);
}
public static void main(String[] args) {
JjwtDiscussion821 test = new JjwtDiscussion821();
String username = "testUser";
for(int i = 0; i < 500; i++) {
String jws = test.generateToken(username);
assert test.isTokenValid(jws, username);
}
}
} |
Beta Was this translation helpful? Give feedback.
@lhazlewood sorry, I found the reason, because my postman already had an authorization token. Thank you for your time lol.