Search in sources :

Example 1 with JwtBuilder

use of io.jsonwebtoken.JwtBuilder in project sonarqube by SonarSource.

the class JwtSerializer method encode.

String encode(JwtSession jwtSession) {
    checkIsStarted();
    long now = system2.now();
    JwtBuilder jwtBuilder = Jwts.builder().setId(uuidFactory.create()).setSubject(jwtSession.getUserLogin()).setIssuedAt(new Date(now)).setExpiration(new Date(now + jwtSession.getExpirationTimeInSeconds() * 1000)).signWith(SIGNATURE_ALGORITHM, secretKey);
    for (Map.Entry<String, Object> entry : jwtSession.getProperties().entrySet()) {
        jwtBuilder.claim(entry.getKey(), entry.getValue());
    }
    return jwtBuilder.compact();
}
Also used : JwtBuilder(io.jsonwebtoken.JwtBuilder) Map(java.util.Map) Date(java.util.Date)

Example 2 with JwtBuilder

use of io.jsonwebtoken.JwtBuilder in project sonarqube by SonarSource.

the class JwtSerializer method refresh.

String refresh(Claims token, int expirationTimeInSeconds) {
    checkIsStarted();
    long now = system2.now();
    JwtBuilder jwtBuilder = Jwts.builder();
    for (Map.Entry<String, Object> entry : token.entrySet()) {
        jwtBuilder.claim(entry.getKey(), entry.getValue());
    }
    jwtBuilder.setExpiration(new Date(now + expirationTimeInSeconds * 1000)).signWith(SIGNATURE_ALGORITHM, secretKey);
    return jwtBuilder.compact();
}
Also used : JwtBuilder(io.jsonwebtoken.JwtBuilder) Map(java.util.Map) Date(java.util.Date)

Example 3 with JwtBuilder

use of io.jsonwebtoken.JwtBuilder in project nymph by Onnt.

the class NymCode method encode.

/**
	  * 创建 jwt
	  * @param id
	  * @param subject
	  * @param ttlMillis
	  * @return
	  * @throws Exception
	  */
public String encode(String id, String subject) {
    SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
    long nowMillis = System.currentTimeMillis();
    Date now = new Date(nowMillis);
    SecretKey key = generalKey();
    JwtBuilder builder = Jwts.builder().setId(id).setIssuedAt(now).setSubject(subject).signWith(signatureAlgorithm, key);
    long ttlMillis = new Date().getTime();
    if (ttlMillis >= 0) {
        long expMillis = nowMillis + ttlMillis;
        Date exp = new Date(expMillis);
        builder.setExpiration(exp);
    }
    return builder.compact();
}
Also used : SecretKey(javax.crypto.SecretKey) SignatureAlgorithm(io.jsonwebtoken.SignatureAlgorithm) JwtBuilder(io.jsonwebtoken.JwtBuilder) Date(java.util.Date)

Aggregations

JwtBuilder (io.jsonwebtoken.JwtBuilder)3 Date (java.util.Date)3 Map (java.util.Map)2 SignatureAlgorithm (io.jsonwebtoken.SignatureAlgorithm)1 SecretKey (javax.crypto.SecretKey)1