feat: added support for different types of token claims

This commit is contained in:
zihluwang
2025-04-18 19:35:41 +08:00
parent f660f8af59
commit b205698eb1
@@ -227,14 +227,101 @@ public class TokenPayload {
* @throws IllegalStateException if the claim name is a registered claim * @throws IllegalStateException if the claim name is a registered claim
*/ */
public TokenPayload withClaim(String name, String value) { public TokenPayload withClaim(String name, String value) {
if (RegisteredClaims.VALUES.contains(name)) { checkClaimName(name);
throw new IllegalStateException("Please set registered claims with pre-defined methods");
}
this.payload.put(name, value); this.payload.put(name, value);
return this; return this;
} }
/**
* Adds a custom claim to the JWT payload.
* <p>
* Stores a custom key-value pair in the payload, provided the key is not a registered claim.
* Registered claims must be set using their dedicated methods to ensure proper handling.
*
* @param name the name of the custom claim
* @param value the value of the custom claim
* @return this {@link TokenPayload} instance for method chaining
* @throws IllegalStateException if the claim name is a registered claim
*/
public TokenPayload withClaim(String name, Long value) {
checkClaimName(name);
this.payload.put(name, value);
return this;
}
/**
* Adds a custom claim to the JWT payload.
* <p>
* Stores a custom key-value pair in the payload, provided the key is not a registered claim.
* Registered claims must be set using their dedicated methods to ensure proper handling.
*
* @param name the name of the custom claim
* @param value the value of the custom claim
* @return this {@link TokenPayload} instance for method chaining
* @throws IllegalStateException if the claim name is a registered claim
*/
public TokenPayload withClaim(String name, Double value) {
checkClaimName(name);
this.payload.put(name, value);
return this;
}
/**
* Adds a custom claim to the JWT payload.
* <p>
* Stores a custom key-value pair in the payload, provided the key is not a registered claim.
* Registered claims must be set using their dedicated methods to ensure proper handling.
*
* @param name the name of the custom claim
* @param value the value of the custom claim
* @return this {@link TokenPayload} instance for method chaining
* @throws IllegalStateException if the claim name is a registered claim
*/
public TokenPayload withClaim(String name, Boolean value) {
checkClaimName(name);
this.payload.put(name, value);
return this;
}
/**
* Adds a custom claim to the JWT payload.
* <p>
* Stores a custom key-value pair in the payload, provided the key is not a registered claim.
* Registered claims must be set using their dedicated methods to ensure proper handling.
*
* @param name the name of the custom claim
* @param value the value of the custom claim
* @return this {@link TokenPayload} instance for method chaining
* @throws IllegalStateException if the claim name is a registered claim
*/
public TokenPayload withClaim(String name, LocalDateTime value) {
checkClaimName(name);
this.payload.put(name, value);
return this;
}
/**
* Adds a custom claim with null value to the JWT payload.
* <p>
* Stores a custom key-value pair in the payload, provided the key is not a registered claim.
* Registered claims must be set using their dedicated methods to ensure proper handling.
*
* @param name the name of the custom claim
* @return this {@link TokenPayload} instance for method chaining
* @throws IllegalStateException if the claim name is a registered claim
*/
public TokenPayload withNullClaim(String name) {
checkClaimName(name);
this.payload.put(name, null);
return this;
}
/** /**
* Checks if the JWT payload has a valid issuer. * Checks if the JWT payload has a valid issuer.
* <p> * <p>
@@ -261,28 +348,28 @@ public class TokenPayload {
Optional.of(audiences) Optional.of(audiences)
.filter((aud) -> !aud.isEmpty()) .filter((aud) -> !aud.isEmpty())
.ifPresent((aud) -> _payload.put(RegisteredClaims.AUDIENCE, aud)); .ifPresent((aud) -> _payload.put(RegisteredClaims.AUDIENCE, aud));
Optional.ofNullable(subject) Optional.ofNullable(subject)
.filter((sub) -> !sub.isBlank()) .filter((sub) -> !sub.isBlank())
.ifPresent((sub) -> _payload.put(RegisteredClaims.SUBJECT, subject)); .ifPresent((sub) -> _payload.put(RegisteredClaims.SUBJECT, subject));
Optional.ofNullable(expiresAt) Optional.ofNullable(expiresAt)
.ifPresent((exp) -> _payload.put(RegisteredClaims.EXPIRES_AT, exp)); .ifPresent((exp) -> _payload.put(RegisteredClaims.EXPIRES_AT, exp));
Optional.ofNullable(tokenId) Optional.ofNullable(tokenId)
.filter((jti) -> !jti.isBlank()) .filter((jti) -> !jti.isBlank())
.ifPresent((jti) -> _payload.put(RegisteredClaims.TOKEN_ID, jti)); .ifPresent((jti) -> _payload.put(RegisteredClaims.TOKEN_ID, jti));
Optional.ofNullable(issuer) Optional.ofNullable(issuer)
.filter((iss) -> !iss.isBlank()) .filter((iss) -> !iss.isBlank())
.ifPresent((iss) -> _payload.put(RegisteredClaims.ISSUER, iss)); .ifPresent((iss) -> _payload.put(RegisteredClaims.ISSUER, iss));
Optional.ofNullable(issuedAt) Optional.ofNullable(issuedAt)
.ifPresent((iat) -> _payload.put(RegisteredClaims.ISSUED_AT, iat)); .ifPresent((iat) -> _payload.put(RegisteredClaims.ISSUED_AT, iat));
Optional.ofNullable(notBefore) Optional.ofNullable(notBefore)
.ifPresent((nbf) -> _payload.put(RegisteredClaims.NOT_BEFORE, nbf)); .ifPresent((nbf) -> _payload.put(RegisteredClaims.NOT_BEFORE, nbf));
return _payload; return _payload;
} }
private void checkClaimName(String name) {
if (RegisteredClaims.VALUES.contains(name)) {
throw new IllegalStateException("Please set registered claims with pre-defined methods");
}
}
} }