Skip to content

Commit

Permalink
support config your custom jwt secret need (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsun28 committed Oct 23, 2022
1 parent 4e54fb2 commit 12987a7
Showing 1 changed file with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.usthe.sureness.util;

import com.usthe.sureness.processor.exception.ExtSurenessException;
import io.jsonwebtoken.*;
import io.jsonwebtoken.security.*;
import io.jsonwebtoken.security.SignatureException;
Expand Down Expand Up @@ -37,6 +38,8 @@ public class JsonWebTokenUtil {
/** Encryption and decryption signature **/
private static Key secretKey;

private static boolean isUsedDefault = true;

static {
byte[] secretKeyBytes = DatatypeConverter.parseBase64Binary(DEFAULT_SECRET_KEY);
secretKey = Keys.hmacShaKeyFor(secretKeyBytes);
Expand Down Expand Up @@ -200,6 +203,9 @@ public static String issueJwt(String subject, Long period, Map<String, Object> c
public static String issueJwtAll(String id, String subject, String issuer, Long period,
String audience, String payload, Long notBefore,
Map<String, Object> headerMap, Map<String, Object> customClaimMap){
if (isUsedDefault) {
throw new ExtSurenessException("Please config your custom jwt secret. JsonWebTokenUtil.setDefaultSecretKey | sureness.jwt.secret");
}
long currentTimeMillis = System.currentTimeMillis();
JwtBuilder jwtBuilder = Jwts.builder();
if (id != null) {
Expand Down Expand Up @@ -295,5 +301,6 @@ public static Claims parseJwt(String jwt) throws ExpiredJwtException, Unsupporte
public static void setDefaultSecretKey(String secretNowKeyValue) {
byte[] secretKeyBytes = DatatypeConverter.parseBase64Binary(secretNowKeyValue);
secretKey = Keys.hmacShaKeyFor(secretKeyBytes);
isUsedDefault = false;
}
}

0 comments on commit 12987a7

Please sign in to comment.