本文整理汇总了Java中io.jsonwebtoken.Claims.get方法的典型用法代码示例。如果您正苦于以下问题:Java Claims.get方法的具体用法?Java Claims.get怎么用?Java Claims.get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.jsonwebtoken.Claims
的用法示例。
在下文中一共展示了Claims.get方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getConnUser
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public static RequestUserDTO getConnUser(HttpServletRequest request) {
String token = request.getHeader(HEADER_STRING);
if (token == null) {
token = getTokenFromCookis(request);
}
if (token != null) {
// 解析 Token
Claims claims = Jwts.parser().setSigningKey(SECRET)
.parseClaimsJws(token).getBody();
return new RequestUserDTO(
claims.get("DomainId", String.class),
claims.get("UserId", String.class),
claims.get("OrgUnitId", String.class));
}
return new RequestUserDTO();
}
示例2: authenticate
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
final JwtToken token = (JwtToken) authentication.getCredentials();
final Claims claims = jwt.validate(token);
final String userId = claims.getSubject();
final String email = claims.get("mail", String.class);
@SuppressWarnings("unchecked")
final List<String> scopes = (List<String>) claims.get("scopes", List.class);
final List<GrantedAuthority> auths = scopes.stream()
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());
final JwtUserDetails user = new JwtUserDetails(userId, email, auths);
return new JwtAuthentication(token, user);
}
示例3: parseAndValidate
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public AuthTokenDetails parseAndValidate(String token) {
AuthTokenDetails authTokenDetails = null;
try {
Claims claims = Jwts.parser().setSigningKey(getSecretKey())
.parseClaimsJws(token)
.getBody();
String userId = claims.getSubject();
String username = (String) claims.get("username");
List<String> roleNames = (List) claims.get("roleNames");
Date expirationDate = claims.getExpiration();
authTokenDetails = new AuthTokenDetails();
authTokenDetails.setId(Long.valueOf(userId));
authTokenDetails.setUsername(username);
authTokenDetails.setRoleNames(roleNames);
authTokenDetails.setExpirationDate(expirationDate);
} catch (JwtException ex) {
logger.error(ex.getMessage(), ex);
}
return authTokenDetails;
}
示例4: getPayload
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
/**
* Parses the token header into a payload data map
*
* @param tokenHeader Object holding the token
* @return data map of the token parsed
*/
private Map<String, String> getPayload(String tokenHeader) {
String tokenWithoutBearer = tokenHeader.replace(TOKEN_PREFIX, "");
String tokenWithoutSignatureAndBearer = removeSignature(tokenWithoutBearer);
Claims body = Jwts.parser()
.parseClaimsJwt(tokenWithoutSignatureAndBearer)
.getBody();
return body.get("data", Map.class);
}
示例5: getAudienceFromToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public String getAudienceFromToken(String token) {
String audience;
try {
final Claims claims = getClaimsFromToken(token);
audience = (String) claims.get(CLAIM_KEY_AUDIENCE);
} catch (Exception e) {
audience = null;
}
return audience;
}
示例6: login
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
@RequestMapping(value = "api/me", method = {RequestMethod.GET})
public User login(final HttpServletRequest request) throws ServletException {
final Claims claims = (Claims) request.getAttribute("claims");
final Integer userId = (Integer) claims.get("userId");
return rbacService.getUserByUserId(userId.longValue());
}
示例7: parseAccessJwtToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public SecurityUser parseAccessJwtToken(RawAccessJwtToken rawAccessToken) {
Jws<Claims> jwsClaims = rawAccessToken.parseClaims(settings.getTokenSigningKey());
Claims claims = jwsClaims.getBody();
String subject = claims.getSubject();
List<String> scopes = claims.get(SCOPES, List.class);
if (scopes == null || scopes.isEmpty()) {
throw new IllegalArgumentException("JWT Token doesn't have any scopes");
}
SecurityUser securityUser = new SecurityUser(new UserId(UUID.fromString(claims.get(USER_ID, String.class))));
securityUser.setEmail(subject);
securityUser.setAuthority(Authority.parse(scopes.get(0)));
securityUser.setFirstName(claims.get(FIRST_NAME, String.class));
securityUser.setLastName(claims.get(LAST_NAME, String.class));
securityUser.setEnabled(claims.get(ENABLED, Boolean.class));
boolean isPublic = claims.get(IS_PUBLIC, Boolean.class);
UserPrincipal principal = new UserPrincipal(isPublic ? UserPrincipal.Type.PUBLIC_ID : UserPrincipal.Type.USER_NAME,
subject);
securityUser.setUserPrincipal(principal);
String tenantId = claims.get(TENANT_ID, String.class);
if (tenantId != null) {
securityUser.setTenantId(new TenantId(UUID.fromString(tenantId)));
}
String customerId = claims.get(CUSTOMER_ID, String.class);
if (customerId != null) {
securityUser.setCustomerId(new CustomerId(UUID.fromString(customerId)));
}
return securityUser;
}
示例8: getLastPasswordResetDateFromTokenClaims
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
Date getLastPasswordResetDateFromTokenClaims(Claims claims) {
Date lastPasswordResetDate;
try {
lastPasswordResetDate = new Date((Long) claims.get(CLAIM_KEY_LAST_PASSWORD_CHANGE_DATE));
} catch (Exception e) {
LOG.error("Failed to extract lastPasswordResetDate claim from token!", e);
lastPasswordResetDate = null;
}
return lastPasswordResetDate;
}
示例9: getAudienceFromToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
/**
* @param token
* @return
*/
public String getAudienceFromToken ( String token ) {
String audience;
try {
final Claims claims = getClaimsFromToken( token );
audience = ( String ) claims.get( CLAIM_KEY_AUDIENCE );
} catch ( Exception e ) {
audience = null;
}
return audience;
}
示例10: getClaims
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
@Override
public Claims getClaims(String requestedPrincipal, String authToken) throws AuthenticationException, AuthorizationException {
Claims claims;
try {
claims = jwtDecoder.decodeAndVerify(authToken);
String principalIdFromToken = claims.get("usr", String.class);
if (!Objects.equals(requestedPrincipal, principalIdFromToken)) {
throw new AuthorizationException("User not authorized to perform action");
}
return claims;
} catch (JWTVerificationException e) {
throw new AuthenticationException("JWT Token not valid");
}
}
示例11: getUserIdFromToken
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
private String getUserIdFromToken(String token) {
String userId;
try {
final Claims clamis=getClaimsFromToken(token);
userId= (String) clamis.get(CLAIM_KEY_USER_ID);
}catch (Exception e){
userId=null;
}
return userId;
}
示例12: doFilterInternal
import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
//CORS
response.addHeader("Access-Control-Allow-Origin", "*");
if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equalsIgnoreCase(request.getMethod())) {
response.addHeader("Access-Control-Allow-Headers", "Authorization");
response.addHeader("Access-Control-Allow-Headers", "Content-Type");
response.addHeader("Access-Control-Max-Age", "1");
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
}
final String authHeader = request.getHeader(this.authHeader);
String username;
String authToken;
if (authHeader != null && authHeader.startsWith("Bearer ")) {
authToken = authHeader.substring(7);
try {
username = jwtTokenUtil.getUsernameFromToken(authToken);
} catch (Exception e) {
username = null;
authToken = null;
}
} else {
username = null;
authToken = null;
}
if (username != null && authToken != null && SecurityContextHolder.getContext().getAuthentication() == null) {
Claims claims = jwtTokenUtil.getAllClaimsFromToken(authToken);
if (jwtTokenUtil.validateToken(authToken)) {
List<String> rolesMap = claims.get("role", List.class);
List<Role> roles = new ArrayList<>();
for (String rolemap : rolesMap) {
roles.add(Role.valueOf(rolemap));
}
UserDetails userDetails = new JWTUser(
username, null, JWTUserFactory.mapToGrantedAuthorities(roles), claims.get("enable", Boolean.class)
);
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authentication);
}
}
if (!request.getMethod().equalsIgnoreCase("OPTIONS")) {
chain.doFilter(request, response);
}
}