本文整理汇总了Java中io.jsonwebtoken.JwtBuilder.compact方法的典型用法代码示例。如果您正苦于以下问题:Java JwtBuilder.compact方法的具体用法?Java JwtBuilder.compact怎么用?Java JwtBuilder.compact使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.jsonwebtoken.JwtBuilder
的用法示例。
在下文中一共展示了JwtBuilder.compact方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: Gerate
import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
public static String Gerate(String issuer, int idSubject, int hours) {
//The JWT signature algorithm we will be using to sign the token
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
//Hours to milliseconds
long ttlMillis = hours * 3600000;
String subject = String.valueOf(idSubject);
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
//We will sign our JWT with our ApiKey secret
byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(Parameters.TOKENKEY);
Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());
//Let's set the JWT Claims
JwtBuilder builder = Jwts.builder().setIssuedAt(now)
.setSubject(subject)
.setIssuer(issuer)
.signWith(signatureAlgorithm, signingKey);
//if it has been specified, let's add the expiration
if (ttlMillis >= 0) {
long expMillis = nowMillis + ttlMillis;
Date exp = new Date(expMillis);
builder.setExpiration(exp);
}
//Builds the JWT and serializes it to a compact, URL-safe string
return builder.compact();
}
示例2: createJWT
import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
private String createJWT(String id, String issuer, String subject, Date expiration){
//The JWT signature algorithm we will be using to sign the token
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
Date now = new Date(System.currentTimeMillis());
//We will sign our JWT with our ApiKey secret
byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(SECRET);
Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());
//Let's set the JWT Claims
JwtBuilder builder = Jwts.builder()
.setId(id)
.setIssuedAt(now)
.setSubject(subject)
.setIssuer(issuer)
.setExpiration(expiration)
.signWith(signatureAlgorithm, signingKey);
//Builds the JWT and serializes it to a compact, URL-safe string
return builder.compact();
}
示例3: createJWT
import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
/**
* 创建jwt
* @param id
* @param subject
* @param ttlMillis
* @return
* @throws Exception
*/
public String createJWT(String id, String subject, long ttlMillis) throws Exception {
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS512;
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
SecretKey key = generalKey();
JwtBuilder builder = Jwts.builder()
.setId(id)
.setIssuedAt(now)
.setSubject(subject)
.signWith(signatureAlgorithm, key);
if (ttlMillis >= 0) {
long expMillis = nowMillis + ttlMillis;
Date exp = new Date(expMillis);
builder.setExpiration(exp);
}
return builder.compact();
}
示例4: before
import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
@Before
public void before(){
String pk = org.apache.commons.codec.binary.Base64.encodeBase64String(keyPair.getPublic().getEncoded());
stubFor(get("/oauth2/publickey").willReturn(aResponse().withStatus(200).withBody(pk)));
JwtBuilder builder = jwtBuilder(System.currentTimeMillis()+3600*1000L)
.signWith(SignatureAlgorithm.RS256,keyPair.getPrivate());
jwtToken = builder.compact();
SSOConfig config = new SSOConfig().autoConfigureUrls(baseUrl);
config.setClientId("test");
config.setClientSecret("test_secret");
config.setResourceName("resourceName");
config.setRedirectUri("http://www.example.com");
client = new SSOClient(config);
basicHeader = SSOUtils.encodeBasicAuthorizationHeader(config.getClientId(),config.getClientSecret());
}
示例5: createJWT
import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
public String createJWT(String id, String subject, long ttlMillis) {
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
SecretKey key = jwtConfig.generalKey();
JwtBuilder builder = Jwts.builder()
.setId(id)
.setIssuedAt(now)
.setSubject(subject)
.signWith(signatureAlgorithm, key);
if (ttlMillis >= 0) {
long expMillis = nowMillis + ttlMillis;
Date exp = new Date(expMillis);
builder.setExpiration(exp);
}
return builder.compact();
}
示例6: login
import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
@Path("login")
@POST
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public String login(@FormParam(value = "id") String id) throws IOException {
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(SECRECT);
Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());
JwtBuilder builder = Jwts.builder()
.setIssuedAt(now)
.setIssuer(id)
.signWith(signatureAlgorithm, signingKey);
long expMillis = nowMillis + EXPIRE ;
Date exp = new Date(expMillis);
builder.setExpiration(exp);
return builder.compact();
}
示例7: issueJwt
import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public String issueJwt(String subject, Map<String, Object> claims) {
Assert.hasText(subject, "Subject can't be null or empty!!");
Instant now = Instant.now();
JwtBuilder jwtBuilder = Jwts.builder()
.setHeaderParam(TYPE, JWT_TYPE)
.setClaims(claims)
.setSubject(subject)
.setIssuer(this.jwtConfig.issuer())
.setIssuedAt(Date.from(now))
.setExpiration(Date.from(now.plus(this.jwtConfig.expirationTime(), MINUTES)))
.setId(UUID.randomUUID().toString());
this.signWith(jwtBuilder);
return jwtBuilder.compact();
}
示例8: createUserJWT
import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
/**
* Creates the jwt for the given dawg credentials
* @param dawgCreds The credentials to encode
* @return
*/
public String createUserJWT(DawgCreds dawgCreds) {
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
Key signingKey = new SecretKeySpec(jwtSecret.getBytes(), SIGNATURE_ALG.getJcaName());
Map<String, Object> claims = new HashMap<String, Object>();
claims.put(DawgJwt.JWT_FIELD_CREDS, dawgCreds);
JwtBuilder builder = Jwts.builder()
.setClaims(claims)
.setId(UUID.randomUUID().toString())
.setIssuedAt(now)
.setSubject(JWT_SUBJECT)
.setIssuer(this.jwtIssuer)
.signWith(SIGNATURE_ALG, signingKey);
if (this.ttlMillis >= 0) {
long expMillis = nowMillis + this.ttlMillis;
Date exp = new Date(expMillis);
builder.setExpiration(exp);
}
return builder.compact();
}
示例9: createJWT
import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
private JwtToken createJWT(String id,
String issuer,
String subject,
String privileges,
long ttlMillis) {
// The JWT signature algorithm we will be using to sign the token
final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.RS256;
final long nowMillis = System.currentTimeMillis();
final Date now = new Date(nowMillis);
// We will sign our JWT with our ApiKey secret
final Key signingKey = EncryptionUtil.getPrivateKey(
env.getProperty("service.jwt.secret"));
final Map<String, Object> claims = new HashMap<>();
claims.put("privileges", privileges);
// Let's set the JWT Claims
final JwtBuilder builder = Jwts.builder()
.setClaims(claims)
.setId(id)
.setIssuer(issuer)
.setIssuedAt(now)
.setSubject(subject)
.signWith(signatureAlgorithm, signingKey);
// If it has been specified, let's add the expiration
if (ttlMillis >= 0) {
long expMillis = nowMillis + ttlMillis;
Date exp = new Date(expMillis);
builder.setExpiration(exp);
}
// Builds the JWT and serializes it to a compact, URL-safe string
return new JwtToken(builder.compact());
}
示例10: generateToken
import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
public static String generateToken(String signingKey, String subject) {
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
JwtBuilder builder = Jwts.builder()
.setSubject(subject)
.setIssuedAt(now)
.signWith(SignatureAlgorithm.HS256, signingKey);
String token = builder.compact();
RedisUtil.INSTANCE.sadd(REDIS_SET_ACTIVE_SUBJECTS, subject);
return token;
}
开发者ID:hellokoding,项目名称:single-sign-on-out-auth-jwt-cookie-redis-springboot-freemarker,代码行数:16,代码来源:JwtUtil.java
示例11: buildJWT
import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
private static String buildJWT(Authentication authentication, String id, String issuer, Long timeToLiveMs,
SignatureAlgorithm algorithm, Key privateKey, byte[] signingKey, AuthPart... includeParts) {
if (authentication == null) {
throw new IllegalArgumentException("Null Authentication");
}
JwtBuilder builder = createJWT(id, authentication.getName(), issuer,
(timeToLiveMs != null) ? timeToLiveMs.longValue() : -1);
// sign
if (privateKey != null || signingKey != null) {
if (algorithm == null) {
throw new IllegalArgumentException("Null signature algorithm");
}
if (privateKey != null) {
builder.signWith(algorithm, privateKey);
} else {
builder.signWith(algorithm, signingKey);
}
}
// auth parts
if (includeParts != null) {
for (AuthPart part : includeParts) {
processAuthPart(builder, authentication, part);
}
}
return builder.compact();
}
示例12: createJwtToken
import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
private String createJwtToken(String subject, LocalDateTime tokenExpirationDateTime, boolean isRefreshToken) {
JwtBuilder jwtBuilder = Jwts.builder()
.setSubject(subject)
.signWith(SignatureAlgorithm.HS512, privateKey)
.setExpiration(toDate(tokenExpirationDateTime));
if (isRefreshToken) {
jwtBuilder.claim(REFRESH_TOKEN_CLAIM_KEY, true);
}
return jwtBuilder.compact();
}
示例13: testSuccess
import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
@Test
public void testSuccess(@Mocked CloseableHttpClient httpClient,
@Mocked CloseableHttpResponse response, @Mocked JwtBuilder jwtBuilder,
@Mocked BasicResponseHandler responseHandler) throws IOException {
new Expectations() {{
//setup enough expectations to allow us to return the result that would have
//happened via http get.
HttpClientBuilder.create(); result = builder;
builder.build(); result = httpClient;
httpClient.execute((HttpGet)any); result = response;
responseHandler.handleResponse(response); result = "{\"credentials\":{\"sharedSecret\":\"WIBBLE\"}}";
//setup enough expectations to allow jwt generation to end up with a known result.
Jwts.builder(); result = jwtBuilder;
jwtBuilder.setHeaderParam((String)any,any); result = jwtBuilder;
jwtBuilder.setClaims((Claims)any); result = jwtBuilder;
jwtBuilder.signWith((SignatureAlgorithm)any, key); result = jwtBuilder;
jwtBuilder.compact(); result = "<<BUILTJWT>>";
}};
String secret = pc.getSecretForId("fish");
Assert.assertEquals(secret,"WIBBLE");
new Verifications() {{
HttpGet httpGet;
httpClient.execute(httpGet = withCapture());
//check if jwt header was set on request.
Assert.assertEquals("<<BUILTJWT>>", httpGet.getFirstHeader("gameon-jwt").getValue());
Assert.assertEquals("playerURL/fish", httpGet.getURI().toString());
}};
}
示例14: encrypt
import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
@Override
public String encrypt(Object principal) {
final JwtBuilder signedBuilder = builderFactory.create().claim(PRINCIPAL, principal)
.signWith(algorithm, keyPair.getPrivate());
if (expiryDuration >= 0) {
return signedBuilder.setExpiration(clock.nowPlus(expiryDuration, expiryUnit)).compact();
}
return signedBuilder.compact();
}
示例15: createToken
import io.jsonwebtoken.JwtBuilder; //导入方法依赖的package包/类
@Override
public String createToken(final String authorizationId, final Authorities authorities) {
JwtBuilder builder = Jwts.builder()
.signWith(algorithm, key)
.setIssuer("Hono")
.setSubject(Objects.requireNonNull(authorizationId))
.setExpiration(Date.from(Instant.now().plus(tokenLifetime)));
if (authorities != null) {
authorities.asMap().forEach((key, value) -> {
builder.claim(key, value);
});
}
return builder.compact();
}