Skip to content

Commit

Permalink
修复获取当前登录用户工具类问题
Browse files Browse the repository at this point in the history
  • Loading branch information
dolyw committed Mar 18, 2019
1 parent e0dc8e1 commit 4969f95
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/wang/config/shiro/UserRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.)");
Expand Down
38 changes: 26 additions & 12 deletions src/main/java/com/wang/util/UserUtil.java
Original file line number Diff line number Diff line change
@@ -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;

/**
* 获取当前登录用户工具类
Expand All @@ -14,6 +16,13 @@
@Component
public class UserUtil {

private final UserMapper userMapper;

@Autowired
public UserUtil(UserMapper userMapper) {
this.userMapper = userMapper;
}

/**
* 获取当前登录用户
* @param
Expand All @@ -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;
}

/**
Expand All @@ -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<String> realmNames = principalCollection.getRealmNames();
for (String realmName : realmNames) {
return realmName;
}
}
return null;
return SecurityUtils.getSubject().getPrincipal().toString();
}

/**
Expand All @@ -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);
}
}

0 comments on commit 4969f95

Please sign in to comment.