From 07301e050cbf0a8fc1d34a2f4cedbab1db739f63 Mon Sep 17 00:00:00 2001 From: xioayue Date: Sun, 26 Jun 2022 09:52:49 +0800 Subject: [PATCH] =?UTF-8?q?JWT=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/chjyj/szwh/utils/JwtUtils.java | 212 ++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 src/main/java/cn/chjyj/szwh/utils/JwtUtils.java diff --git a/src/main/java/cn/chjyj/szwh/utils/JwtUtils.java b/src/main/java/cn/chjyj/szwh/utils/JwtUtils.java new file mode 100644 index 0000000..c20233a --- /dev/null +++ b/src/main/java/cn/chjyj/szwh/utils/JwtUtils.java @@ -0,0 +1,212 @@ +package cn.chjyj.szwh.utils; + +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.CompressionCodecs; +import io.jsonwebtoken.JwtBuilder; +import io.jsonwebtoken.Jwts; +import io.jsonwebtoken.SignatureAlgorithm; + +import java.util.Date; + +/** + * jwt工具类 + */ +public class JwtUtils { + /** + * 实例 + */ + private static JwtUtils instance; + + /** + * 发行者 + */ + private String subObject = "owner"; + + /** + * 过期时间,默认7天 + */ + private long expired = 1000 * 60 * 60 * 24 * 7; + + /** + * jwt构造 + */ + private static JwtBuilder jwtBuilder; + + /** + * 密钥 + */ + private String secret = "secret";// 密钥 + + /** + * 获取实例 + * @return + */ + public static JwtUtils getInstance(){ + if (instance == null){ + instance = new JwtUtils(); + } + jwtBuilder = Jwts.builder(); + return instance; + } + + /** + * 荷载信息(通常是一个User信息,还包括一些其他的元数据) + * @param key + * @param val + * @return + */ + public JwtUtils setClaim(String key,Object val){ + jwtBuilder.claim(key,val); + return this; + } + + /** + * 生成 jwt token + * @return + */ + public String generateToken(){ + String token = jwtBuilder + .setSubject(subObject) // 发行者 + //.claim("id","121") // 参数 + .setIssuedAt(new Date()) // 发行时间 + .setExpiration(new Date(System.currentTimeMillis() + expired)) + .signWith(SignatureAlgorithm.HS256,secret) // 签名类型 与 密钥 + .compressWith(CompressionCodecs.DEFLATE)// 对载荷进行压缩 + .compact(); // 压缩一下 + return token; + } + + /** + * 解析 token + * @param token + * @return + */ + public Claims check(String token){ + try{ + final Claims claims = Jwts.parser() + .setSigningKey(secret) + .parseClaimsJws(token) + .getBody(); + return claims; + }catch (Exception e){} + return null; + } + + public String getSubObject() { + return subObject; + } + + /** + * 设置发行者 + * @param subObject + * @return + */ + public JwtUtils setSubObject(String subObject) { + this.subObject = subObject; + return this; + } + + public long getExpired() { + return expired; + } + + /** + * 设置过期时间 + * @param expired + * @return + */ + public JwtUtils setExpired(long expired) { + this.expired = expired; + return this; + } + + public String getSecret() { + return secret; + } + + /** + * 设置密钥 + * @param secret + * @return + */ + public JwtUtils setSecret(String secret) { + this.secret = secret; + return this; + } +} + + +// import com.auth0.jwt.JWT; +// import com.auth0.jwt.JWTVerifier; +// import com.auth0.jwt.algorithms.Algorithm; +// import com.auth0.jwt.exceptions.JWTDecodeException; +// import com.auth0.jwt.interfaces.Claim; +// import com.auth0.jwt.interfaces.DecodedJWT; +// +// import java.io.Serializable; +// import java.util.Calendar; +// import java.util.Date; +// + +//public class JwtUtils { +// +// /** +// 签发对象:这个用户的id +// 签发时间:现在 +// 有效时间:30分钟 +// 载荷内容:暂时设计为:这个人的名字,这个人的昵称 +// 加密密钥:这个人的id加上一串字符串 +// */ +// public static String createToken(String userId,String realName, String userName) { +// +// Calendar nowTime = Calendar.getInstance(); +// nowTime.add(Calendar.MINUTE,30); +// Date expiresDate = nowTime.getTime(); +// +// return JWT.create().withAudience(userId) //签发对象 +// .withIssuedAt(new Date()) //发行时间 +// .withExpiresAt(expiresDate) //有效时间 +// .withClaim("userName", userName) //载荷,随便写几个都可以 +// .withClaim("realName", realName) +// .sign(Algorithm.HMAC256(userId+"HelloLehr")); //加密 +// } +// +// /** +// * 检验合法性,其中secret参数就应该传入的是用户的id +// * @param token +// * @throws TokenUnavailable +// */ +// public static void verifyToken(String token, String secret) throws TokenUnavailable { +// DecodedJWT jwt = null; +// try { +// JWTVerifier verifier = JWT.require(Algorithm.HMAC256(secret+"HelloLehr")).build(); +// jwt = verifier.verify(token); +// } catch (Exception e) { +// //效验失败 +// //这里抛出的异常是我自定义的一个异常,你也可以写成别的 +// throw new TokenUnavailable(); +// } +// } +// +// /** +// * 获取签发对象 +// */ +// public static String getAudience(String token) throws TokenUnavailable { +// String audience = null; +// try { +// audience = JWT.decode(token).getAudience().get(0); +// } catch (JWTDecodeException j) { +// //这里是token解析失败 +// throw new TokenUnavailable(); +// } +// return audience; +// } +// +// +// /** +// * 通过载荷名字获取载荷的值 +// */ +// public static Claim getClaimByName(String token, String name){ +// return JWT.decode(token).getClaim(name); +// } +//}