Java Code Examples for io.jsonwebtoken.SignatureAlgorithm#HS256
The following examples show how to use
io.jsonwebtoken.SignatureAlgorithm#HS256 .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SecurityServiceImpl.java From Building-RESTful-Web-Services-with-Spring-5-Second-Edition with MIT License | 8 votes |
@Override public String createToken(String subject, long ttlMillis) { if (ttlMillis <= 0) { throw new RuntimeException("Expiry time must be greater than Zero :["+ttlMillis+"] "); } SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; // The JWT signature algorithm we will be using to sign the token long nowMillis = System.currentTimeMillis(); byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(secretKey); Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName()); JwtBuilder builder = Jwts.builder() .setSubject(subject) .signWith(signatureAlgorithm, signingKey); builder.setExpiration(new Date(nowMillis + ttlMillis)); return builder.compact(); }
Example 2
Source File: SecurityServiceImpl.java From Building-RESTful-Web-Services-with-Spring-5-Second-Edition with MIT License | 8 votes |
@Override public String createToken(String subject, long ttlMillis) { if (ttlMillis <= 0) { throw new RuntimeException("Expiry time must be greater than Zero :["+ttlMillis+"] "); } SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; // The JWT signature algorithm we will be using to sign the token long nowMillis = System.currentTimeMillis(); byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(secretKey); Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName()); JwtBuilder builder = Jwts.builder() .setSubject(subject) .signWith(signatureAlgorithm, signingKey); builder.setExpiration(new Date(nowMillis + ttlMillis)); return builder.compact(); }
Example 3
Source File: SecurityServiceImpl.java From Building-RESTful-Web-Services-with-Spring-5-Second-Edition with MIT License | 7 votes |
@Override public String createToken(String subject, long ttlMillis) { if (ttlMillis <= 0) { throw new RuntimeException("Expiry time must be greater than Zero :["+ttlMillis+"] "); } SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; // The JWT signature algorithm we will be using to sign the token long nowMillis = System.currentTimeMillis(); byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(secretKey); Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName()); JwtBuilder builder = Jwts.builder() .setSubject(subject) .signWith(signatureAlgorithm, signingKey); builder.setExpiration(new Date(nowMillis + ttlMillis)); return builder.compact(); }
Example 4
Source File: JjwtUtil.java From ProjectStudy with MIT License | 7 votes |
/** * 生成签名,获取Token * * @param username * @param base64Security * @return java.lang.String * @author Wang926454 * @date 2018/8/31 10:03 */ public static String createJWT(String username, String base64Security) { SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; // 生成JWT的时间 long nowMillis = System.currentTimeMillis(); Date now = new Date(nowMillis); // 添加构成JWT的参数 JwtBuilder builder = Jwts.builder().setHeaderParam("typ", "JWT") .setIssuedAt(now) .setSubject(username) .signWith(signatureAlgorithm, base64Security.getBytes()); // 设置过期时间 if (EXPIRE_TIME >= 0) { long expMillis = nowMillis + EXPIRE_TIME; Date exp = new Date(expMillis); builder.setExpiration(exp); } // 生成JWT return builder.compact(); }
Example 5
Source File: JwtTokenGenerator.java From hsweb-framework with Apache License 2.0 | 6 votes |
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(); }
Example 6
Source File: SecurityServiceImpl.java From Building-RESTful-Web-Services-with-Spring-5-Second-Edition with MIT License | 6 votes |
@Override public String createToken(String subject, long ttlMillis) { if (ttlMillis <= 0) { throw new RuntimeException("Expiry time must be greater than Zero :["+ttlMillis+"] "); } SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; // The JWT signature algorithm we will be using to sign the token long nowMillis = System.currentTimeMillis(); byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(secretKey); Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName()); JwtBuilder builder = Jwts.builder() .setSubject(subject) .signWith(signatureAlgorithm, signingKey); builder.setExpiration(new Date(nowMillis + ttlMillis)); return builder.compact(); }
Example 7
Source File: SecurityServiceImpl.java From Building-RESTful-Web-Services-with-Spring-5-Second-Edition with MIT License | 6 votes |
@Override public String createToken(String subject, long ttlMillis) { if (ttlMillis <= 0) { throw new RuntimeException("Expiry time must be greater than Zero :["+ttlMillis+"] "); } SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; // The JWT signature algorithm we will be using to sign the token long nowMillis = System.currentTimeMillis(); byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(secretKey); Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName()); JwtBuilder builder = Jwts.builder() .setSubject(subject) .signWith(signatureAlgorithm, signingKey); builder.setExpiration(new Date(nowMillis + ttlMillis)); return builder.compact(); }
Example 8
Source File: JwtService.java From faster-framework-project with Apache License 2.0 | 6 votes |
/** * 生成token * * @param audience 观众,理解为此token允许哪些人使用。 * 可以是一个数组字符串,包含了所有的允许对象,如"www.baidu.com","www.qq.com"。 * 也可以是一个单一字符串,如:"{userId}" * @param expSecond 过期时间(秒) * @param base64Security 秘钥 * @return String */ private String createToken(String audience, long expSecond, String base64Security) { SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; long nowMillis = System.currentTimeMillis(); Date now = new Date(nowMillis); //生成签名密钥 byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(base64Security); Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName()); //添加构成JWT的参数 JwtBuilder builder = Jwts.builder() .setAudience(audience) .setIssuedAt(now) .claim("env", env) .signWith(signatureAlgorithm, signingKey); //添加Token过期时间 if (expSecond > 0) { long expMillis = nowMillis + expSecond * 1000; Date exp = new Date(expMillis); builder = builder.setExpiration(exp).setNotBefore(now); } //生成Token return builder.compact(); }
Example 9
Source File: ConfigJwkResolver.java From juiser with Apache License 2.0 | 5 votes |
static SignatureAlgorithm getAlgorithm(byte[] hmacSigningKeyBytes) { Assert.isTrue(hmacSigningKeyBytes != null && hmacSigningKeyBytes.length > 0, "hmacSigningBytes cannot be null or empty."); if (hmacSigningKeyBytes.length >= 64) { return SignatureAlgorithm.HS512; } else if (hmacSigningKeyBytes.length >= 48) { return SignatureAlgorithm.HS384; } else { //<= 32 return SignatureAlgorithm.HS256; } }
Example 10
Source File: JwtUtils.java From mini-platform with MIT License | 5 votes |
/** * 该方法使用HS256算法和Secret生成signKey * * @return */ private static Key getKeyInstance() { //We will sign our JwtUtils with our ApiKey secret SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary("Trnj5MFuNPbAq2V0gbHsv9qMENRT12EI"); Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName()); return signingKey; }
Example 11
Source File: JwtUtils.java From leo-im-server with Apache License 2.0 | 5 votes |
/** * 创建jwt * * @param subject * @param secret * @param ttlMillis * @return */ public static String createJWT(String subject, String secret, long ttlMillis) { SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; long nowMillis = System.currentTimeMillis(); Date now = new Date(nowMillis); SecretKey key = generalKey(secret); JwtBuilder builder = Jwts.builder().setId("jwt").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(); }
Example 12
Source File: JwtUtils.java From leo-im-server with Apache License 2.0 | 5 votes |
/** * 创建jwt * * @param subject * @param secret * @param ttlMillis * @return */ public static String createJWT(String subject, String secret, long ttlMillis) { SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; long nowMillis = System.currentTimeMillis(); Date now = new Date(nowMillis); SecretKey key = generalKey(secret); JwtBuilder builder = Jwts.builder().setId("jwt").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(); }
Example 13
Source File: TokenUtils.java From XUpdateService with Apache License 2.0 | 5 votes |
/** * 生成Token * * @param id 编号 * @param issuer 该JWT的签发者,是否使用是可选的 * @param subject 该JWT所面向的用户,是否使用是可选的; * @param ttlMillis 签发时间 (有效时间,过期会报错) * @return token String */ public static String createJwtToken(String id, String issuer, String subject, long ttlMillis) { // 签名算法 ,将对token进行签名 SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; // 生成签发时间 long nowMillis = System.currentTimeMillis(); Date now = new Date(nowMillis); // 通过秘钥签名JWT 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) .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(); }
Example 14
Source File: JwtUtils.java From zhcc-server with Apache License 2.0 | 5 votes |
/** * 创建jwt * * @param subject * @return */ public String createJWT(String subject) { SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; 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(); }
Example 15
Source File: JwtUtils.java From withme3.0 with MIT License | 5 votes |
public static String createJWT(String authUser) { SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(CONSTANT.SECRET_KEY); Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName()); JwtBuilder builder = Jwts.builder() .setHeaderParam("typ", "jwt") .setHeaderParam("alg", "HS256") .setPayload(authUser) .signWith(signatureAlgorithm, signingKey); return builder.compact(); }
Example 16
Source File: JwtHelper.java From mogu_blog_v2 with Apache License 2.0 | 5 votes |
public String refreshToken(String token, String base64Security, long TTLMillis) { String refreshedToken; try { SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; long nowMillis = System.currentTimeMillis(); Date now = new Date(nowMillis); // 生成签名密钥 byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(base64Security); Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName()); final Claims claims = parseJWT(token, base64Security); claims.put("creatDate", new Date()); JwtBuilder builder = Jwts.builder().setHeaderParam("typ", "JWT") .setClaims(claims) .setSubject(getUsername(token, base64Security)) .setIssuer(getIssuer(token, base64Security)) .setAudience(getAudience(token, base64Security)) .signWith(signatureAlgorithm, signingKey); //添加Token过期时间 if (TTLMillis >= 0) { long expMillis = nowMillis + TTLMillis; Date exp = new Date(expMillis); builder.setExpiration(exp).setNotBefore(now); } refreshedToken = builder.compact(); } catch (Exception e) { refreshedToken = null; } return refreshedToken; }
Example 17
Source File: JwtHelper.java From mogu_blog_v2 with Apache License 2.0 | 5 votes |
/** * 构建jwt * * @param userName 账户名 * @param adminUid 账户id * @param roleName 账户拥有角色名 * @param audience 代表这个Jwt的接受对象 * @param issuer 代表这个Jwt的签发主题 * @param TTLMillis jwt有效时间 * @param base64Security 加密方式 * @return */ public String createJWT(String userName, String adminUid, String roleName, String audience, String issuer, long TTLMillis, String base64Security) { SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; long nowMillis = System.currentTimeMillis(); Date now = new Date(nowMillis); //生成签名密钥 byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(base64Security); Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName()); //添加构成JWT的参数 JwtBuilder builder = Jwts.builder().setHeaderParam("typ", "JWT") .claim("adminUid", adminUid) .claim("role", roleName) .claim("creatTime", now) .setSubject(userName) .setIssuer(issuer) .setAudience(audience) .signWith(signatureAlgorithm, signingKey); //添加Token过期时间 if (TTLMillis >= 0) { long expMillis = nowMillis + TTLMillis; Date exp = new Date(expMillis); builder.setExpiration(exp).setNotBefore(now); } //生成JWT return builder.compact(); }
Example 18
Source File: JwtUtil.java From mogu_blog_v2 with Apache License 2.0 | 5 votes |
public static String createSysUserJWT(Long shopId, Long sysUserId, String loginUserName, String loginPassWord, boolean isShop) { SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; long nowMillis = System.currentTimeMillis(); Date now = new Date(nowMillis); //生成签名密钥 byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(base64Secret); Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName()); //添加构成JWT的参数 JwtBuilder builder = Jwts.builder().setHeaderParam("typ", "JWT") .claim("shop_id", shopId) .claim("sys_user_id", sysUserId) .claim("is_shop", isShop) .claim("login_username", loginUserName) .claim("login_password", loginPassWord) // .claim("user_open_id", userOpenId) .signWith(signatureAlgorithm, signingKey); //添加Token过期时间 if (expiresSecond >= 0) { long expMillis = nowMillis + expiresSecond; Date exp = new Date(expMillis); builder.setExpiration(exp).setNotBefore(now); } //生成JWT String compact = builder.compact(); log.info("生成jwt===========" + compact); return compact; }
Example 19
Source File: JwtUtil.java From mogu_blog_v2 with Apache License 2.0 | 5 votes |
/** * 生成jwt token user * * @param userOpenId * @param userId * @param isUser * @param shopId * @return */ public static String createJWT(String userOpenId, Long userId, boolean isUser, Long shopId) { log.info("userOpenId" + userOpenId + "userId" + userId + "isUser" + isUser + "shopId" + shopId); SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; long nowMillis = System.currentTimeMillis(); Date now = new Date(nowMillis); //生成签名密钥 byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(base64Secret); Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName()); //添加构成JWT的参数 JwtBuilder builder = Jwts.builder().setHeaderParam("typ", "JWT") .claim("user_id", userId) .claim("shop_id", shopId) .claim("is_user", isUser) .claim("user_open_id", userOpenId) .signWith(signatureAlgorithm, signingKey); //添加Token过期时间 if (expiresSecond >= 0) { long expMillis = nowMillis + expiresSecond; Date exp = new Date(expMillis); builder.setExpiration(exp).setNotBefore(now); } //生成JWT String compact = builder.compact(); log.info("生成jwt===========" + compact); return compact; }
Example 20
Source File: JWTUtils.java From NetworkDisk_Storage with GNU General Public License v2.0 | 3 votes |
/** * createJWT: 创建jwt<br/> * * @author guooo * @param id * 唯一id,uuid即可 * @param subject * json形式字符串或字符串,增加用户非敏感信息存储,如user tid,与token解析后进行对比,防止乱用 * @param ttlMillis * 有效期 * @param stringKey * @return jwt token * @throws Exception * @since JDK 1.6 */ public static String createJWT(String id, String subject, long ttlMillis) throws Exception { SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; long nowMillis = System.currentTimeMillis(); Key key = generalKey(signatureAlgorithm); JwtBuilder builder = Jwts.builder().setId(id).setSubject(subject) .signWith(signatureAlgorithm, key); if (ttlMillis >= 0) { long expMillis = nowMillis + ttlMillis; Date exp = new Date(expMillis); builder.setExpiration(exp); } return builder.compact(); }