Java Code Examples for com.auth0.jwt.interfaces.DecodedJWT#getClaims()

The following examples show how to use com.auth0.jwt.interfaces.DecodedJWT#getClaims() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JwtApplication.java    From spring-boot-study with MIT License 6 votes vote down vote up
/**
 * 验证 token
 * */
private static void verifyJWTToken(String token) throws JWTVerificationException {
    Algorithm algorithm=Algorithm.HMAC256("secret");
    JWTVerifier verifier = JWT.require(algorithm)
            .withIssuer("SERVICE")
            .build();

    DecodedJWT jwt =verifier.verify(token);
    String subject=jwt.getSubject();
    Map<String,Claim> claims=jwt.getClaims();
    Claim claim = claims.get("loginName");
    System.out.println("自定义 claim:"+claim.asString());

    List<String> audience = jwt.getAudience();
    System.out.println("subject 值:"+subject);
    System.out.println("audience 值:"+audience.get(0));
}
 
Example 2
Source File: Token.java    From Mall-Server with MIT License 6 votes vote down vote up
/**
 * 解密token
 * @param token jwt类型的token
 * @param classT 加密时的类型
 * @param <T>
 * @return 返回解密后的对象 - 如果token过期返回空对象
 */
public static <T> T validToken(String token, Class<T> classT)  {
    DecodedJWT decode = null;
    try {
        decode = JWT.decode(token);
        Map<String, Claim> claims = decode.getClaims();
        if (claims.containsKey(EXP) && claims.containsKey(PAYLOAD)){
            long tokenTime = claims.get(EXP).asDate().getTime();
            long nowTime = new Date().getTime();
            // 判断令牌是否超时
            if (tokenTime > nowTime){
                String json = claims.get(PAYLOAD).asString();
                if (classT != null) {
                    return JSON.parseObject(json, classT);
                } else {
                    return (T) JSON.parse(json);
                }
            }
        }
    } catch (Exception e) {
        System.out.println(e);
        return null;
    }
    return null;
}
 
Example 3
Source File: JWTAuthentication.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 解析JWT,获取claims
 * @param jwtStr:待解密的jwt
 * @return
 */
public static Map<String, String> parseJwtToClaims(String jwtStr)  {
	DecodedJWT jwt = JWT.decode(jwtStr);
	Map<String, Claim> map = jwt.getClaims();
	Map<String, String> resultMap = Maps.newHashMap();
	map.forEach((k,v) -> resultMap.put(k, v.asString()));
	return resultMap;
}
 
Example 4
Source File: JwtHelper.java    From BigDataPlatform with GNU General Public License v3.0 5 votes vote down vote up
public Integer verifyTokenAndGetUserId(String token) {
		try {
		    Algorithm algorithm = Algorithm.HMAC256(SECRET);
		    JWTVerifier verifier = JWT.require(algorithm)
		        .withIssuer(ISSUSER)
		        .build();
		    DecodedJWT jwt = verifier.verify(token);
		    Map<String, Claim> claims = jwt.getClaims();
		    Claim claim = claims.get("userId");
		    return claim.asInt();
		} catch (JWTVerificationException exception){
//			exception.printStackTrace();
		}

		return 0;
	}
 
Example 5
Source File: JwtPrincipal.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
public JwtPrincipal(DecodedJWT decodedJWT, String credentials) {
    this.hashcode = MurmurHash3.hash32(credentials);
    this.subject = decodedJWT.getSubject();
    this.audience = decodedJWT.getAudience();
    Map<String, Claim> claims = decodedJWT.getClaims();
    this.serviceAccounts = new HashSet<>(decodedJWT.getClaim("sas").asList(String.class));
    this.organizations = new HashSet<>(decodedJWT.getClaim("orgs").asList(String.class));
    if (claims.containsKey("roles")) {
        this.roles = new HashSet<>(decodedJWT.getClaim("roles").asList(String.class));
    }
    if (claims.containsKey("authorities")) {
        this.authorities = new HashSet<>(decodedJWT.getClaim("authorities").asList(String.class));
    }
}
 
Example 6
Source File: JwtManager.java    From Mars-Java with MIT License 5 votes vote down vote up
/**
 * 解密Token
 *
 * @param token
 * @return map
 */
private Map<String, Claim> decryptToken(String token) {
    DecodedJWT jwt = null;
    try {
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
        jwt = verifier.verify(token);
        return jwt.getClaims();
    } catch (Exception e) {
        return null;
    }
}
 
Example 7
Source File: JwtTokenService.java    From singleton with Eclipse Public License 2.0 5 votes vote down vote up
public  Map<String, Claim> verifyToken(String token) throws Exception{
	JWTVerifier verifier = null;
	verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
	DecodedJWT decoded = null;
	try {
	    decoded = verifier.verify(token);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		logger.error(e.getMessage(), e);
		throw new RuntimeException(e);
	} 

  return decoded.getClaims();
}
 
Example 8
Source File: JWTAuthentication.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 解析JWT,获取claims
 * @param jwtStr:待解密的jwt
 * @return
 */
public static Map<String, String> parseJwtToClaims(String jwtStr)  {
	DecodedJWT jwt = JWT.decode(jwtStr);
	Map<String, Claim> map = jwt.getClaims();
	Map<String, String> resultMap = Maps.newHashMap();
	map.forEach((k,v) -> resultMap.put(k, v.asString()));
	return resultMap;
}
 
