Программисты JAVA общаются здесь
Anonymous
Проверка срока действия и алгоритма подписи ключа
Сообщение
Anonymous » 25 сен 2024, 23:34
У меня есть следующий код, используемый для декодирования и проверки токена JWT:
Код: Выделить всё
public class JwtTokenDecoder implements JwtDecoder {
private String secretKey = '123456789';
@SneakyThrows
@Override
public Jwt decode(String token) throws JwtException {
JwtParser build = Jwts.parser().verifyWith(getSigningKey()).build();
io.jsonwebtoken.Jwt parse = build.parse(token);
Map claims = JsonHelper.jsonMapper().convertValue(parse.getPayload(), new TypeReference() {
});
claims.put("iat", Instant.ofEpochSecond((long) claims.get("iat")));
claims.put("exp", Instant.ofEpochSecond((long) claims.get("exp")));
return Jwt.withTokenValue(token)
.headers((val) -> val.putAll(parse.getHeader()))
.claims((val) -> val.putAll(claims))
.build();
}
private SecretKey getSigningKey() {
byte[] keyBytes = secretKey.getBytes(StandardCharsets.UTF_8);
return Keys.hmacShaKeyFor(keyBytes);
}
}
Как я могу проверить, что срок действия истекает на основе минимально допустимого значения? Например, я хочу убедиться, что токен не старше 30 дней, и вручную установить значение типа алгоритма.
Подробнее здесь:
https://stackoverflow.com/questions/790 ... -algorithm
1727296474
Anonymous
У меня есть следующий код, используемый для декодирования и проверки токена JWT: [code]public class JwtTokenDecoder implements JwtDecoder { private String secretKey = '123456789'; @SneakyThrows @Override public Jwt decode(String token) throws JwtException { JwtParser build = Jwts.parser().verifyWith(getSigningKey()).build(); io.jsonwebtoken.Jwt parse = build.parse(token); Map claims = JsonHelper.jsonMapper().convertValue(parse.getPayload(), new TypeReference() { }); claims.put("iat", Instant.ofEpochSecond((long) claims.get("iat"))); claims.put("exp", Instant.ofEpochSecond((long) claims.get("exp"))); return Jwt.withTokenValue(token) .headers((val) -> val.putAll(parse.getHeader())) .claims((val) -> val.putAll(claims)) .build(); } private SecretKey getSigningKey() { byte[] keyBytes = secretKey.getBytes(StandardCharsets.UTF_8); return Keys.hmacShaKeyFor(keyBytes); } } [/code] Как я могу проверить, что срок действия истекает на основе минимально допустимого значения? Например, я хочу убедиться, что токен не старше 30 дней, и вручную установить значение типа алгоритма. Подробнее здесь: [url]https://stackoverflow.com/questions/79024676/validate-expire-time-and-signing-key-algorithm[/url]