【问题标题】:How to convert String to Private Key and set it to JsonWebSignature如何将字符串转换为私钥并将其设置为 JsonWebSignature
【发布时间】: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";这是我要设置的键

标签: java jwt jose4j


【解决方案1】:

你这样做:

String key = "jxFd%asdjd";
....
jws.setKey(key);// Getting error here

setKey 方法的签名是public void setKey(Key key) 所以你需要给它一个Key。您正在向它传递一个字符串,因此它不会编译。你需要用你的字符串做一个键。

虽然不知道该怎么做。

编辑:

我想你可以按照以下方式做一些事情:

String keyString = "jxFd%asdjd";
PublicJsonWebKey originalKey = PublicJsonWebKey.Factory.newPublicJwk(keyString);

JsonWebSignature jws = new JsonWebSignature();
jws.setKey(originalKey.getPrivateKey());

但这不起作用,因为 newPublicJwk 方法需要一个 JSON 字符串。您是否从 JSON 字符串中获取密钥?

【讨论】:

  • 用同样的方法试过:--String key = "jxFd%asdjd"; byte[] decodedKey = Base64.getDecoder().decode(key); SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "HS256");
  • 获取异常:线程“main”中的异常 java.lang.IllegalArgumentException:byte[] 处的非法 base64 字符 decodedKey = Base64.getDecoder().decode(key);
  • 您的字符串不是有效的 Base64 字符串。看看我上次的编辑,你有没有从一些 JSON 中得到密钥?
【解决方案2】:

我已经找到了解决问题的方法。下面是我的一段代码,其中“key”是“RSAPrivateKey”。
公共静态字符串 getJWTToken(String userName) 抛出 JoseException {

    JwtClaims claims = new JwtClaims();
    claims.setAudience(Constants.AUDIENCE);
    claims.setIssuer(InitialLoader.JWT_KEY);
    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
    PrivateKey privateKey = null;
    try {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        System.out.println("InitialLoader.RSAPrivateKey is::"+InitialLoader.RSAPrivateKey);
        byte[] content = Files.readAllBytes(Paths.get(InitialLoader.RSAPrivateKey));//from config file

        String pkcs8Pem = new String(content, StandardCharsets.UTF_8);
        byte[] pkcs8EncodedBytes = org.apache.commons.codec.binary.Base64.decodeBase64(pkcs8Pem);
        KeyFactory factory = KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes);
        privateKey = factory.generatePrivate(privKeySpec);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JsonWebSignature jws = new JsonWebSignature();
    jws.setKey(privateKey);
    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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 2021-03-01
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    相关资源
    最近更新 更多