Skip to content

Commit

Permalink
添加获取当前登录用户工具类
Browse files Browse the repository at this point in the history
  • Loading branch information
dolyw committed Mar 15, 2019
1 parent 5637435 commit e0dc8e1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 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(token, token, "userRealm");
return new SimpleAuthenticationInfo(userDto, token, token);
}
}
throw new AuthenticationException("Token已过期(Token expired or incorrect.)");
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/com/wang/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.wang.service.IUserService;
import com.wang.util.AesCipherUtil;
import com.wang.util.JwtUtil;
import com.wang.util.UserUtil;
import com.wang.util.common.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
Expand Down Expand Up @@ -46,10 +47,13 @@ public class UserController {
@Value("${refreshTokenExpireTime}")
private String refreshTokenExpireTime;

private final UserUtil userUtil;

private final IUserService userService;

@Autowired
public UserController(IUserService userService) {
public UserController(UserUtil userUtil, IUserService userService) {
this.userUtil = userUtil;
this.userService = userService;
}

Expand Down Expand Up @@ -178,6 +182,27 @@ public ResponseBean requireAuth() {
return new ResponseBean(HttpStatus.OK.value(), "您已经登录了(You are already logged in)", null);
}

/**
* 获取当前登录用户信息
* @param
* @return com.wang.model.common.ResponseBean
* @author Wang926454
* @date 2019/3/15 11:51
*/
@GetMapping("/info")
@RequiresAuthentication
public ResponseBean info() {
// 获取当前登录用户
UserDto userDto = userUtil.getUser();
// 获取当前登录用户Id
Integer id = userUtil.getUserId();
// 获取当前登录用户Token
String Token = userUtil.getToken();
// 获取当前登录用户Account
String account = userUtil.getAccount();
return new ResponseBean(HttpStatus.OK.value(), "您已经登录了(You are already logged in)", userDto);
}

/**
* 获取指定用户
* @param id
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/com/wang/util/UserUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.wang.util;

import com.wang.model.UserDto;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;
import java.util.Set;

/**
* 获取当前登录用户工具类
* @author Wang926454
* @date 2019/3/15 11:45
*/
@Component
public class UserUtil {

/**
* 获取当前登录用户
* @param
* @return com.wang.model.UserDto
* @author Wang926454
* @date 2019/3/15 11:48
*/
public UserDto getUser() {
return (UserDto) SecurityUtils.getSubject().getPrincipal();
}

/**
* 获取当前登录用户Id
* @param
* @return com.wang.model.UserDto
* @author Wang926454
* @date 2019/3/15 11:48
*/
public Integer getUserId() {
return getUser().getId();
}

/**
* 获取当前登录用户Token
* @param
* @return com.wang.model.UserDto
* @author Wang926454
* @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;
}

/**
* 获取当前登录用户Account
* @param
* @return com.wang.model.UserDto
* @author Wang926454
* @date 2019/3/15 11:48
*/
public String getAccount() {
return getUser().getAccount();
}
}

0 comments on commit e0dc8e1

Please sign in to comment.