下面列出了io.jsonwebtoken.SignatureAlgorithm#HS512 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 创建jwt
* @param id
* @param subject
* @param ttlMillis
* @return
* @throws Exception
*/
public String createJWT(String id, String subject, long ttlMillis) throws Exception {
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS512;
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
SecretKey key = generalKey();
JwtBuilder builder = Jwts.builder()
.setId(id)
.setIssuedAt(now)
.setSubject(subject)
.signWith(signatureAlgorithm, key);
if (ttlMillis >= 0) {
long expMillis = nowMillis + ttlMillis;
Date exp = new Date(expMillis);
builder.setExpiration(exp);
}
return builder.compact();
}
static SignatureAlgorithm getAlgorithm(byte[] hmacSigningKeyBytes) {
Assert.isTrue(hmacSigningKeyBytes != null && hmacSigningKeyBytes.length > 0,
"hmacSigningBytes cannot be null or empty.");
if (hmacSigningKeyBytes.length >= 64) {
return SignatureAlgorithm.HS512;
} else if (hmacSigningKeyBytes.length >= 48) {
return SignatureAlgorithm.HS384;
} else { //<= 32
return SignatureAlgorithm.HS256;
}
}
public JsonWebTokenUtility() {
// THIS IS NOT A SECURE PRACTICE!
// For simplicity, we are storing a static key here.
// Ideally, in a microservices environment, this key would kept on a
// config server.
signatureAlgorithm = SignatureAlgorithm.HS512;
String encodedKey = "L7A/6zARSkK1j7Vd5SDD9pSSqZlqF7mAhiOgRbgv9Smce6tf4cJnvKOjtKPxNNnWQj+2lQEScm3XIUjhW+YVZg==";
secretKey = deserializeKey(encodedKey);
// secretKey = MacProvider.generateKey(signatureAlgorithm);
}
public static SignatureAlgorithm getSignatureAlgorithm(SigningAlgorithms alg) {
switch (alg) {
case HS256:
return SignatureAlgorithm.HS256;
case HS384:
return SignatureAlgorithm.HS384;
case HS512:
return SignatureAlgorithm.HS512;
case RS256:
return SignatureAlgorithm.RS256;
case RS384:
return SignatureAlgorithm.RS384;
case RS512: // NOSONAR - asking to reduce line count
return SignatureAlgorithm.RS512;
// The following are not JDK standard and are difficult to test, so ignoring for now.
// case PS256:
// return SignatureAlgorithm.PS256; //NOSONAR
// case PS384:
// return SignatureAlgorithm.PS384; //NOSONAR
// case PS512:
// return SignatureAlgorithm.PS512; //NOSONAR
// case ES256:
// return SignatureAlgorithm.ES256; //NOSONAR
// case ES384:
// return SignatureAlgorithm.ES384; //NOSONAR
// case ES512:
// return SignatureAlgorithm.ES512; //NOSONAR
case NONE:
return SignatureAlgorithm.NONE;
default:
throw new IllegalStateException("Unknown Signing Algorithm: " + alg.getLabel());
}
}
public SignatureAlgorithm getSignatureAlgorithm() {
return SignatureAlgorithm.HS512;
}
private static SignatureAlgorithm getValidSignatureAlgorithm() {
return SignatureAlgorithm.HS512;
}
private String createJWT(String secretKey, String username) {
final byte[] decodedSecretKey = Base64.getDecoder().decode(secretKey);
final Key secretKeyKey = new SecretKeySpec(decodedSecretKey, 0, decodedSecretKey.length, "HS512");
final SignatureAlgorithm alg = SignatureAlgorithm.HS512;
return Jwts.builder().setSubject(username).signWith(alg, secretKeyKey).compact();
}