【发布时间】:2019-03-01 23:34:59
【问题描述】:
我已经有一个私钥作为varchar2 存储在数据库中,并存储在一个名为Key 的变量中,如代码所示。
下面是我将此私钥设置为 JsonWebSignature 的一段代码,但我收到类似
JsonWebStructure 类型中的方法 setKey(Key) 不适用于参数(String)
我不想生成新的 RSA 密钥,因为我已经有了它。
public static String getJWTToken(String userName) throws JoseException {
JwtClaims claims = new JwtClaims();
claims.setAudience(Constants.AUDIENCE);
claims.setIssuer(InitialLoader.JWT_KEY);//Getting from config property file
claims.setIssuedAtToNow();
NumericDate tokenExpDate = NumericDate.now();
tokenExpDate.addSeconds(Constants.SECONDS);
claims.setExpirationTime(tokenExpDate);
if(userName!=null && !userName.isEmpty())
claims.setClaim("userName", userName);
System.out.println("Senders end :: " + claims.toJson());
// SIGNING the token
String key = "jxFd%asdjd";
RsaJsonWebKey jsonSignKey = RsaJwkGenerator.generateJwk(2048);
JsonWebSignature jws = new JsonWebSignature();
//jws.setKey(jsonSignKey.getPrivateKey());
jws.setKey(key);// Getting error here
jws.setPayload(claims.toJson());
jws.setHeader("typ", Constants.TYP);
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.HMAC_SHA256);// Setting the algorithm to be used
String signedJwt = jws.getCompactSerialization();// payload is signed using this compactSerialization
System.out.println("Signed key for sender is::" + signedJwt);
return signedJwt;
}
【问题讨论】:
-
字符串键 = "jxFd%asdjd";这是我要设置的键