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


Java Claims.getSubject方法代码示例

本文整理汇总了Java中io.jsonwebtoken.Claims.getSubject方法的典型用法代码示例。如果您正苦于以下问题:Java Claims.getSubject方法的具体用法?Java Claims.getSubject怎么用?Java Claims.getSubject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.jsonwebtoken.Claims的用法示例。


在下文中一共展示了Claims.getSubject方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getAuthentication

import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public Authentication getAuthentication(String token) {
    Claims claims = Jwts.parser()
        .setSigningKey(secretKey)
        .parseClaimsJws(token)
        .getBody();

    Collection<? extends GrantedAuthority> authorities = Arrays.stream(claims.get(AUTHORITIES_KEY)
        .toString()
        .split(","))
        .map(SimpleGrantedAuthority::new)
        .collect(Collectors.toList());

    User principal = new User(claims.getSubject(), "", authorities);

    return new UsernamePasswordAuthenticationToken(principal, token, authorities);
}
 
开发者ID:megadotnet,项目名称:SpringBootDemoApp,代码行数:17,代码来源:TokenProvider.java

示例2: parseRefreshToken

import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public SecurityUser parseRefreshToken(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("Refresh Token doesn't have any scopes");
  }
  if (!scopes.get(0).equals(Authority.REFRESH_TOKEN.name())) {
    throw new IllegalArgumentException("Invalid Refresh Token scope");
  }
  boolean isPublic = claims.get(IS_PUBLIC, Boolean.class);
  UserPrincipal principal = new UserPrincipal(isPublic ? UserPrincipal.Type.PUBLIC_ID : UserPrincipal.Type.USER_NAME,
      subject);
  SecurityUser securityUser = new SecurityUser(new UserId(UUID.fromString(claims.get(USER_ID, String.class))));
  securityUser.setUserPrincipal(principal);
  return securityUser;
}
 
开发者ID:osswangxining,项目名称:iotplatform,代码行数:19,代码来源:JwtTokenFactory.java

示例3: getAuthentication

import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public Authentication getAuthentication(String token) {
    Claims claims = Jwts.parser()
        .setSigningKey(secretKey)
        .parseClaimsJws(token)
        .getBody();

    Collection<? extends GrantedAuthority> authorities =
        Arrays.asList(claims.get(AUTHORITIES_KEY).toString().split(",")).stream()
            .map(SimpleGrantedAuthority::new)
            .collect(Collectors.toList());

    User principal = new User(claims.getSubject(), "",
        authorities);

    return new UsernamePasswordAuthenticationToken(principal, "", authorities);
}
 
开发者ID:klask-io,项目名称:klask-io,代码行数:17,代码来源:TokenProvider.java

示例4: getUserFromToken

import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public JWTUserDetails getUserFromToken(String token){
    JWTUserDetails user;
    try {
        final Claims claims=getClaimsFromToken(token);
       String userId = getUserIdFromToken(token);
        String username=claims.getSubject();
        List<GrantedAuthority> authorities =AuthorityUtils.commaSeparatedStringToAuthorityList((String) claims.get(CLAIM_KEY_AUTHORITIES));
       /* boolean account_enabled = (Boolean) claims.get(CLAIM_KEY_ACCOUNT_ENABLED);
        boolean account_non_locked = (Boolean) claims.get(CLAIM_KEY_ACCOUNT_NON_LOCKED);
        boolean account_non_expired = (Boolean) claims.get(CLAIM_KEY_ACCOUNT_NON_EXPIRED);*/

        user = new JWTUserDetails(userId, username, "password",  authorities);
    }catch (Exception e){
        logger.error("getUserFromToken error");
        user=null;
    }
    return user;
}
 
开发者ID:BENULL,项目名称:LushX,代码行数:19,代码来源:JWTUtils.java

示例5: 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;
}
 
开发者ID:osswangxining,项目名称:iotplatform,代码行数:31,代码来源:JwtTokenFactory.java

示例6: getUsernameFromToken

import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
/**
 * Obtém o username (email) contido no token JWT.
 * 
 * @param token
 * @return String
 */
public String getUsernameFromToken(String token) {
	String username;
	try {
		Claims claims = getClaimsFromToken(token);
		username = claims.getSubject();
	} catch (Exception e) {
		username = null;
	}
	return username;
}
 
开发者ID:m4rciosouza,项目名称:ponto-inteligente-api,代码行数:17,代码来源:JwtTokenUtil.java

示例7: getUsernameFromToken

import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public String getUsernameFromToken(String token) {
    String username;
    try {
        final Claims claims = getClaimsFromToken(token);
        username = claims.getSubject();
    } catch (Exception e) {
        username = null;
    }
    return username;
}
 
开发者ID:rodrigues882013,项目名称:springboot-rest-api-skeleton,代码行数:11,代码来源:JwtTokenUtil.java

示例8: getUsernameFromToken

import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
/**
 * 根据token得到用户名
 *
 * @param token
 * @return
 */
