spring.turbo.module.jwt.JwtData Maven / Gradle / Ivy
The newest version!
package spring.turbo.module.jwt;
import java.io.Serializable;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import static spring.turbo.module.jwt.JwtConstants.*;
/**
* JWT签名所包含的信息
*
* @author 应卓
* @see #newInstance()
* @see JwtConstants
* @since 3.1.1
*/
public final class JwtData implements Serializable {
private final Map headerMap = new HashMap<>();
private final Map payloadMap = new HashMap<>();
/**
* 构造方法
*/
public JwtData() {
this.headerMap.put(HEADER_TYPE, "JWT");
}
public static JwtData newInstance() {
return new JwtData();
}
public JwtData addHeaderType(String type) {
headerMap.put(HEADER_TYPE, type);
return this;
}
public JwtData addHeaderKeyId(String id) {
headerMap.put(HEADER_KEY_ID, id);
return this;
}
public JwtData addHeaderKeyId(Supplier idSupplier) {
return addHeaderKeyId(idSupplier.get());
}
public JwtData addHeaderContentType(String contentType) {
headerMap.put(HEADER_CONTENT_TYPE, contentType);
return this;
}
public JwtData addHeaderAlgorithm(String algorithm) {
headerMap.put(HEADER_ALGORITHM, algorithm);
return this;
}
public JwtData addPayloadIssuer(String issuer) {
payloadMap.put(PAYLOAD_ISSUER, issuer);
return this;
}
public JwtData addPayloadSubject(String subject) {
payloadMap.put(PAYLOAD_SUBJECT, subject);
return this;
}
public JwtData addPayloadAudience(String... audience) {
payloadMap.put(PAYLOAD_AUDIENCE, audience);
return this;
}
public JwtData addPayloadExpiresAt(LocalDateTime time) {
payloadMap.put(PAYLOAD_EXPIRES, toDate(time));
return this;
}
public JwtData addPayloadExpiresAtFuture(Duration duration) {
payloadMap.put(PAYLOAD_EXPIRES, toDate(LocalDateTime.now().plus(duration)));
return this;
}
public JwtData addPayloadNotBefore(LocalDateTime time) {
payloadMap.put(PAYLOAD_NOT_BEFORE, toDate(time));
return this;
}
public JwtData addPayloadNotBeforeAtFuture(Duration duration) {
payloadMap.put(PAYLOAD_NOT_BEFORE, toDate(LocalDateTime.now().plus(duration)));
return this;
}
public JwtData addPayloadIssuedAt(LocalDateTime time) {
payloadMap.put(PAYLOAD_ISSUED_AT, toDate(time));
return this;
}
public JwtData addPayloadIssuedAtNow() {
return addPayloadIssuedAt(LocalDateTime.now());
}
public JwtData addPayloadJwtId(Object jwtId) {
payloadMap.put(PAYLOAD_JWT_ID, jwtId);
return this;
}
public JwtData addPayloadJwtId(Supplier