当前位置: 首页>>代码示例>>Java>>正文


Java JwtBuilder.setExpiration方法代码示例

本文整理汇总了Java中io.jsonwebtoken.JwtBuilder.setExpiration方法的典型用法代码示例。如果您正苦于以下问题:Java JwtBuilder.setExpiration方法的具体用法?Java JwtBuilder.setExpiration怎么用?Java JwtBuilder.setExpiration使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.jsonwebtoken.JwtBuilder的用法示例。


在下文中一共展示了JwtBuilder.setExpiration方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: Gerate

import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
public static String Gerate(String issuer, int idSubject, int hours) {

        //The JWT signature algorithm we will be using to sign the token
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        
        //Hours to milliseconds
        long ttlMillis = hours * 3600000;
        String subject = String.valueOf(idSubject);
        
        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);
        
        //We will sign our JWT with our ApiKey secret
        byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(Parameters.TOKENKEY);
        Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());

        //Let's set the JWT Claims
        JwtBuilder builder = Jwts.builder().setIssuedAt(now)
                                    .setSubject(subject)
                                    .setIssuer(issuer)
                                    .signWith(signatureAlgorithm, signingKey);

        //if it has been specified, let's add the expiration
        if (ttlMillis >= 0) {
        long expMillis = nowMillis + ttlMillis;
            Date exp = new Date(expMillis);
            builder.setExpiration(exp);
        }

        //Builds the JWT and serializes it to a compact, URL-safe string
        return builder.compact();
    }
 
开发者ID:Montanheiro,项目名称:SistemaAlmoxarifado,代码行数:33,代码来源:Token.java

示例2: generate

import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
@Override
public TokenDto generate(final String username, final String password) {
    if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
        throw new BadCredentialsException("Input data can't be empty.");
    }
    final User user = userService.findByUsername(username);

    validateInputPassword(user.getPassword(), password);

    final Map<String, Object> tokenData = new HashMap<>();
    tokenData.put("username", user.getUsername());
    tokenData.put("password", user.getPassword());
    tokenData.put("create_date", LocalDateTime.now());
    final JwtBuilder jwtBuilder = Jwts.builder();
    jwtBuilder.setClaims(tokenData);
    final Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MINUTE, expirationTime);
    jwtBuilder.setExpiration(calendar.getTime());
    final String token = jwtBuilder.signWith(SignatureAlgorithm.HS512, secretKey).compact();
    return new TokenDto(token, mapper.map(user, UserDto.class));
}
 
开发者ID:akraskovski,项目名称:product-management-system,代码行数:22,代码来源:JwtService.java

示例3: createJWT

import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
/**
 * 创建jwt
 * @param id
 * @param subject
 * @param ttlMillis
 * @return
 * @throws Exception
 */
public String createJWT(String id, String subject, long ttlMillis) throws Exception {

    SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS512;
    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);
    if (ttlMillis >= 0) {
        long expMillis = nowMillis + ttlMillis;
        Date exp = new Date(expMillis);
        builder.setExpiration(exp);
    }
    return builder.compact();
}
 
开发者ID:TomChen001,项目名称:xmanager,代码行数:27,代码来源:JwtUtil.java

示例4: createJWT

import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
public String createJWT(String id, String subject, long ttlMillis) {
    SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
    long nowMillis = System.currentTimeMillis();
    Date now = new Date(nowMillis);
    SecretKey key = jwtConfig.generalKey();
    JwtBuilder builder = Jwts.builder()
            .setId(id)
            .setIssuedAt(now)
            .setSubject(subject)
            .signWith(signatureAlgorithm, key);
    if (ttlMillis >= 0) {
        long expMillis = nowMillis + ttlMillis;
        Date exp = new Date(expMillis);
        builder.setExpiration(exp);
    }
    return builder.compact();
}
 
开发者ID:hs-web,项目名称:hsweb-framework,代码行数:18,代码来源:JwtTokenGenerator.java

示例5: login