public String getUsernameFromToken ( String token ) {
	String username;
	try {
		final Claims claims = getClaimsFromToken( token );
		username = claims.getSubject();
	} catch ( Exception e ) {
		username = null;
	}
	return username;
}
 
开发者ID:yujunhao8831,项目名称:spring-boot-start-current,代码行数:17,代码来源:JwtTokenUtil.java

示例9: getAuthentication

import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
static Authentication getAuthentication(HttpServletRequest request) {
    // 从Header中拿到token
    String token = request.getHeader(HEADER_STRING);

    if (token != null) {
        try {
            // 解析 Token
            Claims claims = Jwts.parser()
                    // 验签
                    .setSigningKey(SECRET)
                    // 去掉 Bearer
                    .parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
                    .getBody();

            // 拿用户名
            String user = claims.getSubject();

            // 得到 权限(角色)
            List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList((String) claims.get("authorities"));

            // 返回验证令牌
            return user != null ?
                    new UsernamePasswordAuthenticationToken(user, null, authorities) :
                    null;
        } catch (Exception e) {
            return null;
        }
    }
    return null;
}
 
开发者ID:myliang,项目名称:fish-admin,代码行数:31,代码来源:TokenAuthenticationService.java

示例10: getUsernameFromToken

import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public String getUsernameFromToken(String token) {
    String username;
    try {
        final Claims clamis=getClaimsFromToken(token);
        username=clamis.getSubject();
    }catch (Exception e){
        username=null;
    }
    return username;
}
 
开发者ID:BENULL,项目名称:LushX,代码行数:11,代码来源:JWTUtils.java

示例11: isAdmin

import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
/**
 * Checks if use is an admin
 * 
 * @param token the JWT given
 * @return true if admin exists, false otherwise
 */
public static boolean isAdmin(String token) {
	Claims claims = null;
	UserDaoImpl ud = new UserDaoImpl();
	User user = null;
	String cUsername = null;
	boolean verified = false;
	
	if(token == null) {
		return false;
	}
	
	try {
		claims = Jwts.parser()
				.setSigningKey(getSecret())
				.parseClaimsJws(token).getBody();
		
		cUsername = claims.getSubject();
		user = ud.getUserByUsername(cUsername);
		
		if(cUsername == null || user == null || user.getRoleFlag() != 2) {
			return false;
		}
		
		verified = true;
		
	}catch(SignatureException se) {
		se.printStackTrace();
	}
	
	return verified;
}
 
开发者ID:grantleymorrison,项目名称:FlashBoard,代码行数:38,代码来源:TokenService.java

示例12: getAuthentication

import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
public Authentication getAuthentication(String token) {
        Claims claims = Jwts.parser()
            .setSigningKey(secretKey)
            .parseClaimsJws(token)
            .getBody();

        Collection<? extends GrantedAuthority> authorities =
            Arrays.stream(claims.get(AUTHORITIES_KEY).toString().split(","))
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());

        User principal = new User(claims.getSubject(), "", authorities);

        return new UsernamePasswordAuthenticationToken(principal, "", authorities);

//        Claims claims = Jwts.parser()
//                .setSigningKey(secretKey)
//                .parseClaimsJws(token)
//                .getBody();
//
//        String principal = claims.getSubject();
//
//        // CHANGES START HERE
//        String roles = claims.get(AUTHORITIES_KEY, Map.class).get("roles").toString();
//        if(roles != null && roles.length() > 0) {
//            roles = roles.substring(1, roles.length() - 1);
//        }
//
//        Collection<? extends GrantedAuthority> authorities =
//                Arrays.asList(roles.split(",")).stream()
//                        .map(authority -> new SimpleGrantedAuthority("ROLE_" + authority.toUpperCase()))
//                        .collect(Collectors.toList());
//
//        return new UsernamePasswordAuthenticationToken(principal, token, authorities);
    }
 
开发者ID:AppertaFoundation,项目名称:Code4Health-Platform,代码行数:36,代码来源:TokenProvider.java

示例13: getUsernameFromToken

import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
/**
 * 从token中获取username信息
 * @param token token
 * @return
 */
public String getUsernameFromToken(String token) {
    String username;
    try {
        final Claims claims = this.getClaimsFromToken(token);
        username = claims.getSubject();
    } catch (Exception e) {
        username = null;
    }
    return username;
}
 
开发者ID:YupaiTS,项目名称:docs-manage,代码行数:16,代码来源:JwtHelper.java

示例14: decodeCookie

import io.jsonwebtoken.Claims; //导入方法依赖的package包/类
@Override
protected String[] decodeCookie(String cookieValue) throws InvalidCookieException {
  try {
    Claims claims = Jwts.parser()
        .setSigningKey(getKey())
        .parseClaimsJws(cookieValue)
        .getBody();

    return new String[] { claims.getId(), claims.getSubject() };
  } catch (JwtException e) {
    LOGGER.warn(e.getMessage());
    throw new InvalidCookieException(e.getMessage());
  }
}
 
开发者ID:springuni,项目名称:springuni-particles,代码行数:15,代码来源:PersistentJwtTokenBasedRememberMeServices.java


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