Skip to content

Instantly share code, notes, and snippets.

@aliesbelik
Last active December 20, 2015 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aliesbelik/f1d47a7419e5c88efe5b to your computer and use it in GitHub Desktop.
Save aliesbelik/f1d47a7419e5c88efe5b to your computer and use it in GitHub Desktop.
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