Last active
December 20, 2015 16:42
-
-
Save aliesbelik/f1d47a7419e5c88efe5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.text.*; | |
import org.json.*; | |
import io.jsonwebtoken.* | |
import org.apache.commons.codec.binary.Base64; | |
import com.google.gson.JsonObject; | |
// get JWT secret key from params | |
String [] params = Parameters.split(","); | |
String secret_key = params[0]; | |
// construct your body data - JSON entity in case below | |
JsonObject jo = new JsonObject(); | |
jo.addProperty("param1", "PARAM_1"); | |
jo.addProperty("param2", "PARAM_2"); | |
jo.addProperty("param3", "PARAM_3"); | |
String jsonString = jo.toString(); | |
// perform JWT-signing of body data | |
byte[] bytesEncoded = Base64.encodeBase64(secret_key.getBytes()); | |
String secret = new String(bytesEncoded); | |
try { | |
String jwtToken = Jwts.builder() | |
.setHeaderParam("typ","JWT") | |
.setPayload(jsonString) | |
.signWith(SignatureAlgorithm.HS256, secret) // base64EncodedSecretKey | |
.compact(); | |
// put JWT-signed body data into variable | |
vars.put("jwtToken", jwtToken); | |
} catch (Exception ex) { | |
prev.setSuccessful(false); | |
log.error(ex.getMessage()); | |
System.err.println(ex.getMessage()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment