/** * Generates base64 representation of JWT token sign using "RS256" algorithm * * <p>getHeader().toBase64UrlEncode() + "." + getClaim().toBase64UrlEncode() + "." + sign * * @return base64 representation of JWT token */ public String sign() { for (JwtTokenDecorator decorator : JwtTokenDecorator.all()) { decorator.decorate(this); } /** * kid might have been set already by using {@link #header} or {@link JwtTokenDecorator}, if * present use it otherwise use the default kid */ String keyId = (String) header.get(HeaderParameterNames.KEY_ID); if (keyId == null) { keyId = DEFAULT_KEY_ID; } JwtRsaDigitalSignatureKey rsaDigitalSignatureConfidentialKey = new JwtRsaDigitalSignatureKey(keyId); try { return rsaDigitalSignatureConfidentialKey.sign(claim); } catch (JoseException e) { String msg = "Failed to sign JWT token: " + e.getMessage(); logger.error(msg); throw new ServiceException.UnexpectedErrorException(msg, e); } }
public static void generateWebToken() { try { key = new AesKey(ByteUtil.randomBytes(16)); webToken = new JsonWebEncryption(); webToken.setPayload("Hello David"); webToken.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.A128KW); webToken.setEncryptionMethodHeaderParameter( ContentEncryptionAlgorithmIdentifiers.AES_128_CBC_HMAC_SHA_256); webToken.setKey(key); String encrypted = webToken.getCompactSerialization(); System.out.println("Hello David : Encrypted " + encrypted); webToken = new JsonWebEncryption(); webToken.setKey(key); webToken.setCompactSerialization(encrypted); System.out.println("Payload: " + webToken.getPayload()); } catch (JoseException ex) { ex.printStackTrace(); } }