本文整理汇总了Java中com.auth0.jwt.interfaces.DecodedJWT.getClaim方法的典型用法代码示例。如果您正苦于以下问题:Java DecodedJWT.getClaim方法的具体用法?Java DecodedJWT.getClaim怎么用?Java DecodedJWT.getClaim使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.auth0.jwt.interfaces.DecodedJWT
的用法示例。
在下文中一共展示了DecodedJWT.getClaim方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validateToken
import com.auth0.jwt.interfaces.DecodedJWT; //导入方法依赖的package包/类
private User validateToken(String token) {
try {
Algorithm algorithm = Algorithm.HMAC256(Constants.JWT_TOKEN_KEY);
JWTVerifier verifier = JWT.require(algorithm)
.withIssuer("jwtauth")
.build(); //Reusable verifier instance
DecodedJWT jwt = verifier.verify(token);
//Get the userId from token claim.
Claim userId = jwt.getClaim("userId");
// Find user by token subject(id).
UserDao userDao = new UserDao();
return userDao.findUserById(userId.asLong());
} catch (UnsupportedEncodingException | JWTVerificationException e){
LOGGER.error(e.getMessage(), e);
}
return null;
}
示例2: authenticate
import com.auth0.jwt.interfaces.DecodedJWT; //导入方法依赖的package包/类
@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
JWTAuthenticationToken authentication = (JWTAuthenticationToken) auth;
if(authentication.getCredentials() == null || !(authentication.getCredentials() instanceof String)) {
logger.error("Credentials not present");
return null;
}
String rawToken = (String) auth.getCredentials();
DecodedJWT token = JWTSecurityService.verify(jwtKey, rawToken);
Claim path = token.getClaim(JWTSecurityService.CLAIM_PATH);
authentication.setAuthenticated(true);
// TODO:AD This is super unfortunate, but not sure there is a better way when using JSP
if(StringUtils.contains(authentication.getRequestedPath(), "/WEB-INF/jsp/")) {
logger.warn("BYPASSING AUTH FOR WEB-INF page");
} else
if(!roughlyEqual(path.asString(), authentication.getRequestedPath())) {
throw new InsufficientAuthenticationException("Credentials not valid for path " + authentication
.getRequestedPath() + ". They are valid for " + path.asString());
}
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("IS_AUTHENTICATED_FULLY"));
authorities.add(new SimpleGrantedAuthority("ROLE_TEMP"));
return new JWTAuthenticationToken(authorities, rawToken, authentication.getRequestedPath());
}
示例3: addJWTToken
import com.auth0.jwt.interfaces.DecodedJWT; //导入方法依赖的package包/类
@Test
public void addJWTToken() throws Exception {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uriString);
String actualUri = service.addJWTToken(builder).build().toUriString();
String jwtToken = UriComponentsBuilder.fromUriString(actualUri).build().getQueryParams().getFirst(
JWTSecurityService.JWT_PARAM_NAME);
DecodedJWT verify = verifier.verify(jwtToken);
Claim claim = verify.getClaim(JWTSecurityService.CLAIM_PATH);
assertEquals(expectedClaimString, claim.asString());
}
示例4: getUserIdFromJwtTokenOrThrow
import com.auth0.jwt.interfaces.DecodedJWT; //导入方法依赖的package包/类
/**
* From a DecodedJWT, returns the user id or throws if there isn't a token, or is invalid.
* @param decodedJWT A DecodedJWT token.
* @return the user id.
* @throws NotAuthenticatedException if the DecodedJWT is null or the JWT doesn't contain the {@code userId}.
*/
public static String getUserIdFromJwtTokenOrThrow(DecodedJWT decodedJWT) throws NotAuthenticatedException {
if(decodedJWT == null) throw NotAuthenticatedException.noAutenticado();
Claim c = decodedJWT.getClaim("userId");
if(c != null) return c.asString(); else throw NotAuthenticatedException.noAutenticado();
}