public String JWTokenCreate(String giss, String gscp, String gauth, String gsub, String gidt, String gedt, Container container) throws StreamTransformationException{
try
{
// Creation of encoded header body string for Json Web Token
String Head = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}";
byte[] hencrypt = Head.getBytes();
String Hencrypt = DatatypeConverter.printBase64Binary(hencrypt);
String Body = "{\"iss\": \""+giss+"\",\"sub\": \""+gsub+"\",\"scope\": \""+gscp+"\",\"aud\":\""+gauth+"\",\"exp\""+": "+gedt+",\"iat\""+": "+gidt+"}";
byte[] bdencrypt = Body.getBytes();
String Bdencrypt = DatatypeConverter.printBase64Binary(bdencrypt);
//Concate header and body used for Json Web Token
String HBfinal = Hencrypt+"."+Bdencrypt;
//Private Key
String PRIVATE_KEY = "complete private key start with BEGIN till END of key.";
// Read in the key into a String
StringBuilder pkcs8Lines = new StringBuilder();
BufferedReader rdr = new BufferedReader(new StringReader(PRIVATE_KEY));
String line;
while ((line = rdr.readLine()) != null) {
pkcs8Lines.append(line);
}
// Remove the "BEGIN" and "END" lines, as well as any whitespace
String pkcs8Pem = pkcs8Lines.toString();
pkcs8Pem = pkcs8Pem.replace("-----BEGIN PRIVATE KEY-----", "");
pkcs8Pem = pkcs8Pem.replace("-----END PRIVATE KEY-----", "");
pkcs8Pem = pkcs8Pem.replaceAll("\\s+","");
// Base64 decode the result
byte [] pkcs8EncodedBytes = DatatypeConverter.parseBase64Binary(pkcs8Pem);
// Extract the private key
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privKey = kf.generatePrivate(keySpec);
//Function to create google api signature
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(privKey);
byte[] raw = HBfinal.getBytes("UTF-8");
signature.update(raw);
byte[] signed = signature.sign();
//Create SignKey with privatekey and base64encode
String Signkey = DatatypeConverter.printBase64Binary(signed);
String finalBearer = HBfinal +"."+Signkey;
//URL Encoder for content type x-www-form-urlencoded
String encodedData = URLEncoder.encode(finalBearer, "UTF-8");
return encodedData;
}
catch (Exception error)
{
throw new RuntimeException(error);
}
}
In Operation Mapping I am using 3 Mappings :
- Graphical mapping for Request Message
- XSLT mapping to create JSON message
- Java mapping to create final Raw message format
ID: We have to use "Get" Method with below REST URL, Response need to be convert from JSON to XML.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
8 | |
8 | |
7 | |
6 | |
5 | |
4 | |
4 | |
4 | |
3 | |
3 |