public class DefaultJwtBuilder extends java.lang.Object implements JwtBuilder
| 构造器和说明 |
|---|
DefaultJwtBuilder() |
| 限定符和类型 | 方法和说明 |
|---|---|
JwtBuilder |
addClaims(java.util.Map<java.lang.String,java.lang.Object> claims)
Adds all given name/value pairs to the JSON Claims in the payload.
|
protected java.lang.String |
base64UrlEncode(java.lang.Object o,
java.lang.String errMsg) |
JwtBuilder |
claim(java.lang.String name,
java.lang.Object value)
Sets a custom JWT Claims parameter value.
|
java.lang.String |
compact()
Actually builds the JWT and serializes it to a compact, URL-safe string according to the
JWT Compact Serialization
rules.
|
JwtBuilder |
compressWith(CompressionCodec compressionCodec)
Compresses the JWT body using the specified
CompressionCodec. |
protected JwtSigner |
createSigner(SignatureAlgorithm alg,
java.security.Key key) |
protected Claims |
ensureClaims() |
protected Header |
ensureHeader() |
JwtBuilder |
setAudience(java.lang.String aud)
Sets the JWT Claims
aud (audience) value. |
JwtBuilder |
setClaims(Claims claims)
Sets the JWT payload to be a JSON Claims instance.
|
JwtBuilder |
setClaims(java.util.Map<java.lang.String,java.lang.Object> claims)
Sets the JWT payload to be a JSON Claims instance populated by the specified name/value pairs.
|
JwtBuilder |
setDescription(java.lang.String description) |
JwtBuilder |
setExpiration(java.util.Date exp)
Sets the JWT Claims
exp (expiration) value. |
JwtBuilder |
setHeader(Header header)
Sets (and replaces) any existing header with the specified header.
|
JwtBuilder |
setHeader(java.util.Map<java.lang.String,java.lang.Object> header)
Sets (and replaces) any existing header with the specified header.
|
JwtBuilder |
setHeaderParam(java.lang.String name,
java.lang.Object value)
Applies the specified name/value pair to the header.
|
JwtBuilder |
setHeaderParams(java.util.Map<java.lang.String,java.lang.Object> params)
Applies the specified name/value pairs to the header.
|
JwtBuilder |
setId(java.lang.String jti)
Sets the JWT Claims
jti (JWT ID) value. |
JwtBuilder |
setIssuedAt(java.util.Date iat)
Sets the JWT Claims
iat (issued at) value. |
JwtBuilder |
setIssuer(java.lang.String iss)
Sets the JWT Claims
iss (issuer) value. |
JwtBuilder |
setNotBefore(java.util.Date nbf)
Sets the JWT Claims
nbf (not before) value. |
JwtBuilder |
setPayload(java.lang.String payload)
Sets the JWT's payload to be a plaintext (non-JSON) string.
|
JwtBuilder |
setSubject(java.lang.String sub)
Sets the JWT Claims
sub (subject) value. |
JwtBuilder |
signWith(SignatureAlgorithm alg,
byte[] secretKey)
Signs the constructed JWT using the specified algorithm with the specified key, producing a JWS.
|
JwtBuilder |
signWith(SignatureAlgorithm alg,
java.security.Key key)
Signs the constructed JWT using the specified algorithm with the specified key, producing a JWS.
|
JwtBuilder |
signWith(SignatureAlgorithm alg,
java.lang.String base64EncodedSecretKey)
Signs the constructed JWT using the specified algorithm with the specified key, producing a JWS.
|
protected byte[] |
toJson(java.lang.Object object) |
public JwtBuilder setHeader(Header header)
JwtBuilderJwtBuilder.setHeaderParams(java.util.Map) method instead.setHeader 在接口中 JwtBuilderheader - the header to set (and potentially replace any existing header).public JwtBuilder setHeader(java.util.Map<java.lang.String,java.lang.Object> header)
JwtBuilderJwtBuilder.setHeaderParams(java.util.Map) method instead.setHeader 在接口中 JwtBuilderheader - the header to set (and potentially replace any existing header).public JwtBuilder setHeaderParams(java.util.Map<java.lang.String,java.lang.Object> params)
JwtBuildersetHeaderParams 在接口中 JwtBuilderparams - the header name/value pairs to append to the header.protected Header ensureHeader()
public JwtBuilder setHeaderParam(java.lang.String name, java.lang.Object value)
JwtBuildersetHeaderParam 在接口中 JwtBuildername - the header parameter namevalue - the header parameter valuepublic JwtBuilder signWith(SignatureAlgorithm alg, byte[] secretKey)
JwtBuildersignWith 在接口中 JwtBuilderalg - the JWS algorithm to use to digitally sign the JWT, thereby producing a JWS.secretKey - the algorithm-specific signing key to use to digitally sign the JWT.public JwtBuilder signWith(SignatureAlgorithm alg, java.lang.String base64EncodedSecretKey)
JwtBuilderThis is a convenience method: the string argument is first BASE64-decoded to a byte array and this resulting
byte array is used to invoke JwtBuilder.signWith(SignatureAlgorithm, byte[]).
signWith 在接口中 JwtBuilderalg - the JWS algorithm to use to digitally sign the JWT, thereby producing a JWS.base64EncodedSecretKey - the BASE64-encoded algorithm-specific signing key to use to digitally sign the
JWT.public JwtBuilder signWith(SignatureAlgorithm alg, java.security.Key key)
JwtBuildersignWith 在接口中 JwtBuilderalg - the JWS algorithm to use to digitally sign the JWT, thereby producing a JWS.key - the algorithm-specific signing key to use to digitally sign the JWT.public JwtBuilder compressWith(CompressionCodec compressionCodec)
JwtBuilderCompressionCodec.
If your compact JWTs are large, and you want to reduce their total size during network transmission, this can be useful. For example, when embedding JWTs in URLs, some browsers may not support URLs longer than a certain length. Using compression can help ensure the compact JWT fits within that length. However, NOTE:
The JWT family of specifications defines compression only for JWE (Json Web Encryption) tokens. Even so, JJWT will also support compression for JWS tokens as well if you choose to use it. However, be aware that if you use compression when creating a JWS token, other libraries may not be able to parse that JWS token. When using compression for JWS tokens, be sure that that all parties accessing the JWS token support compression for JWS.
Compression when creating JWE tokens however should be universally accepted for any library that supports JWE.
compressWith 在接口中 JwtBuildercompressionCodec - implementation of the CompressionCodec to be used.CompressionCodecspublic JwtBuilder setPayload(java.lang.String payload)
JwtBuilderJwtBuilder.setClaims(Claims) or JwtBuilder.setClaims(java.util.Map) methods instead.
The payload and claims properties are mutually exclusive - only one of the two may be used.
setPayload 在接口中 JwtBuilderpayload - the plaintext (non-JSON) string that will be the body of the JWT.protected Claims ensureClaims()
public JwtBuilder setClaims(Claims claims)
JwtBuilderJwtBuilder.setPayload(String) method instead.
The payload and claims properties are mutually exclusive - only one of the two may be used.
setClaims 在接口中 JwtBuilderclaims - the JWT claims to be set as the JWT body.public JwtBuilder setClaims(java.util.Map<java.lang.String,java.lang.Object> claims)
JwtBuilderJwtBuilder.setPayload(String)
method instead.
The payload* and claims* properties are mutually exclusive - only one of the two may be used.
setClaims 在接口中 JwtBuilderclaims - the JWT claims to be set as the JWT body.public JwtBuilder addClaims(java.util.Map<java.lang.String,java.lang.Object> claims)
JwtBuilderThe payload and claims properties are mutually exclusive - only one of the two may be used.
addClaims 在接口中 JwtBuilderclaims - the JWT claims to be added to the JWT body.public JwtBuilder setIssuer(java.lang.String iss)
JwtBuilderiss (issuer) value. A null value will remove the property from the Claims.
This is a convenience method. It will first ensure a Claims instance exists as the JWT body and then set
the Claims issuer field with the specified value. This allows you to write
code like this:
String jwt = Jwts.builder().setIssuer("Joe").compact();
instead of this:
Claims claims = Jwts.claims().setIssuer("Joe");
String jwt = Jwts.builder().setClaims(claims).compact();
if desired.
setIssuer 在接口中 ClaimsMutator<JwtBuilder>setIssuer 在接口中 JwtBuilderiss - the JWT iss value or null to remove the property from the Claims map.public JwtBuilder setSubject(java.lang.String sub)
JwtBuildersub (subject) value. A null value will remove the property from the Claims.
This is a convenience method. It will first ensure a Claims instance exists as the JWT body and then set
the Claims subject field with the specified value. This allows you to write
code like this:
String jwt = Jwts.builder().setSubject("Me").compact();
instead of this:
Claims claims = Jwts.claims().setSubject("Me");
String jwt = Jwts.builder().setClaims(claims).compact();
if desired.
setSubject 在接口中 ClaimsMutator<JwtBuilder>setSubject 在接口中 JwtBuildersub - the JWT sub value or null to remove the property from the Claims map.public JwtBuilder setAudience(java.lang.String aud)
JwtBuilderaud (audience) value. A null value will remove the property from the Claims.
This is a convenience method. It will first ensure a Claims instance exists as the JWT body and then set
the Claims audience field with the specified value. This allows you to write
code like this:
String jwt = Jwts.builder().setAudience("You").compact();
instead of this:
Claims claims = Jwts.claims().setSubject("You");
String jwt = Jwts.builder().setClaims(claims).compact();
if desired.
setAudience 在接口中 ClaimsMutator<JwtBuilder>setAudience 在接口中 JwtBuilderaud - the JWT aud value or null to remove the property from the Claims map.public JwtBuilder setExpiration(java.util.Date exp)
JwtBuilderexp (expiration) value. A null value will remove the property from the Claims.
A JWT obtained after this timestamp should not be used.
This is a convenience method. It will first ensure a Claims instance exists as the JWT body and then set
the Claims expiration field with the specified value. This allows
you to write code like this:
String jwt = Jwts.builder().setExpiration(new Date(System.currentTimeMillis() + 3600000)).compact();
instead of this:
Claims claims = Jwts.claims().setExpiration(new Date(System.currentTimeMillis() + 3600000)); String jwt = Jwts.builder().setClaims(claims).compact();
if desired.
setExpiration 在接口中 ClaimsMutator<JwtBuilder>setExpiration 在接口中 JwtBuilderexp - the JWT exp value or null to remove the property from the Claims map.public JwtBuilder setNotBefore(java.util.Date nbf)
JwtBuildernbf (not before) value. A null value will remove the property from the Claims.
A JWT obtained before this timestamp should not be used.
This is a convenience method. It will first ensure a Claims instance exists as the JWT body and then set
the Claims notBefore field with the specified value. This allows
you to write code like this:
String jwt = Jwts.builder().setNotBefore(new Date()).compact();
instead of this:
Claims claims = Jwts.claims().setNotBefore(new Date()); String jwt = Jwts.builder().setClaims(claims).compact();
if desired.
setNotBefore 在接口中 ClaimsMutator<JwtBuilder>setNotBefore 在接口中 JwtBuildernbf - the JWT nbf value or null to remove the property from the Claims map.public JwtBuilder setIssuedAt(java.util.Date iat)
JwtBuilderiat (issued at) value. A null value will remove the property from the Claims.
The value is the timestamp when the JWT was created.
This is a convenience method. It will first ensure a Claims instance exists as the JWT body and then set
the Claims issuedAt field with the specified value. This allows
you to write code like this:
String jwt = Jwts.builder().setIssuedAt(new Date()).compact();
instead of this:
Claims claims = Jwts.claims().setIssuedAt(new Date()); String jwt = Jwts.builder().setClaims(claims).compact();
if desired.
setIssuedAt 在接口中 ClaimsMutator<JwtBuilder>setIssuedAt 在接口中 JwtBuilderiat - the JWT iat value or null to remove the property from the Claims map.public JwtBuilder setId(java.lang.String jti)
JwtBuilderjti (JWT ID) value. A null value will remove the property from the Claims.
The value is a CaSe-SenSiTiVe unique identifier for the JWT. If specified, this value MUST be assigned in a manner that ensures that there is a negligible probability that the same value will be accidentally assigned to a different data object. The ID can be used to prevent the JWT from being replayed.
This is a convenience method. It will first ensure a Claims instance exists as the JWT body and then set
the Claims id field with the specified value. This allows
you to write code like this:
String jwt = Jwts.builder().setId(UUID.randomUUID().toString()).compact();
instead of this:
Claims claims = Jwts.claims().setIssuedAt(UUID.randomUUID().toString()); String jwt = Jwts.builder().setClaims(claims).compact();
if desired.
setId 在接口中 ClaimsMutator<JwtBuilder>setId 在接口中 JwtBuilderjti - the JWT jti (id) value or null to remove the property from the Claims map.public JwtBuilder setDescription(java.lang.String description)
setDescription 在接口中 ClaimsMutator<JwtBuilder>public JwtBuilder claim(java.lang.String name, java.lang.Object value)
JwtBuildernull value will remove the property from the Claims.
This is a convenience method. It will first ensure a Claims instance exists as the JWT body and then set the
named property on the Claims instance using the Claims put method. This allows
you to write code like this:
String jwt = Jwts.builder().claim("aName", "aValue").compact();
instead of this:
Claims claims = Jwts.claims().put("aName", "aValue");
String jwt = Jwts.builder().setClaims(claims).compact();
if desired.
claim 在接口中 JwtBuildername - the JWT Claims property namevalue - the value to set for the specified Claims property namepublic java.lang.String compact()
JwtBuildercompact 在接口中 JwtBuilderprotected JwtSigner createSigner(SignatureAlgorithm alg, java.security.Key key)
protected java.lang.String base64UrlEncode(java.lang.Object o,
java.lang.String errMsg)
protected byte[] toJson(java.lang.Object object)