OAuth 2.0 JWT承载令牌流-从UDF访问密钥库

2020-09-01 22:52发布

点击此处---> 群内免费提供SAP练习系统(在群公告中)加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)大家好, 我正在进行Sales...

         点击此处--->   EasySAP.com群内免费提供SAP练习系统(在群公告中)

加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)


大家好,

我正在进行Salesforce集成,以将JWT令牌发送到Salesforce。

Salesforce为此提供了一系列说明。

OAuth 2.0 JWT承载令牌流

导入org.apache.commons.codec.binary.Base64;
 导入java.io. *;
 导入java.security。*;
 导入java.text.MessageFormat;

 公共类JWTExample {

   公共静态void main(String [] args){

     字符串标题=" {\" alg \":\" RS256 \"}";
     字符串ClaimTemplate ="'{'\" iss \":\" {0} \",\" sub \":\" {1} \",\" aud \":\" {2} \",\  " exp \":\" {3} \"'}'";

     尝试{
       StringBuffer令牌=新的StringBuffer();

      //编码JWT标头并将其添加到我们的字符串中以进行签名
       token.append(Base64.encodeBase64URLSafeString(header.getBytes(" UTF-8")));;

      //以句点分隔
       token.append("。");

      //创建JWT Claims对象
       String [] ClaimArray = new String [4];
       ClaimArray [0] =" 3MVG99OxTyEMCQ3gNp2PjkqeZKxnmAiG1xV4oHh9AKL_rSK.BoSVPGZHQukXnVjzRgSuQqGn75NL7yfkQcyy7"
       ClaimArray [1] =" my@email.com";
       claimArray [2] =" https://login.salesforce.com";
       ClaimArray [3] = Long.toString((System.currentTimeMillis()/1000)+ 300);
       MessageFormat声明;
       Claims = new MessageFormat(claimTemplate);
       字符串有效负载= Claims.format(claimArray);

      //添加编码的声明对象
       token.append(Base64.encodeBase64URLSafeString(payload.getBytes(" UTF-8")));;

      //从密钥库加载私钥
       KeyStore keystore = KeyStore.getInstance(" JKS");
       keystore.load(new FileInputStream(" ./path/to/keystore.jks")," keystorepassword" .toCharArray());
       PrivateKey privateKey =(PrivateKey)keystore.getKey(" certalias"," privatekeypassword" .toCharArray());

      //签名JWT标头+"。"  + JWT索赔对象
       签名签名= Signature.getInstance(" SHA256withRSA");
       signature.initSign(privateKey);
       signature.update(token.toString()。getBytes(" UTF-8"));
       字符串signedPayload = Base64.encodeBase64URLSafeString(signature.sign());

      //以句点分隔
       token.append("。");

      //添加编码后的签名
       token.append(signedPayload);

       System.out.println(token.toString());

     } catch(Exception e){
         e.printStackTrace();
     }
   }
 } 

请帮助我访问存储在密钥库中的私钥

 KeyStore keystore = KeyStore.getInstance(" JKS");
       keystore.load(new FileInputStream(" ./path/to/keystore.jks")," keystorepassword" .toCharArray());
       PrivateKey privateKey =(PrivateKey)keystore.getKey(" certalias"," privatekeypassword" .toCharArray()); 

我的密钥存储在/root/JXX/sec

这是用于存储消息的PGP加密的其他密钥的存储位置。

我应该将密钥存储在同一位置还是没关系?

并指导我如何访问此密钥存储的pls?

-

谢谢。


2条回答
me_for_i
2020-09-01 23:22

你好拉贾尼,

是的,能够读取私钥。

 InitialContext ctx = new InitialContext();
//mt.addInfo("1");
 KeystoreManagerWrapper kmanager =(KeystoreManagerWrapper)ctx.lookup(" keystore");
//mt.addInfo("2");
 KeyStore keyStore = kmanager.getKeystore(" TrustedCAs");
//mt.addInfo("3");
 mt.addInfo(keyStore.toString());
//KeyStore keystore = KeyStore.getInstance(" JKS");


//keystore.load(new FileInputStream(" ./path/to/keystore.jks")," keystorepassword" .toCharArray());


 PrivateKey privateKey =(PrivateKey)keyStore.getKey(" 11652324",null);
//mt.addInfo("4");
//var5 = keystore.toString();


 签名签名= Signature.getInstance(" sha256WithRSA");
//mt.addInfo("5");
 signature.initSign(privateKey);
//mt.addInfo("6");
 signature.update(token.toString()。getBytes(" UTF-8"));
//mt.addInfo("7");
 字符串signedPayload = Base64.encodeBase64URLSafeString(signature.sign());
//mt.addInfo("8");
 

一周热门 更多>