全局登录态处理
This commit is contained in:
@@ -1,30 +0,0 @@
|
||||
package com.cool.store.context;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class CurrentUserContext {
|
||||
|
||||
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
|
||||
|
||||
public static CurrentUser getUser() {
|
||||
String userStr = contextHolder.get();
|
||||
if (StringUtils.isNotBlank(userStr)) {
|
||||
return JSON.parseObject(userStr, CurrentUser.class);
|
||||
}
|
||||
return new CurrentUser();
|
||||
}
|
||||
|
||||
public static void setUser(String user) {
|
||||
contextHolder.set(user);
|
||||
}
|
||||
|
||||
public static void removeUser(){
|
||||
contextHolder.remove();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.cool.store.context;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.cool.store.entity.SysRoleDO;
|
||||
import com.cool.store.enums.ErrorCodeEnum;
|
||||
import com.cool.store.exception.ServiceException;
|
||||
import org.apache.commons.lang3.SerializationException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 当前登录用户信息
|
||||
*/
|
||||
public class CurrentUserHolder {
|
||||
|
||||
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
|
||||
|
||||
public static LoginUserInfo getUser() {
|
||||
String userStr = contextHolder.get();
|
||||
if (StringUtils.isNotBlank(userStr)) {
|
||||
return JSON.parseObject(userStr, LoginUserInfo.class);
|
||||
}
|
||||
return new LoginUserInfo();
|
||||
}
|
||||
|
||||
public static void setUser(String user) {
|
||||
contextHolder.set(user);
|
||||
}
|
||||
|
||||
public static void removeUser(){
|
||||
contextHolder.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅登录态可获取
|
||||
* @return
|
||||
*/
|
||||
public static String getUserId(){
|
||||
LoginUserInfo user = getUser();
|
||||
return Optional.ofNullable(user).map(o->o.getUserId()).orElseThrow(()->new ServiceException(ErrorCodeEnum.ACCESS_TOKEN_INVALID));
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅登录态可获取
|
||||
* @return
|
||||
*/
|
||||
public static String getRoleId(){
|
||||
LoginUserInfo user = getUser();
|
||||
return Optional.ofNullable(user).map(LoginUserInfo::getSysRole).map(SysRoleDO::getRoleId).orElse(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.cool.store.entity.SysRoleDO;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CurrentUser {
|
||||
public class LoginUserInfo {
|
||||
|
||||
private String userId;
|
||||
|
||||
@@ -29,5 +29,5 @@ public class CurrentUser {
|
||||
*/
|
||||
private String avatar;
|
||||
|
||||
private SysRoleDO sysRoleDO;
|
||||
private SysRoleDO sysRole;
|
||||
}
|
||||
@@ -12,5 +12,11 @@ import java.util.List;
|
||||
*/
|
||||
public interface MenuService {
|
||||
|
||||
List<MenuTreeVO> getUserMenus(String userId);
|
||||
/**
|
||||
* 获取用户某个角色的菜单
|
||||
* @param userId
|
||||
* @param roleId
|
||||
* @return
|
||||
*/
|
||||
List<MenuTreeVO> getUserMenus(String userId, String roleId);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package com.cool.store.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.cool.store.constants.CommonConstants;
|
||||
import com.cool.store.context.CurrentUser;
|
||||
import com.cool.store.context.LoginUserInfo;
|
||||
import com.cool.store.context.DataSourceContext;
|
||||
import com.cool.store.dao.*;
|
||||
import com.cool.store.dto.login.RefreshUser;
|
||||
@@ -51,7 +50,7 @@ public class LoginServiceImpl implements LoginService {
|
||||
if (StringUtils.isEmpty(userId)) {
|
||||
throw new ServiceException(ErrorCodeEnum.USER_NOT_EXIST);
|
||||
}
|
||||
CurrentUser currentUser = new CurrentUser();
|
||||
LoginUserInfo currentUser = new LoginUserInfo();
|
||||
RefreshUser refreshUser = new RefreshUser();
|
||||
// 查企业用户
|
||||
EnterpriseUserDO enterpriseUser = enterpriseUserDAO.getUserInfoById(userId);
|
||||
@@ -76,7 +75,7 @@ public class LoginServiceImpl implements LoginService {
|
||||
enterpriseUser.setFaceUrl(finalAvatar);
|
||||
currentUser.setUserId(enterpriseUser.getUserId());
|
||||
currentUser.setIsAdmin(enterpriseUser.getIsAdmin());
|
||||
currentUser.setSysRoleDO(sysRole);
|
||||
currentUser.setSysRole(sysRole);
|
||||
currentUser.setCorpId(corpId);
|
||||
currentUser.setAvatar(enterpriseUser.getAvatar());
|
||||
//生成令牌
|
||||
|
||||
@@ -24,7 +24,7 @@ public class MenuServiceImpl implements MenuService {
|
||||
private SysMenuDAO sysMenuDAO;
|
||||
|
||||
@Override
|
||||
public List<MenuTreeVO> getUserMenus(String userId) {
|
||||
public List<MenuTreeVO> getUserMenus(String userId, String roleId) {
|
||||
List<SysMenuDO> menuList= sysMenuDAO.selectMenuAll(null, PlatFormTypeEnum.PC.getCode());
|
||||
return MenuTreeVO.dealMenuTree(CommonConstants.ZERO_LONG, menuList);
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@ package com.cool.store.config;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.cool.store.enums.ErrorCodeEnum;
|
||||
import com.cool.store.response.ResponseResult;
|
||||
import com.cool.store.context.CurrentUserContext;
|
||||
import com.cool.store.context.CurrentUserHolder;
|
||||
import com.cool.store.utils.RedisUtilPool;
|
||||
import com.cool.store.context.CurrentUser;
|
||||
import com.cool.store.context.LoginUserInfo;
|
||||
import com.google.common.collect.Lists;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -72,14 +72,14 @@ public class TokenValidateFilter implements Filter {
|
||||
String uri = reqs.getRequestURI();
|
||||
String method = reqs.getMethod();
|
||||
String userStr = "";
|
||||
CurrentUser currentUser = null;
|
||||
LoginUserInfo currentUser = null;
|
||||
boolean isInWhiteList = excludePath(uri);
|
||||
String accessToken = reqs.getHeader("accessToken");
|
||||
String key = "access_token:" + accessToken;
|
||||
if(StringUtils.isNotBlank(accessToken)){
|
||||
userStr = redisUtilPool.getString(key);
|
||||
if(StringUtils.isNotBlank(userStr)){
|
||||
currentUser = JSON.parseObject(userStr, CurrentUser.class);
|
||||
currentUser = JSON.parseObject(userStr, LoginUserInfo.class);
|
||||
}
|
||||
}
|
||||
log.info("url:{}", uri);
|
||||
@@ -105,10 +105,10 @@ public class TokenValidateFilter implements Filter {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
CurrentUserContext.setUser(userStr);
|
||||
CurrentUserHolder.setUser(userStr);
|
||||
filterChain.doFilter(servletRequest, servletResponse);
|
||||
} finally {
|
||||
CurrentUserContext.removeUser();
|
||||
CurrentUserHolder.removeUser();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.cool.store.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.cool.store.context.CurrentUserContext;
|
||||
import com.cool.store.context.CurrentUserHolder;
|
||||
import com.cool.store.dto.login.FeiShuLoginDTO;
|
||||
import com.cool.store.dto.login.UserIdInfoDTO;
|
||||
import com.cool.store.enums.ErrorCodeEnum;
|
||||
@@ -59,7 +59,7 @@ public class LoginController {
|
||||
|
||||
@GetMapping("/getUserInfoByToken")
|
||||
public ResponseResult getUserInfoByToken(){
|
||||
return ResponseResult.success(CurrentUserContext.getUser());
|
||||
return ResponseResult.success(CurrentUserHolder.getUser());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.cool.store.controller;
|
||||
|
||||
import com.cool.store.context.CurrentUserHolder;
|
||||
import com.cool.store.response.ResponseResult;
|
||||
import com.cool.store.service.MenuService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -21,7 +22,7 @@ public class MenuController {
|
||||
|
||||
@GetMapping("/menu/getUserMenus")
|
||||
public ResponseResult getUserMenus(){
|
||||
return ResponseResult.success(menuService.getUserMenus(null));
|
||||
return ResponseResult.success(menuService.getUserMenus(CurrentUserHolder.getUserId(), CurrentUserHolder.getRoleId()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user