Example 9
Source File: JwtHelper.java    From litemall with MIT License 5 votes vote down vote up
public Integer verifyTokenAndGetUserId(String token) {
		try {
		    Algorithm algorithm = Algorithm.HMAC256(SECRET);
		    JWTVerifier verifier = JWT.require(algorithm)
		        .withIssuer(ISSUSER)
		        .build();
		    DecodedJWT jwt = verifier.verify(token);
		    Map<String, Claim> claims = jwt.getClaims();
		    Claim claim = claims.get("userId");
		    return claim.asInt();
		} catch (JWTVerificationException exception){
//			exception.printStackTrace();
		}
		
		return 0;
	}
 
Example 10
Source File: ClaimsIdentity.java    From botbuilder-java with MIT License 5 votes vote down vote up
/**
 * Extract data from an auth0 JWT.
 * 
 * @param jwt The decoded JWT.
 */
public ClaimsIdentity(DecodedJWT jwt) {
    claims = new HashMap<>();
    if (jwt.getClaims() != null) {
        jwt.getClaims().forEach((k, v) -> claims.put(k, v.asString()));
    }
    issuer = jwt.getIssuer();
}
 
Example 11
Source File: JwtUtil.java    From demo-project with MIT License 5 votes vote down vote up
/**
 * Description: 解密jwt
 *
 * @param token  token
 * @param secret secret
 * @return java.util.Map<java.lang.String                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               com.auth0.jwt.interfaces.Claim>
 * @author fanxb
 * @date 2019/3/4 18:14
 */
public static Map<String, Claim> decode(String token, String secret) {
    if (token == null || token.length() == 0) {
        throw new CustomException("token为空:" + token);
    }
    Algorithm algorithm = Algorithm.HMAC256(secret);
    JWTVerifier jwtVerifier = JWT.require(algorithm).build();
    DecodedJWT decodedJWT = jwtVerifier.verify(token);
    return decodedJWT.getClaims();
}
 
Example 12
Source File: AuthenticationFactory.java    From MicroCommunity with Apache License 2.0 5 votes vote down vote up
/**
 * 校验Token
 *
 * @param token
 * @return
 * @throws Exception
 */
public static Map<String, String> verifyToken(String token) throws Exception {
    String jwtSecret = MappingCache.getValue(MappingConstant.KEY_JWT_SECRET);
    if (StringUtil.isNullOrNone(jwtSecret)) {
        jwtSecret = CommonConstant.DEFAULT_JWT_SECRET;
    }
    Algorithm algorithm = Algorithm.HMAC256(jwtSecret);
    JWTVerifier verifier = JWT.require(algorithm).withIssuer("java110").build();
    DecodedJWT jwt = verifier.verify(token);
    String jdi = jwt.getId();
    //保存token Id
    String userId = JWTCache.getValue(jdi);
    if (StringUtil.isNullOrNone(userId)) {
        throw new JWTVerificationException("用户还未登录");
    }
    String expireTime = MappingCache.getValue(MappingConstant.KEY_JWT_EXPIRE_TIME);
    if (StringUtil.isNullOrNone(expireTime)) {
        expireTime = CommonConstant.DEFAULT_JWT_EXPIRE_TIME;
    }
    //刷新过时时间
    JWTCache.resetExpireTime(jdi, Integer.parseInt(expireTime));
    Map<String, Claim> claims = jwt.getClaims();
    // Add the claim to request header
    Map<String, String> paramOut = new HashMap<String, String>();
    for (String key : claims.keySet()) {
        paramOut.put(key, claims.get(key).asString());
    }
    paramOut.put(CommonConstant.LOGIN_USER_ID, userId);
    return paramOut;
}
 
Example 13
Source File: JwtSessionConfigurator.java    From aceql-http with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
    * Extracts the username from the decoded JWT.
    */
   @Override
   public String getUsername(String sessionId) {
try {
    DecodedJWT jwt = JWT.decode(sessionId);
    Map<String, Claim> claims = jwt.getClaims(); // Key is the Claim
						 // name
    Claim claim = claims.get("usr");
    return claim.asString();

} catch (JWTDecodeException exception) {
    exception.printStackTrace();
    return null;
}
   }
 
Example 14
Source File: JwtSessionConfigurator.java    From aceql-http with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
    * Extracts the Database from the decoded JWT.
    */
   @Override
   public String getDatabase(String sessionId) {
try {
    DecodedJWT jwt = JWT.decode(sessionId);
    Map<String, Claim> claims = jwt.getClaims(); // Key is the Claim
						 // name
    Claim claim = claims.get("dbn");
    return claim.asString();

} catch (JWTDecodeException exception) {
    System.err.println(exception);
    return null;
}
   }
 
Example 15
Source File: JwtUtil.java    From bookmark with MIT License 5 votes vote down vote up
/**
 * Description: 解密jwt
 *
 * @param token  token
 * @param secret secret
 * @return java.util.Map<java.lang.String                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               com.auth0.jwt.interfaces.Claim>
 * @author fanxb
 * @date 2019/3/4 18:14
 */
public static Map<String, Claim> decode(String token, String secret) {
    if (token == null || token.length() == 0) {
        throw new CustomException("token为空:" + token);
    }
    Algorithm algorithm = Algorithm.HMAC256(secret);
    JWTVerifier jwtVerifier = JWT.require(algorithm).build();
    DecodedJWT decodedJWT = jwtVerifier.verify(token);
    return decodedJWT.getClaims();
}