I already have a private key stored in a database as varchar2
and stored in a variable named Key
as shown in code.
Below is my piece of code to set this private key to JsonWebSignature but I am getting an error like
The method setKey(Key) in the type JsonWebStructure is not applicable for the arguments (String)
I don't want to generate a new RSA key as I already have it.
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;
}
You do:
The signature of the
setKey
method is public void setKey(Key key) So you need to pass it a Key. You are passing it a String so it won't compile. You need to make a Key out of your String.Not sure how to do that though.
EDIT:
I guess you could do something along these lines:
But that won't work as the
newPublicJwk
method is expecting a JSON string. Did you get your key out of a JSON String?