本文整理汇总了Java中io.jsonwebtoken.Jwts.builder方法的典型用法代码示例。如果您正苦于以下问题:Java Jwts.builder方法的具体用法?Java Jwts.builder怎么用?Java Jwts.builder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.jsonwebtoken.Jwts
的用法示例。
在下文中一共展示了Jwts.builder方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generate
import io.jsonwebtoken.Jwts; //导入方法依赖的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));
}
示例2: createToken
import io.jsonwebtoken.Jwts; //导入方法依赖的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();
}
示例3: createJWT
import io.jsonwebtoken.Jwts; //导入方法依赖的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;
}
示例4: generate
import io.jsonwebtoken.Jwts; //导入方法依赖的package包/类
public static String generate(String shopperId, Integer validitySeconds) throws IOException {
JwtBuilder builder = Jwts.builder();
String apiKey = Lightrail.apiKey;
String secret = Lightrail.clientSecret;
if (apiKey == null)
throw new BadParameterException("Lightrail.apiKey is not set.");
if (secret == null)
throw new BadParameterException("Lightrail.clientSecret is not set.");
String payload = apiKey.substring(apiKey.indexOf(".") + 1);
payload = payload.substring(0, payload.indexOf("."));
payload = new String(new BASE64Decoder().decodeBuffer(payload));
JsonObject jsonObject = new Gson().fromJson(payload, JsonObject.class);
String gui = jsonObject.get("g").getAsJsonObject().get("gui").getAsString();
Map<String, Object> claims = new HashMap<String, Object>();
Long iat = System.currentTimeMillis()/1000;
claims.put("iat", iat);
claims.put("shopperId", shopperId);
Map<String, Object> gClaims = new HashMap<String, Object>();
gClaims.put("gui", gui);
claims.put("g", gClaims);
if (validitySeconds != null) {
Long exp = iat + validitySeconds;
claims.put("exp", exp);
}
return builder.setClaims(claims)
.setHeaderParam("typ", "JWT")
.signWith(SignatureAlgorithm.HS256, secret.getBytes("UTF-8"))
.compact();
}
示例5: getToken
import io.jsonwebtoken.Jwts; //导入方法依赖的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());
}
}
示例6: modifyTokenExpirationTime
import io.jsonwebtoken.Jwts; //导入方法依赖的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();
}