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


Java JWTSigner.sign方法代码示例

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


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

示例1: prepareEmailConfirmationCode

import com.auth0.jwt.JWTSigner; //导入方法依赖的package包/类
public String prepareEmailConfirmationCode(User user, String linkId) {
    final long iat = System.currentTimeMillis() / 1000L; // issued at claim
    final long exp = iat + expireResetCode.getSeconds(); // expires claim
    final JWTSigner signer = new JWTSigner(jwtSecret);

    final HashMap<String, Object> claims = new HashMap<>();
    claims.put("vt", 6); //type=email_confirm_code
    claims.put("exp", exp);
    claims.put("email", user.getEmail());
    claims.put("username", user.getUsername());
    if(user.getPassword() != null)
        claims.put("password", user.getPassword());
    if(linkId != null)
        claims.put("linkId", linkId);

    return signer.sign(claims);
}
 
开发者ID:bekce,项目名称:oauthly,代码行数:18,代码来源:JwtUtils.java

示例2: prepareToken

import com.auth0.jwt.JWTSigner; //导入方法依赖的package包/类
/**
 * Prepares & issues a new token and returns it.
 * It puts the following information to the token:
 * <li>
 * <ul>version 'v'</ul>
 * <ul>expiry date 'exp'</ul>
 * <ul>combined hash 'h' value of client_id, client_secret, username and password.</ul><br>
 * This hash is important because if either one of the parameters change, the change in hash value will render the token invalid. Example: if password of user changes, the token will automatically get invalid. Note that this hash is not technically bulletproof, there exist very little (approx. 1 in 4*10^9) chance that it will compute the same hash value.
 * <ul>token type 't' -> 'a' (access) or 'r' (refresh)</ul>
 * </li>
 * It prepares both access and refresh tokens, signs them using JWT and packs in a Token object.
 * Note that this single user implementation does not put username to the claims map.
 *
 * @return generated token
 */
public Token prepareToken(String client_id, String client_secret, String grant_id, Collection<String> scopes) {
    int hash = Objects.hash(client_id, client_secret);
    final long iat = System.currentTimeMillis() / 1000L; // issued at claim
    final long exp = iat + expireAccessToken.getSeconds(); // expires claim. In this case the token expires in 60 seconds
    final JWTSigner signer = new JWTSigner(jwtSecret);

    final HashMap<String, Object> claims = new HashMap<>();
    claims.put("vt", 1); //version=1 & type=access_token
    claims.put("exp", exp);
    claims.put("h", hash);
    claims.put("grant", grant_id);
    final String token_a = signer.sign(claims);

    final HashMap<String, Object> claims_r = new HashMap<>();
    final long exp_r = iat + expireRefreshToken.getSeconds(); // refresh token expire time: 1 week
    claims_r.put("vt", 2); //version=1 & type=refresh_token
    claims_r.put("exp", exp_r);
    claims_r.put("h", hash);
    claims_r.put("grant", grant_id);
    final String token_r = signer.sign(claims_r);

    /* The last parameter (scope) is entirely optional. You can use it to implement scoping requirements. If you would like so, put it to claims map to verify it. */
    return new Token(token_a, token_r, "bearer", expireAccessToken.getSeconds(), scopes == null ? "" : String.join(" ", scopes));
}
 
开发者ID:bekce,项目名称:oauthly,代码行数:40,代码来源:JwtUtils.java

示例3: sign

import com.auth0.jwt.JWTSigner; //导入方法依赖的package包/类
/**
 * 加密,传入一个对象和有效期
 * @param object
 * @param maxAge
 * @param <T>
 * @return
 */
public static <T> String sign(T object, long maxAge) {
    try {
        final JWTSigner signer = new JWTSigner(SECRET);
        final Map<String, Object> claims = new HashMap<String, Object>();
        ObjectMapper mapper = new ObjectMapper();
        String jsonString = mapper.writeValueAsString(object);
        claims.put(PAYLOAD, jsonString);
        claims.put(EXP, System.currentTimeMillis() + maxAge);
        return signer.sign(claims);
    } catch(Exception e) {
        return null;
    }
}
 
开发者ID:MasonQAQ,项目名称:WeatherSystem,代码行数:21,代码来源:JavaWebTokenUtil.java

示例4: prepareResetCode

import com.auth0.jwt.JWTSigner; //导入方法依赖的package包/类
public String prepareResetCode(User user) {
    int hash = Objects.hash(user.getUsername(), user.getEmail(), user.getPassword(), user.getLastUpdateTime());
    final long iat = System.currentTimeMillis() / 1000L; // issued at claim
    final long exp = iat + expireResetCode.getSeconds(); // expires claim
    final JWTSigner signer = new JWTSigner(jwtSecret);

    final HashMap<String, Object> claims = new HashMap<>();
    claims.put("vt", 5); //type=reset_code
    claims.put("exp", exp);
    claims.put("hash", hash);
    claims.put("user", user.getId());

    return signer.sign(claims);
}
 