import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
@Path("login")
@POST
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public String login(@FormParam(value = "id") String id) throws IOException {
	SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
	 
    long nowMillis = System.currentTimeMillis();
    Date now = new Date(nowMillis);
 
    byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(SECRECT);
    Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());
 
    JwtBuilder builder = Jwts.builder()
                                .setIssuedAt(now)
                                .setIssuer(id)
                                .signWith(signatureAlgorithm, signingKey);
 
    long expMillis = nowMillis + EXPIRE	;
       Date exp = new Date(expMillis);
       builder.setExpiration(exp);
     
    return builder.compact();
}
 
开发者ID:nphau,项目名称:Angular2-OnlineCinema,代码行数:24,代码来源:AccountController.java

示例6: createUserJWT

import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
/**
 * Creates the jwt for the given dawg credentials
 * @param dawgCreds The credentials to encode
 * @return
 */
public String createUserJWT(DawgCreds dawgCreds) {

    long nowMillis = System.currentTimeMillis();
    Date now = new Date(nowMillis);
    Key signingKey = new SecretKeySpec(jwtSecret.getBytes(), SIGNATURE_ALG.getJcaName());
    Map<String, Object> claims = new HashMap<String, Object>();
    claims.put(DawgJwt.JWT_FIELD_CREDS, dawgCreds);

    JwtBuilder builder = Jwts.builder()
            .setClaims(claims)
            .setId(UUID.randomUUID().toString())
            .setIssuedAt(now) 
            .setSubject(JWT_SUBJECT)
            .setIssuer(this.jwtIssuer)
            .signWith(SIGNATURE_ALG, signingKey);

    if (this.ttlMillis >= 0) {
        long expMillis = nowMillis + this.ttlMillis;
        Date exp = new Date(expMillis);
        builder.setExpiration(exp);
    }

    return builder.compact();
}
 
开发者ID:Comcast,项目名称:dawg,代码行数:30,代码来源:DawgJwtEncoder.java

示例7: createJWT

import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
private JwtToken createJWT(String id,
                           String issuer,
                           String subject,
                           String privileges,
                           long ttlMillis) {

    // The JWT signature algorithm we will be using to sign the token
    final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.RS256;

    final long nowMillis = System.currentTimeMillis();
    final Date now = new Date(nowMillis);

    // We will sign our JWT with our ApiKey secret
    final Key signingKey = EncryptionUtil.getPrivateKey(
        env.getProperty("service.jwt.secret"));

    final Map<String, Object> claims = new HashMap<>();
    claims.put("privileges", privileges);

    // Let's set the JWT Claims
    final JwtBuilder builder = Jwts.builder()
        .setClaims(claims)
        .setId(id)
        .setIssuer(issuer)
        .setIssuedAt(now)
        .setSubject(subject)
        .signWith(signatureAlgorithm, signingKey);

    // If it has been specified, let's add the expiration
    if (ttlMillis >= 0) {
        long expMillis = nowMillis + ttlMillis;
        Date exp = new Date(expMillis);
        builder.setExpiration(exp);
    }

    // Builds the JWT and serializes it to a compact, URL-safe string
    return new JwtToken(builder.compact());
}
 
开发者ID:membaza,项目名称:users-service,代码行数:39,代码来源:JwtServiceImpl.java

示例8: createToken

import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
/**
 * Creates a new JWT for the specified principal. Token is signed using
 * the SecretKey with an HMAC 256 algorithm.
 *
 * @param principal the Principal to create the token for
 * @return a String representation of the generated token
 * @since 1.0.0
 */
public String createToken(Principal principal) {
    final Date today = new Date();
    final JwtBuilder jwtBuilder = Jwts.builder();
    jwtBuilder.setSubject(principal.getName());
    jwtBuilder.setIssuer("Alpine");
    jwtBuilder.setIssuedAt(today);
    jwtBuilder.setExpiration(addDays(today, 7));
    return jwtBuilder.signWith(SignatureAlgorithm.HS256, key).compact();
}
 
