当前位置: 首页>>代码示例>>Java>>正文


Java DecodedJWT.getClaim方法代码示例

本文整理汇总了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;
}
 
开发者ID:Petrakus,项目名称:java-jwt-auth,代码行数:20,代码来源:RESTService.java

示例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());
}
 
开发者ID:airsonic,项目名称:airsonic,代码行数:28,代码来源:JWTAuthenticationProvider.java

示例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());
}
 
开发者ID:airsonic,项目名称:airsonic,代码行数:11,代码来源:JWTSecurityServiceTest.java

示例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();
}
 
开发者ID:melchor629,项目名称:agendamlgr,代码行数:12,代码来源:TokensUtils.java


注:本文中的com.auth0.jwt.interfaces.DecodedJWT.getClaim方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。