增加通过 token 获取当前登录用户基本信息TUser 方法

This commit is contained in:
x ronger 2019-11-24 11:43:32 +08:00
parent 733b57ca41
commit 0a0e51e18b

View File

@ -1,5 +1,6 @@
package com.rymcu.vertical.util; package com.rymcu.vertical.util;
import com.rymcu.vertical.dto.TUser;
import com.rymcu.vertical.entity.User; import com.rymcu.vertical.entity.User;
import com.rymcu.vertical.jwt.def.JwtConstants; import com.rymcu.vertical.jwt.def.JwtConstants;
import com.rymcu.vertical.jwt.model.TokenModel; 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.Claims;
import io.jsonwebtoken.Jwts; import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureException; import io.jsonwebtoken.SignatureException;
import org.apache.commons.lang.StringUtils;
public class UserUtils { public class UserUtils {
@ -30,7 +32,7 @@ public class UserUtils {
try { try {
claims = Jwts.parser().setSigningKey(JwtConstants.JWT_SECRET).parseClaimsJws(authHeader).getBody(); claims = Jwts.parser().setSigningKey(JwtConstants.JWT_SECRET).parseClaimsJws(authHeader).getBody();
} catch (final SignatureException e) { } catch (final SignatureException e) {
throw new MallApiException(ErrorCode.UNAUTHORIZED);
} }
Object account = claims.getId(); Object account = claims.getId();
if (!oConvertUtils.isEmpty(account)) { if (!oConvertUtils.isEmpty(account)) {
@ -43,4 +45,30 @@ public class UserUtils {
} }
return null; 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;
}
} }