开发者ID:bekce,项目名称:oauthly,代码行数:15,代码来源:JwtUtils.java

示例5: prepareEmailChangeConfirmationCode

import com.auth0.jwt.JWTSigner; //导入方法依赖的package包/类
public String prepareEmailChangeConfirmationCode(User user, String newEmail) {
    int hash = Objects.hash(user.getUsername(), user.getEmail(), user.getPassword());
    final long iat = System.currentTimeMillis() / 1000L; // issued at claim
    final long exp = iat + expireResetCode.getSeconds(); // expires claim
    final JWTSigner signer = new JWTSigner(jwtSecret);

    final HashMap<String, Object> claims = new HashMap<>();
    claims.put("vt", 7); //type=email_change_confirm_code
    claims.put("exp", exp);
    claims.put("user", user.getId());
    claims.put("hash", hash);
    claims.put("new_email", newEmail);
    return signer.sign(claims);
}
 
开发者ID:bekce,项目名称:oauthly,代码行数:15,代码来源:JwtUtils.java

示例6: prepareCookieValue

import com.auth0.jwt.JWTSigner; //导入方法依赖的package包/类
private String prepareCookieValue(User user) {
    int hash = Objects.hash(user.getUsername(), user.getEmail(), user.getPassword());
    final long iat = System.currentTimeMillis() / 1000L; // issued at claim
    final long exp = iat + expireCookie.getSeconds(); // expires claim
    final JWTSigner signer = new JWTSigner(jwtSecret);

    final HashMap<String, Object> claims = new HashMap<>();
    claims.put("vt", 4); //type=cookie_ltat
    claims.put("exp", exp);
    claims.put("hash", hash);
    claims.put("user", user.getId());

    return signer.sign(claims);
}
 
开发者ID:bekce,项目名称:oauthly,代码行数:15,代码来源:JwtUtils.java

示例7: prepareAuthorizationCode

import com.auth0.jwt.JWTSigner; //导入方法依赖的package包/类
public String prepareAuthorizationCode(String client_id, String client_secret, String grant_id, String redirect_uri) {
    int hash = Objects.hash(client_id, client_secret);
    final long iat = System.currentTimeMillis() / 1000L; // issued at claim
    final long exp = iat + expireAuthorizationCode.getSeconds();
    final JWTSigner signer = new JWTSigner(jwtSecret);

    final HashMap<String, Object> claims = new HashMap<>();
    claims.put("vt", 3); //type=authorization_code
    claims.put("exp", exp);
    claims.put("h", hash);
    claims.put("g", grant_id);
    claims.put("r", redirect_uri);
    return signer.sign(claims);
}
 
开发者ID:bekce,项目名称:oauthly,代码行数:15,代码来源:JwtUtils.java

示例8: generateJwt

import com.auth0.jwt.JWTSigner; //导入方法依赖的package包/类
private String generateJwt(boolean shouldBeValid) {
    final String audience = "https://example.com/gw";
    final String issuer = "https://example.com/idp";
    final String secret = CommonTestUtil.JWT_SIGNING_SECRET;

    // issued at claim
    final long issuedAt;
    if (shouldBeValid) {
        // issued now - should not have expired
        issuedAt = System.currentTimeMillis() / 1000L;
    } else {
        // issued in the past - will have expired already
        issuedAt = (System.currentTimeMillis() / 1000L) - 120L;
    }

    // expires claim. In this case the token expires in 60 seconds
    final long expires = issuedAt + 60L;

    final JWTSigner signer = new JWTSigner(secret);

    @SuppressWarnings("unchecked")
    final Map<String, Object> claims = new HashMap() {{
        put("aud", audience);
        put("iss", issuer);
        put("iat", issuedAt);
        put("exp", expires);
        put("sub", CommonTestUtil.AUTHENTICATED_PRINICPAL);
    }};

    return signer.sign(claims);
}
 
开发者ID:outofcoffee,项目名称:apiman-plugins-session,代码行数:32,代码来源:LoginBackEndApi.java

示例9: sign

import com.auth0.jwt.JWTSigner; //导入方法依赖的package包/类
/**
 * get jwt String of object
 * @param object
 *            the POJO object
 * @param maxAge
 *            the milliseconds of life time
 * @return the jwt token
 */
