|
@@ -0,0 +1,114 @@
|
|
|
+package com.zhentao.config.token;
|
|
|
+
|
|
|
+
|
|
|
+import com.zhentao.config.enums.ApiServiceExceptionEnum;
|
|
|
+import com.zhentao.config.exceptions.ApiException;
|
|
|
+import io.jsonwebtoken.Claims;
|
|
|
+import io.jsonwebtoken.JwtBuilder;
|
|
|
+import io.jsonwebtoken.Jwts;
|
|
|
+import io.jsonwebtoken.SignatureAlgorithm;
|
|
|
+
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
+import javax.xml.bind.DatatypeConverter;
|
|
|
+import java.security.Key;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 生成Token工具类
|
|
|
+ */
|
|
|
+public class TokenUtils {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 签名秘钥
|
|
|
+ */
|
|
|
+ public static final String SECRET = "cjyfutu1688";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成token
|
|
|
+ * @param id 一般传入userName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String createJwtToken(String id){
|
|
|
+ String issuer = "www.futureading.com";
|
|
|
+ String subject = "65532781@qq.com";
|
|
|
+ long ttlMillis = System.currentTimeMillis();
|
|
|
+ return createJwtToken(id, issuer, subject, ttlMillis);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成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();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // Sample method to validate and read the JWT
|
|
|
+ public static Claims parseJWT(String jwt) {
|
|
|
+ // This line will throw an exception if it is not a signed JWS (as expected)
|
|
|
+ Claims claims = Jwts.parser()
|
|
|
+ .setSigningKey(DatatypeConverter.parseBase64Binary(SECRET))
|
|
|
+ .parseClaimsJws(jwt).getBody();
|
|
|
+ return claims;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Long getUserId(String token){
|
|
|
+ Claims claims = null;
|
|
|
+ try {
|
|
|
+ claims = TokenUtils.parseJWT(token);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ApiException(ApiServiceExceptionEnum.Login_ERROR);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(null==claims) {
|
|
|
+ throw new ApiException(ApiServiceExceptionEnum.Login_ERROR);
|
|
|
+ }
|
|
|
+ String id = claims.getId();
|
|
|
+ Long userId=Long.valueOf(id);
|
|
|
+
|
|
|
+ return userId;
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ System.out.println(TokenUtils.createJwtToken("admin"));
|
|
|
+ }
|
|
|
+}
|