io.jsonwebtoken.CompressionCodecs Java Examples

The following examples show how to use io.jsonwebtoken.CompressionCodecs. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JwtTokenUtil.java    From SpringBootLearn with Apache License 2.0 5 votes vote down vote up
/**
 * 生成token
 * @param subject  用户名
 * @param claims
 * @param expiration 过期时间
 * @return
 */
private String generateToken(String subject, Map<String, Object> claims, long expiration) {
    return Jwts.builder()
            .setClaims(claims)
            .setSubject(subject)
            .setId(UUID.randomUUID().toString())
            .setIssuedAt(new Date())
            .setExpiration(generateExpirationDate(expiration))
            .compressWith(CompressionCodecs.DEFLATE)
            .signWith(SIGNATURE_ALGORITHM, secret)
            .compact();
}
 
Example #2
Source File: JsonWebTokenUtil.java    From sureness with Apache License 2.0 5 votes vote down vote up
/**
 *   json web token 签发
 * @param id 令牌ID
 * @param subject 用户ID
 * @param issuer 签发人
 * @param period 有效时间(毫秒)
 * @param roles 访问主张-角色
 * @param permissions 访问主张-权限
 * @param isRefresh 是否是刷新token
 * @param algorithm 加密算法
 * @return java.lang.String jwt
 */
public static String issueJwt(String id, String subject, String issuer, Long period,
                              List<String> roles, List<String> permissions,
                              Boolean isRefresh, SignatureAlgorithm algorithm) {
    // 当前时间戳
    long currentTimeMillis = System.currentTimeMillis();
    // 秘钥
    byte[] secretKeyBytes = DatatypeConverter.parseBase64Binary(secretKey);
    JwtBuilder jwtBuilder = Jwts.builder();
    if (id != null) {
        jwtBuilder.setId(id);
    }
    if (subject != null) {
        jwtBuilder.setSubject(subject);
    }
    if (issuer != null) {
        jwtBuilder.setIssuer(issuer);
    }
    // 设置签发时间
    jwtBuilder.setIssuedAt(new Date(currentTimeMillis));
    // 设置到期时间
    if (null != period) {
        jwtBuilder.setExpiration(new Date(currentTimeMillis + period * 1000));
    }
    if (roles != null) {
        jwtBuilder.claim("roles", roles);
    }
    if (permissions != null) {
        jwtBuilder.claim("perms", permissions);
    }
    if (isRefresh != null) {
        jwtBuilder.claim("isRefresh", isRefresh);
    }
    // 压缩,可选GZIP
    jwtBuilder.compressWith(CompressionCodecs.DEFLATE);
    // 加密设置
    jwtBuilder.signWith(algorithm, secretKeyBytes);
    return jwtBuilder.compact();
}
 
Example #3
Source File: ShiroUtils.java    From jsets-shiro-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * 签发JWT
 * 
 * @param subject
 *            用户名称
 * @param issuer
 *            签发人
 * @param period
 *            有效时间
 * @param roles
 *            访问主张-角色
 * @param permissions
 *            访问主张-资源
 * @param algorithm
 *            算法
 * @return JSON WEB TOKEN
 */
public static String issueJwt(String subject, String issuer, Long period, String roles, String permissions,
		SignatureAlgorithm algorithm) {
	// 当前时间戳(精确到毫秒)
	long currentTimeMillis = System.currentTimeMillis();
	// 秘钥
	byte[] secretKeyBytes = DatatypeConverter.parseBase64Binary(properties().getJwtSecretKey());
	JwtBuilder jwt = Jwts.builder();
	jwt.setId(UUID.randomUUID().toString());
	// 用户名
	jwt.setSubject(subject);
	// 签发者
	if (null != issuer && !"".equals(issuer))
		jwt.setIssuer(issuer);
	// 签发时间
	jwt.setIssuedAt(new Date(currentTimeMillis));
	// 有效时间
	if (null != period) {
		Date expiration = new Date(currentTimeMillis + period);
		jwt.setExpiration(expiration);
	}
	// 访问主张-角色
	if (null != roles && !"".equals(roles))
		jwt.claim("roles", roles);
	// 访问主张-权限
	if (null != permissions && !"".equals(permissions))
		jwt.claim("perms", permissions);
	jwt.compressWith(CompressionCodecs.DEFLATE);
	jwt.signWith(algorithm, secretKeyBytes);
	return jwt.compact();
}
 
Example #4
Source File: JwtTokenService.java    From secrets-proxy with Apache License 2.0 5 votes vote down vote up
/**
 * Generate a JWT token for the given user. The roles will be stored as a claim in JWT token as a
 * comma separated string.
 *
 * @param user authenticated user details object.
 * @return compact JWS (JSON Web Signature)
 */
public @Nonnull String generateToken(OneOpsUser user) {
  Instant now = Instant.now();
  Instant expiresIn = now.plusSeconds(expiresInSec);

  JwtBuilder jwt =
      Jwts.builder()
          .setSubject(user.getUsername())
          .setIssuer(issuer)
          .setIssuedAt(Date.from(now))
          .setExpiration(Date.from(expiresIn))
          .signWith(SIGNATURE_ALGORITHM, String.valueOf(secretKey));
  if (user.getAuthorities() != null) {
    List<String> roles =
        user.getAuthorities()
            .stream()
            .map(GrantedAuthority::getAuthority)
            .collect(Collectors.toList());
    jwt.claim(ROLE_CLAIM, String.join(",", roles));
  }
  if (user.getDomain() != null) {
    jwt.claim(DOMAIN_CLAIM, user.getDomain().getType());
  }
  if (user.getCn() != null) {
    jwt.claim(CN_CLAIM, user.getCn());
  }
  if (compressionEnabled) {
    jwt.compressWith(CompressionCodecs.DEFLATE);
  }
  return jwt.compact();
}
 
Example #5
Source File: DefaultCompressionCodecResolver.java    From jjwt with Apache License 2.0 5 votes vote down vote up
public DefaultCompressionCodecResolver() {
    Map<String, CompressionCodec> codecMap = new HashMap<>();
    for (CompressionCodec codec : Services.loadAll(CompressionCodec.class)) {
        codecMap.put(codec.getAlgorithmName().toUpperCase(), codec);
    }

    codecMap.put(CompressionCodecs.DEFLATE.getAlgorithmName().toUpperCase(), CompressionCodecs.DEFLATE);
    codecMap.put(CompressionCodecs.GZIP.getAlgorithmName().toUpperCase(), CompressionCodecs.GZIP);

    codecs = Collections.unmodifiableMap(codecMap);
}