本文整理汇总了Java中io.jsonwebtoken.Claims.put方法的典型用法代码示例。如果您正苦于以下问题:Java Claims.put方法的具体用法?Java Claims.put怎么用?Java Claims.put使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.jsonwebtoken.Claims
的用法示例。
在下文中一共展示了Claims.put方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createRefreshToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public JwtToken createRefreshToken(UserContext userContext) {
if (StringUtils.isBlank(userContext.getTenantId()))
throw new IllegalArgumentException("Cannot create JWT Token without tenantId");
if (StringUtils.isBlank(userContext.getOrgId()))
throw new IllegalArgumentException("Cannot create JWT Token without orgId");
DateTime currentTime = new DateTime();
Claims claims = Jwts.claims().setSubject(userContext.getOrgId());
claims.put("scopes", userContext.getAuthorities().stream().map(s -> s.toString()).collect(Collectors.toList()));
claims.put("tenant", userContext.getTenantId());
String token = Jwts.builder()
.setClaims(claims)
.setIssuer(settings.getTokenIssuer())
.setId(UUID.randomUUID().toString())
.setIssuedAt(currentTime.toDate())
.setExpiration(currentTime.plusMinutes(settings.getRefreshTokenExpTime()).toDate())
.signWith(SignatureAlgorithm.HS512, settings.getTokenSigningKey())
.compact();
return new AccessJwtToken(token, claims);
}
示例2: createRefreshToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public JwtToken createRefreshToken(SecurityUser securityUser) {
if (StringUtils.isBlank(securityUser.getEmail())) {
throw new IllegalArgumentException("Cannot create JWT Token without username/email");
}
DateTime currentTime = new DateTime();
UserPrincipal principal = securityUser.getUserPrincipal();
Claims claims = Jwts.claims().setSubject(principal.getValue());
claims.put(SCOPES, Arrays.asList(Authority.REFRESH_TOKEN.name()));
claims.put(USER_ID, securityUser.getId().getId().toString());
claims.put(IS_PUBLIC, principal.getType() == UserPrincipal.Type.PUBLIC_ID);
String token = Jwts.builder().setClaims(claims).setIssuer(settings.getTokenIssuer())
.setId(UUID.randomUUID().toString()).setIssuedAt(currentTime.toDate())
.setExpiration(currentTime.plusSeconds(settings.getRefreshTokenExpTime()).toDate())
.signWith(SignatureAlgorithm.HS512, settings.getTokenSigningKey()).compact();
return new AccessJwtToken(token, claims);
}
示例3: createRefreshToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public JwtToken createRefreshToken(UserContext userContext) {
if (StringUtils.isBlank(userContext.getUsername())) {
throw new IllegalArgumentException("Cannot create JWT Token without username");
}
LocalDateTime currentTime = LocalDateTime.now();
Claims claims = Jwts.claims().setSubject(userContext.getUsername());
claims.put("scopes", Arrays.asList(Scopes.REFRESH_TOKEN.authority()));
String token = Jwts.builder()
.setClaims(claims)
.setIssuer(AppConfig.prop.getProperty("security.tokenIssuer"))
.setId(UUID.randomUUID().toString())
.setIssuedAt(Date.from(currentTime.atZone(ZoneId.systemDefault()).toInstant()))
.setExpiration(Date.from(currentTime
.plusMinutes(Long.parseLong(AppConfig.prop.getProperty("security.refreshTokenExpTime")))
.atZone(ZoneId.systemDefault()).toInstant()))
.signWith(SignatureAlgorithm.HS512, AppConfig.prop.getProperty("security.tokenSigningKey"))
.compact();
return new AccessJwtToken(token, claims);
}
示例4: getEnterpriseToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
/**
* Get the enterprise token witch can used to invoke admin api,such as managing departments and groups
*
* @param enterpriseId Your enterprise id
* @param expirationTimeSeconds Expiration time seconds in the future(can not be bigger than 60)
* @return Detailed user access information
* @throws YfyException
*/
public YfyAuthFinish getEnterpriseToken(long enterpriseId, int expirationTimeSeconds) throws YfyException {
Claims claims = new DefaultClaims();
claims.put("yifangyun_sub_type", "enterprise");
claims.setSubject(String.valueOf(enterpriseId));
claims.setExpiration(getExpirationTimeSecondsInTheFuture(expirationTimeSeconds));
claims.setIssuedAt(new Date());
claims.setId(getGeneratedJwtId(16));
final String compactJws = Jwts.builder().setHeader(headers).setClaims(claims).signWith(SignatureAlgorithm.RS256, key).compact();
return YfyRequestUtil.doPostInAuth(
requestConfig,
YfyAppInfo.getHost().getAuth(),
"oauth/token",
new HashMap<String, String>() {{
put("grant_type", "jwt");
put("assertion", compactJws);
}},
YfyAuthFinish.class);
}
示例5: addAuthentication
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
void addAuthentication(HttpServletResponse response, String username, Collection<? extends GrantedAuthority> authorities) throws IOException {
List<String> roles = authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
Claims claims = Jwts.claims()
.setSubject(username)
.setExpiration(new Date(System.currentTimeMillis() + expirationTime * 60 * 1000));
claims.put(ROLE_KEY, roles.stream().collect(Collectors.joining(ROLE_DELIMITER)));
String JWT = Jwts.builder()
.setClaims(claims)
.signWith(SignatureAlgorithm.HS512, secret)
.compact();
response.addHeader(headerString, headerStartWith + JWT);
JwtAuthenticatedUser user = new JwtAuthenticatedUser(username, roles);
PrintWriter printWriter = response.getWriter();
printWriter.print(mapper.writeValueAsString(user));
printWriter.flush();
}
示例6: createJwtToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
@Override
public String createJwtToken(Authentication authentication, int minutes) {
Claims claims = Jwts.claims()
.setId(String.valueOf(IdentityGenerator.generate()))
.setSubject(authentication.getName())
.setExpiration(new Date(currentTimeMillis() + minutes * 60 * 1000))
.setIssuedAt(new Date());
String authorities = authentication.getAuthorities()
.stream()
.map(GrantedAuthority::getAuthority)
.map(String::toUpperCase)
.collect(Collectors.joining(","));
claims.put(AUTHORITIES, authorities);
return Jwts.builder()
.setClaims(claims)
.signWith(HS512, secretkey)
.compact();
}
示例7: createAccessJwtToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
/**
* Factory method for issuing new JWT Tokens.
*
* @param username
* @param roles
* @return
*/
public AccessJwtToken createAccessJwtToken(UserContext userContext) {
if (StringUtils.isBlank(userContext.getUsername()))
throw new IllegalArgumentException("Cannot create JWT Token without username");
if (userContext.getAuthorities() == null || userContext.getAuthorities().isEmpty())
throw new IllegalArgumentException("User doesn't have any privileges");
Claims claims = Jwts.claims().setSubject(userContext.getUsername());
claims.put("scopes", userContext.getAuthorities().stream().map(s -> s.toString()).collect(Collectors.toList()));
LocalDateTime currentTime = LocalDateTime.now();
String token = Jwts.builder()
.setClaims(claims)
.setIssuer(AppConfig.prop.getProperty("security.tokenIssuer"))
.setIssuedAt(Date.from(currentTime.atZone(ZoneId.systemDefault()).toInstant()))
.setExpiration(Date.from(currentTime
.plusMinutes(Long.parseLong(AppConfig.prop.getProperty("security.tokenExpirationTime")))
.atZone(ZoneId.systemDefault()).toInstant()))
.signWith(SignatureAlgorithm.HS512, AppConfig.prop.getProperty("security.tokenSigningKey"))
.compact();
return new AccessJwtToken(token, claims);
}
示例8: refreshToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public String refreshToken(String token) {
String refreshedToken;
try {
final Claims claims = getClaimsFromToken(token);
claims.put(CLAIM_KEY_CREATED, timeProvider.now());
refreshedToken = doGenerateToken(claims);
} catch (Exception e) {
refreshedToken = null;
}
return refreshedToken;
}
示例9: refreshToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public String refreshToken(String token) {
String refreshedToken;
try {
final Claims claims = getClaimsFromToken(token);
claims.put(CLAIM_KEY_CREATED, new Date());
refreshedToken = generateToken(claims);
} catch (Exception e) {
refreshedToken = null;
}
return refreshedToken;
}
示例10: createToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public String createToken(String username, List<Role> roles) {
Claims claims = Jwts.claims().setSubject(username);
claims.put("auth", roles.stream().map(s -> new SimpleGrantedAuthority(s.getAuthority())).filter(Objects::nonNull).collect(Collectors.toList()));
Date now = new Date();
Date validity = new Date(now.getTime() + validityInMilliseconds);
return Jwts.builder()//
.setClaims(claims)//
.setIssuedAt(now)//
.setExpiration(validity)//
.signWith(SignatureAlgorithm.HS256, secretKey)//
.compact();
}
示例11: refreshToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
/**
* Cria um novo token (refresh).
*
* @param token
* @return String
*/
public String refreshToken(String token) {
String refreshedToken;
try {
Claims claims = getClaimsFromToken(token);
claims.put(CLAIM_KEY_CREATED, new Date());
refreshedToken = gerarToken(claims);
} catch (Exception e) {
refreshedToken = null;
}
return refreshedToken;
}
示例12: refreshToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
/**
* 刷新token
*
* @param token
* @return
*/
public String refreshToken ( String token ) {
String refreshedToken;
try {
final Claims claims = getClaimsFromToken( token );
claims.put( CLAIM_KEY_CREATED , new Date() );
refreshedToken = generateToken( claims );
} catch ( Exception e ) {
refreshedToken = null;
}
return refreshedToken;
}
示例13: createAccessJwtToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
/**
* Factory method for issuing new JWT Tokens.
*
* @param username
* @param roles
* @return
*/
public AccessJwtToken createAccessJwtToken(UserContext userContext) {
if (StringUtils.isBlank(userContext.getTenantId()))
throw new IllegalArgumentException("Cannot create JWT Token without tenantId");
if (StringUtils.isBlank(userContext.getOrgId()))
throw new IllegalArgumentException("Cannot create JWT Token without orgId");
if (userContext.getAuthorities() == null || userContext.getAuthorities().isEmpty())
throw new IllegalArgumentException("User doesn't have any privileges");
Claims claims = Jwts.claims().setSubject(userContext.getOrgId());
claims.put("scopes", userContext.getAuthorities().stream().map(s -> s.toString()).collect(Collectors.toList()));
claims.put("tenant", userContext.getTenantId());
DateTime currentTime = new DateTime();
String token = Jwts.builder()
.setClaims(claims)
.setIssuer(settings.getTokenIssuer())
.setIssuedAt(currentTime.toDate())
.setExpiration(currentTime.plusMinutes(settings.getTokenExpirationTime()).toDate())
.signWith(SignatureAlgorithm.HS512, settings.getTokenSigningKey())
.compact();
return new AccessJwtToken(token, claims);
}