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


Java JWTSigner类代码示例

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


JWTSigner类属于com.auth0.jwt包,在下文中一共展示了JWTSigner类的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: DcosAuthImpl

import com.auth0.jwt.JWTSigner; //导入依赖的package包/类
public DcosAuthImpl(final StringCredentials credentials) {
    this(
            credentials,
            new JWTSigner.Options(),
            ContentType.APPLICATION_JSON,
            HttpClientBuilder.create(),
            new HttpClientContext()
    );
}
 
开发者ID:jenkinsci,项目名称:marathon-plugin,代码行数:10,代码来源:DcosAuthImpl.java

示例12: 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

示例13: JwtTokenManager

import com.auth0.jwt.JWTSigner; //导入依赖的package包/类
public JwtTokenManager(final String jwtSecret, final int jwtExpirySeconds) {
    jwtSigner = new JWTSigner(jwtSecret);
    jwtVerifier = new JWTVerifier(jwtSecret);
    jwtOptions = new JWTSigner.Options().setExpirySeconds(jwtExpirySeconds);

    jsonMapper = new ObjectMapper();
    jsonMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
 
开发者ID:lewisd32,项目名称:authrite,代码行数:9,代码来源:JwtTokenManager.java

示例14: connectUser

import com.auth0.jwt.JWTSigner; //导入依赖的package包/类
protected Response connectUser(String username) {
        UserEntity user = userService.connect(username);

        // JWT signer
        final Map<String, Object> claims = new HashMap<>();

        claims.put(JWTHelper.Claims.ISSUER, environment.getProperty("jwt.issuer", JWTHelper.DefaultValues.DEFAULT_JWT_ISSUER));
        claims.put(JWTHelper.Claims.SUBJECT, user.getUsername());
//        claims.put(JWTHelper.Claims.PERMISSIONS, Collections.singleton(new SimpleGrantedAuthority(Role.USER.toString())));
        claims.put(JWTHelper.Claims.EMAIL, user.getEmail());
        claims.put(JWTHelper.Claims.FIRSTNAME, user.getFirstname());
        claims.put(JWTHelper.Claims.LASTNAME, user.getLastname());

        final JWTSigner.Options options = new JWTSigner.Options();
        options.setExpirySeconds(environment.getProperty("jwt.expire-after", Integer.class, DEFAULT_JWT_EXPIRE_AFTER));
        options.setIssuedAt(true);
        options.setJwtId(true);

        return Response.ok()
                .entity(user)
                .cookie(new NewCookie(
                        HttpHeaders.AUTHORIZATION,
                        "Bearer " + new JWTSigner(
                                environment.getProperty("jwt.secret")
                        ).sign(claims, options),
                        environment.getProperty("jwt.cookie-path", "/"),
                        environment.getProperty("jwt.cookie-domain"),
                        "",
                        environment.getProperty("jwt.expire-after", Integer.class, DEFAULT_JWT_EXPIRE_AFTER),
                        environment.getProperty("jwt.cookie-secure", Boolean.class, false),
                        true))
                .build();
    }
 
开发者ID:gravitee-io,项目名称:gravitee-management-rest-api,代码行数:34,代码来源:AbstractAuthenticationResource.java

示例15: create

import com.auth0.jwt.JWTSigner; //导入依赖的package包/类
/**
 * Creates a new {@link JwtTokenBuilder} instance
 * 
 * @param secret secret phrase
 * 
 * @return a new {@link JwtTokenBuilder} instance
 */
public static JwtTokenBuilder create(String secret) {
	JwtTokenBuilder builder = new JwtTokenBuilder();
	builder.signer = new JWTSigner(secret); 
	builder.options.setIssuedAt(true);
	
	return builder;
}
 
开发者ID:andreacomo,项目名称:tomcat-jwt-security,代码行数:15,代码来源:JwtTokenBuilder.java


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