diff --git a/src/main/java/com/rymcu/vertical/util/UserUtils.java b/src/main/java/com/rymcu/vertical/util/UserUtils.java index 6ee4914..b19fa0c 100644 --- a/src/main/java/com/rymcu/vertical/util/UserUtils.java +++ b/src/main/java/com/rymcu/vertical/util/UserUtils.java @@ -1,5 +1,6 @@ package com.rymcu.vertical.util; +import com.rymcu.vertical.dto.TUser; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.jwt.def.JwtConstants; import com.rymcu.vertical.jwt.model.TokenModel; @@ -10,6 +11,7 @@ import com.rymcu.vertical.web.api.exception.MallApiException; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureException; +import org.apache.commons.lang.StringUtils; public class UserUtils { @@ -30,7 +32,7 @@ public class UserUtils { try { claims = Jwts.parser().setSigningKey(JwtConstants.JWT_SECRET).parseClaimsJws(authHeader).getBody(); } catch (final SignatureException e) { - + throw new MallApiException(ErrorCode.UNAUTHORIZED); } Object account = claims.getId(); if (!oConvertUtils.isEmpty(account)) { @@ -43,4 +45,30 @@ public class UserUtils { } return null; } + + public static TUser getTUser(String token) { + if(StringUtils.isNotBlank(token)){ + // 验证token + Claims claims = null; + try { + claims = Jwts.parser().setSigningKey(JwtConstants.JWT_SECRET).parseClaimsJws(token).getBody(); + } catch (final SignatureException e) { + return null; + } + Object account = claims.getId(); + if (!oConvertUtils.isEmpty(account)) { + TokenModel model = tokenManager.getToken(token, account.toString()); + if (tokenManager.checkToken(model)) { + User user = userMapper.findByAccount(account.toString()); + if(user != null){ + TUser tUser = new TUser(); + BeanCopierUtil.copy(user,tUser); + tUser.setToken(token); + return tUser; + } + } + } + } + return null; + } }