开发者ID:stevespringett,项目名称:Alpine,代码行数:18,代码来源:JsonWebToken.java

示例9: getToken

import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
@Override
public String getToken(final String username, final String password) {
    if (username == null || password == null) {
        return null;
    }
    final User user = (User) userDetailsService.loadUserByUsername(username);
    Map<String, Object> tokenData = new HashMap<>();
    if (password.equals(user.getPassword())) {
        tokenData.put("clientType", "user");
        tokenData.put("userID", user.getId());
        tokenData.put("username", user.getUsername());
        tokenData.put("token_create_date", LocalDateTime.now());
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, tokenExpirationTime);
        tokenData.put("token_expiration_date", calendar.getTime());
        JwtBuilder jwtBuilder = Jwts.builder();
        jwtBuilder.setExpiration(calendar.getTime());
        jwtBuilder.setClaims(tokenData);
        return jwtBuilder.signWith(SignatureAlgorithm.HS512, tokenKey).compact();

    } else {
        throw new ServiceException("Authentication error", this.getClass().getName());
    }
}
 
开发者ID:vlsidlyarevich,项目名称:Spring-Boot-MongoDB-JWT,代码行数:25,代码来源:JsonWebTokenService.java

示例10: jwtBuilder

import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
protected JwtBuilder jwtBuilder(long exp, Map<String, Object> ext){
    JwtBuilder jwt = Jwts.builder()
            .claim("user_id","43FE6476-CD7B-493B-8044-C7E3149D0876")
            .claim("scope","perm")
            .claim("client_id","console")
            .claim("username","admin");
    if(ext != null){
        for (Entry<String, Object> entry : ext.entrySet()){
            jwt.claim(entry.getKey(),entry.getValue());
        }
    }
    jwt.setExpiration(new Date(exp));
    return jwt;
}
 
开发者ID:bingo-oss,项目名称:sso-client,代码行数:15,代码来源:SSOClientTest.java

示例11: createJWT

import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
private static JwtBuilder createJWT(String id, String subject, String issuer, long timeToLiveMs) {

		long nowMs = System.currentTimeMillis();
		Date now = new Date(nowMs);

		JwtBuilder builder = Jwts.builder();
		builder.setIssuedAt(now);

		if (id != null) {
			builder.setId(id);
		}

		if (subject != null) {
			builder.setSubject(subject);
		}

		if (issuer != null) {
			builder.setIssuer(issuer);
		}

		if (timeToLiveMs >= 0) {
			long expireMs = nowMs + timeToLiveMs;
			builder.setExpiration(new Date(expireMs));
		}

		return builder;

	}
 
开发者ID:holon-platform,项目名称:holon-core,代码行数:29,代码来源:JwtTokenBuilder.java

示例12: modifyTokenExpirationTime

import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
private String modifyTokenExpirationTime(final String token) {
    final Jws<Claims> tokenData = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token);
    final JwtBuilder jwtBuilder = Jwts.builder();
    final Calendar calendar = Calendar.getInstance();

    jwtBuilder.setClaims(tokenData.getBody());
    calendar.add(Calendar.MILLISECOND, 1);
    jwtBuilder.setExpiration(calendar.getTime());
    return jwtBuilder.signWith(SignatureAlgorithm.HS512, secretKey).compact();
}
 
开发者ID:akraskovski,项目名称:product-management-system,代码行数:11,代码来源:TokenServiceTest.java

示例13: prepareJwtBuilder

import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
private void prepareJwtBuilder(final JwtBuilder jwtBuilder) {
    final Calendar calendar = Calendar.getInstance();
    jwtBuilder.setExpiration(calendar.getTime());
}
 
开发者ID:vlsidlyarevich,项目名称:unity,代码行数:5,代码来源:JsonWebTokenService.java


注:本文中的io.jsonwebtoken.JwtBuilder.setExpiration方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。