/ * Use JWT generation TOKEN, and log APP Token generation and parsing * / public class JwtTokenUtil { / ** * token secret key * / public static Final String SECRET = "1234567890" ; Private static Final String Key = "the user_code" ; / ** * the JWT generate the Token. * constituting the JWT: header, payload, Signature * @param after successful login user userNo no, no parameter is not passed empty * / @Validated public static String createToken (@NotBlank userNo String) throws Exception { iatDate DATE =new Date(); // expire time Calendar nowTime = Calendar.getInstance(); nowTime.add(Calendar.DATE, 10); Date expiresDate = nowTime.getTime(); // header Map Map<String, Object> map = new HashMap<>(); map.put("alg", "HS256"); map.put("typ", "JWT"); // build token // param backups {iss:Service, aud:APP} String token = JWT.create().withHeader(map) // header .withClaim("iss", "Service") // payload .withClaim("aud", "APP") .withClaim(key, userNo) .withIssuedAt(iatDate) // sign time .withExpiresAt(expiresDate) // expire time .sign(Algorithm.HMAC256(SECRET)); // signature return token; } /** * 解密Token * @param token * @return * @throws Exception */ private staticMap <String, the Claim> verifyToken (String token) { DecodedJWT JWT = null ; the try { JWTVerifier Verifier = JWT.require (Algorithm.HMAC256 (SECRET)) Build ();. JWT = verifier.verify (token); } the catch ( E exception) { // e.printStackTrace (); // token verification fails, the illegal authentication token thrown exception the throw new new BusinessException ( "token verification failure" ); } return jwt.getClaims (); } / ** * The Token obtain user_no *@param token * @return user_No */ public static String getAppUID(String token) { Map<String, Claim> claims = verifyToken(token); Claim user_id_claim = claims.get(key); if (null == user_id_claim || StringUtils.isBlank(user_id_claim.asString())) { // token 校验失败, 抛出Token验证非法异常 throw new BusinessException("token 异常"); } return user_id_claim.asString(); } }