本文整理匯總了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();
}