public static <T> String sign(T object, long maxAge) {
	try {
		final JWTSigner signer = new JWTSigner(SECRET);
		final Map<String, Object> claims = new HashMap<String, Object>();
		ObjectMapper mapper = new ObjectMapper();
		String jsonString = mapper.writeValueAsString(object);
		claims.put(PAYLOAD, jsonString);
		claims.put(EXP, System.currentTimeMillis() + maxAge);
		return signer.sign(claims);
	} catch(Exception e) {
		return null;
	}
}
 
开发者ID:imchenglibin,项目名称:spring-web-tutorial,代码行数:22,代码来源:JWT.java

示例10: createJwt

import com.auth0.jwt.JWTSigner; //导入方法依赖的package包/类
public static String createJwt(String secret, String issuer, String subject) {
  final long iat = System.currentTimeMillis() / 1000l; // issued at claim
  final long exp = iat + 60L; // expires claim. In this case the token expires in 60 seconds

  final JWTSigner signer = new JWTSigner(secret);
  final HashMap<String, Object> claims = new HashMap<String, Object>();
  claims.put("iss", issuer);
  claims.put("exp", exp);
  claims.put("iat", iat);
  claims.put("sub", subject);

  return signer.sign(claims);
}
 
开发者ID:saturnism,项目名称:grpc-java-by-example,代码行数:14,代码来源:AuthClient.java

示例11: sign

import com.auth0.jwt.JWTSigner; //导入方法依赖的package包/类
/**
 * 对claims签名
 * @param claims    待签名数据
 * @return
 */
public String sign(HashMap<String, Object> claims){
    Map<String, Object> tmpClaims = (HashMap)claims.clone();

    JWTSigner signer = new JWTSigner(secret);
    long created = System.currentTimeMillis() / 1000L;
    long expired = created + 7 * 24 * 60 * 60L;
    tmpClaims.put("issuer", issuer);
    tmpClaims.put("expired", expired);

    return signer.sign(tmpClaims);
}
 
开发者ID:CecilJxSu,项目名称:OnlineCourse,代码行数:17,代码来源:JWT.java

示例12: before

import com.auth0.jwt.JWTSigner; //导入方法依赖的package包/类
@BeforeClass
public static void before() {
	Map<String, Object> claims = new HashMap<String, Object>();
	claims.put(JwtConstants.USER_ID, "foo");
	claims.put(JwtConstants.ROLES, Arrays.asList("role1", "role2"));
	
	JWTSigner signer = new JWTSigner(SECRET);
	token = signer.sign(claims);
}
 
开发者ID:andreacomo,项目名称:tomcat-jwt-security,代码行数:10,代码来源:JwtTokenVerifierTest.java

示例13: setDCOSCookie

import com.auth0.jwt.JWTSigner; //导入方法依赖的package包/类
public void setDCOSCookie(String DCOSsecret, String email) throws Exception {
    final JWTSigner signer = new JWTSigner(DCOSsecret);
    final HashMap<String, Object> claims = new HashMap();
    claims.put("uid", email);
    final String jwt = signer.sign(claims);
    Cookie cookie = new Cookie("dcos-acs-auth-cookie", jwt, false, "", "", 99999, false, false);
    List<Cookie> cookieList = new ArrayList<Cookie>();
    cookieList.add(cookie);
    commonspec.setCookies(cookieList);
}
 
开发者ID:Stratio,项目名称:bdt,代码行数:11,代码来源:GivenGSpec.java

示例14: createJWT

import com.auth0.jwt.JWTSigner; //导入方法依赖的package包/类
public String createJWT(String user_id, int expires) {
	JWTSigner signer = new JWTSigner(this.secret);
	HashMap<String, Object> claims = new HashMap<String, Object>();
	String token = signer.sign(claims, new JWTSigner.Options().setExpirySeconds(expires).setAlgorithm(Algorithm.HS256));
	token = user_id + ':' + token;
	return token;
}
 
开发者ID:playlyfe,项目名称:playlyfe-java-sdk,代码行数:8,代码来源:PlaylyfeGraphQL.java

示例15: createJWT

import com.auth0.jwt.JWTSigner; //导入方法依赖的package包/类
public static String createJWT(String client_id, String client_secret, String player_id, String[] scopes, int expires) {
	JWTSigner signer = new JWTSigner(client_secret);
	HashMap<String, Object> claims = new HashMap<String, Object>();
	claims.put("player_id", player_id);
	claims.put("scopes", scopes);
	String token = signer.sign(claims, new JWTSigner.Options().setExpirySeconds(expires).setAlgorithm(Algorithm.HS256));
	token = client_id + ':' + token;
	return token;
}
 
开发者ID:playlyfe,项目名称:playlyfe-java-sdk,代码行数:10,代码来源:Playlyfe.java


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