From 4f0e73ba97517ff9bbe1ce5e490a03214b3996cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Tue, 30 Nov 2021 10:33:35 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BD=BF=E7=94=A8=20hutool-jwt=20?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E8=80=81=E6=97=A7=20jjwt=20=E4=BE=9D?= =?UTF-8?q?=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 - ruoyi-common/pom.xml | 6 ---- .../com/ruoyi/common/constant/Constants.java | 3 +- .../system/service/impl/TokenServiceImpl.java | 28 +++++++++---------- 4 files changed, 14 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index 4dd51662..ad4980fd 100644 --- a/pom.xml +++ b/pom.xml @@ -25,7 +25,6 @@ 4.1.2 2.2.11 2.3 - 0.9.1 3.4.3.4 3.9.1 5.7.16 diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 4a0e1a86..68db4ccb 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -70,12 +70,6 @@ snakeyaml - - - io.jsonwebtoken - jjwt - - com.sun.xml.bind diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java b/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java index 9c91a1a1..88a3c3bc 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java @@ -1,6 +1,5 @@ package com.ruoyi.common.constant; -import io.jsonwebtoken.Claims; /** * 通用常量信息 @@ -106,7 +105,7 @@ public class Constants { /** * 用户名称 */ - public static final String JWT_USERNAME = Claims.SUBJECT; + public static final String JWT_USERNAME = "sub"; /** * 用户头像 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TokenServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TokenServiceImpl.java index 15b9759b..eaf87865 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TokenServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TokenServiceImpl.java @@ -3,6 +3,10 @@ package com.ruoyi.system.service.impl; import cn.hutool.core.util.IdUtil; import cn.hutool.http.useragent.UserAgent; import cn.hutool.http.useragent.UserAgentUtil; +import cn.hutool.json.JSONObject; +import cn.hutool.jwt.JWTUtil; +import cn.hutool.jwt.signers.JWTSigner; +import cn.hutool.jwt.signers.JWTSignerUtil; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.domain.model.LoginUser; import com.ruoyi.common.core.service.TokenService; @@ -11,9 +15,6 @@ import com.ruoyi.common.utils.RedisUtils; import com.ruoyi.common.utils.ServletUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.ip.AddressUtils; -import io.jsonwebtoken.Claims; -import io.jsonwebtoken.Jwts; -import io.jsonwebtoken.SignatureAlgorithm; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -50,9 +51,9 @@ public class TokenServiceImpl implements TokenService { String token = getToken(request); if (StringUtils.isNotEmpty(token)) { try { - Claims claims = parseToken(token); + JSONObject claims = parseToken(token); // 解析对应的权限以及用户信息 - String uuid = (String) claims.get(Constants.LOGIN_USER_KEY); + String uuid = claims.getStr(Constants.LOGIN_USER_KEY); String userKey = getTokenKey(uuid); LoginUser user = RedisUtils.getCacheObject(userKey); return user; @@ -153,9 +154,8 @@ public class TokenServiceImpl implements TokenService { * @return 令牌 */ private String createToken(Map claims) { - String token = Jwts.builder() - .setClaims(claims) - .signWith(SignatureAlgorithm.HS512, tokenProperties.getSecret()).compact(); + JWTSigner signer = JWTSignerUtil.hs512(tokenProperties.getSecret().getBytes()); + String token = JWTUtil.createToken(claims, signer); return token; } @@ -165,11 +165,9 @@ public class TokenServiceImpl implements TokenService { * @param token 令牌 * @return 数据声明 */ - private Claims parseToken(String token) { - return Jwts.parser() - .setSigningKey(tokenProperties.getSecret()) - .parseClaimsJws(token) - .getBody(); + private JSONObject parseToken(String token) { + JWTSigner signer = JWTSignerUtil.hs512(tokenProperties.getSecret().getBytes()); + return JWTUtil.parseToken(token).setSigner(signer).getPayload().getClaimsJson(); } /** @@ -180,8 +178,8 @@ public class TokenServiceImpl implements TokenService { */ @Override public String getUsernameFromToken(String token) { - Claims claims = parseToken(token); - return claims.getSubject(); + JSONObject claims = parseToken(token); + return claims.getStr("sub"); } /**