From 4969f95f4c9064c270d9790f3569df4fcd77d10b Mon Sep 17 00:00:00 2001 From: wang926454 <1107224733@qq.com> Date: Mon, 18 Mar 2019 12:42:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8E=B7=E5=8F=96=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E7=99=BB=E5=BD=95=E7=94=A8=E6=88=B7=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/wang/config/shiro/UserRealm.java | 2 +- src/main/java/com/wang/util/UserUtil.java | 38 +++++++++++++------ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/wang/config/shiro/UserRealm.java b/src/main/java/com/wang/config/shiro/UserRealm.java index fb27ce9..c54b1e1 100644 --- a/src/main/java/com/wang/config/shiro/UserRealm.java +++ b/src/main/java/com/wang/config/shiro/UserRealm.java @@ -100,7 +100,7 @@ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auth) t String currentTimeMillisRedis = JedisUtil.getObject(Constant.PREFIX_SHIRO_REFRESH_TOKEN + account).toString(); // 获取AccessToken时间戳,与RefreshToken的时间戳对比 if (JwtUtil.getClaim(token, Constant.CURRENT_TIME_MILLIS).equals(currentTimeMillisRedis)) { - return new SimpleAuthenticationInfo(userDto, token, token); + return new SimpleAuthenticationInfo(token, token, "userRealm"); } } throw new AuthenticationException("Token已过期(Token expired or incorrect.)"); diff --git a/src/main/java/com/wang/util/UserUtil.java b/src/main/java/com/wang/util/UserUtil.java index 901801f..93df735 100644 --- a/src/main/java/com/wang/util/UserUtil.java +++ b/src/main/java/com/wang/util/UserUtil.java @@ -1,10 +1,12 @@ package com.wang.util; +import com.wang.exception.CustomException; +import com.wang.mapper.UserMapper; import com.wang.model.UserDto; +import com.wang.model.common.Constant; import org.apache.shiro.SecurityUtils; -import org.apache.shiro.subject.PrincipalCollection; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.Set; /** * 获取当前登录用户工具类 @@ -14,6 +16,13 @@ @Component public class UserUtil { + private final UserMapper userMapper; + + @Autowired + public UserUtil(UserMapper userMapper) { + this.userMapper = userMapper; + } + /** * 获取当前登录用户 * @param @@ -22,7 +31,17 @@ public class UserUtil { * @date 2019/3/15 11:48 */ public UserDto getUser() { - return (UserDto) SecurityUtils.getSubject().getPrincipal(); + String token = SecurityUtils.getSubject().getPrincipal().toString(); + // 解密获得Account + String account = JwtUtil.getClaim(token, Constant.ACCOUNT); + UserDto userDto = new UserDto(); + userDto.setAccount(account); + userDto = userMapper.selectOne(userDto); + // 用户是否存在 + if (userDto == null) { + throw new CustomException("该帐号不存在(The account does not exist.)"); + } + return userDto; } /** @@ -44,14 +63,7 @@ public Integer getUserId() { * @date 2019/3/15 11:48 */ public String getToken() { - PrincipalCollection principalCollection = SecurityUtils.getSubject().getPrincipals(); - if (principalCollection != null) { - Set realmNames = principalCollection.getRealmNames(); - for (String realmName : realmNames) { - return realmName; - } - } - return null; + return SecurityUtils.getSubject().getPrincipal().toString(); } /** @@ -62,6 +74,8 @@ public String getToken() { * @date 2019/3/15 11:48 */ public String getAccount() { - return getUser().getAccount(); + String token = SecurityUtils.getSubject().getPrincipal().toString(); + // 解密获得Account + return JwtUtil.getClaim(token, Constant.ACCOUNT); } }