저는 스프링 - MVC 응용 프로그램에서 작업 중이며 암호 재설정 프로그램에서 작업하고 있습니다. 그 안에는 emailid : timestamp : 토큰으로 암호화 된 secretkey가있는 수동으로 만든 토큰을 보내고 있습니다. 토큰을 확인하는 동안 24 시간 미만으로 계산 된 시간을 확인하는 데 실패합니다. 내 코드를 게시하고 있습니다. 친절하게 내가 뭘 잘못 알려 :타임 스탬프 해상도가 실패합니다.
이ServiceClass :
@Service
public class PersonServiceImpl implements PersonService {
private static final String HMAC_ALGO = "HmacSHA256";
private static final String TOKEN_SEPARATOR = ":";
private static final long MAX_AGE = 1_000 * 60 * 60 * 24; // 24h
private static final String signKey = "secretvalue";
@Override
public void createToken(String username){
long timestamp = System.currentTimeMillis();
StringBuilder sb = new StringBuilder();
sb.append(generateTokenStringPublicPart(username, timestamp));
sb.append(TOKEN_SEPARATOR);
try {
sb.append(computeSignature(username, timestamp, signKey));
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
private static String generateTokenStringPublicPart(String username, long timestamp) {
StringBuilder sb = new StringBuilder();
sb.append(username);
sb.append(TOKEN_SEPARATOR);
sb.append(timestamp);
return sb.toString();
}
private static String computeSignature(String username, long timestamp, String secretKey) throws InvalidKeyException, NoSuchAlgorithmException {
StringBuilder sb = new StringBuilder();
sb.append(generateTokenStringPublicPart(username, timestamp));
SecretKeySpec sks = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), HMAC_ALGO);
Mac hmac = Mac.getInstance(HMAC_ALGO);
hmac.init(sks);
return Base64.encodeBase64URLSafeString(hmac.doFinal(sb.toString().getBytes(StandardCharsets.UTF_8)));
}
public static boolean verifyToken(String token) throws InvalidKeyException, NoSuchAlgorithmException {
String[] parts = token.split(TOKEN_SEPARATOR);
boolean result = false;
if (parts.length == 3) {
String username = parts[0];
System.out.println("username in verify token is"+username);
Long timestamp = Long.valueOf(parts[1]);
System.out.println("Current timestamp of token is"+timestamp);
String signature = parts[2];
if (signature.equals(computeSignature(username, timestamp, signKey))) {
if (System.currentTimeMillis() - timestamp < MAX_AGE) { // It fails here
result = true;
}else {
System.out.println("Timestamp remaining is"+(System.currentTimeMillis() - timestamp));
System.out.println("Time verificaiton failed");
}
}
else {
System.out.println("Signature is not equal");
}
} else{
System.out.println("Token parts are not 3"+parts.length);
}
return result;
}
콘솔 출력 :
Token value is [email protected]:1416659092440:MUYLhJS24EIQv5f-Ak7TXyzWKZSjSFKjcmJkJx2SVew
username in verify token is [email protected]
Current timestamp of token is 1416659092440
Timestamp remaining is 173238774
Time verificaiton failed
Token verification failed
코드를 디버깅하여 관련 부분 만 게시하십시오. –
안녕하세요, 관련성이없는 부분 만 게시했습니다. –
실제로 토큰이 만료되지 않았습니까? 173238774 <86400000 이상에서 실패하고 다른 부분에 착륙하셨습니까? – SMA