diff --git a/src/main/java/com/wang/config/shiro/UserRealm.java b/src/main/java/com/wang/config/shiro/UserRealm.java index c54b1e1..fb27ce9 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(token, token, "userRealm"); + return new SimpleAuthenticationInfo(userDto, token, token); } } throw new AuthenticationException("Token已过期(Token expired or incorrect.)"); diff --git a/src/main/java/com/wang/controller/UserController.java b/src/main/java/com/wang/controller/UserController.java index ffd6497..78bce44 100644 --- a/src/main/java/com/wang/controller/UserController.java +++ b/src/main/java/com/wang/controller/UserController.java @@ -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; @@ -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; } @@ -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 diff --git a/src/main/java/com/wang/util/UserUtil.java b/src/main/java/com/wang/util/UserUtil.java new file mode 100644 index 0000000..901801f --- /dev/null +++ b/src/main/java/com/wang/util/UserUtil.java @@ -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 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(); + } +}