Commit 5c77209a by guojuxing

权限拦截错误

parent a5725c0a
...@@ -45,131 +45,135 @@ public class AuthInterceptor extends HandlerInterceptorAdapter { ...@@ -45,131 +45,135 @@ public class AuthInterceptor extends HandlerInterceptorAdapter {
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
String token = UserContext.getContext().getToken(); try {
log.info("权限拦截token:{}", token); String token = UserContext.getContext().getToken();
RpcContext.getContext().getAttachments().put(Constants.USER_TOKEN, token); log.info("权限拦截token:{}", token);
System.out.println(JSON.toJSONString(RpcContext.getContext().getArguments())); RpcContext.getContext().getAttachments().put(Constants.USER_TOKEN, token);
System.out.println(JSON.toJSONString(RpcContext.getContext().getArguments()));
// 更新redis
UserContext.getContext().refresh(); // 更新redis
if (!(object.getClass().isAssignableFrom(HandlerMethod.class))) { UserContext.getContext().refresh();
return true; if (!(object.getClass().isAssignableFrom(HandlerMethod.class))) {
} return true;
}
//获取uri //获取uri
String uri = request.getRequestURI(); String uri = request.getRequestURI();
log.info("权限拦截token url:{}", uri); log.info("权限拦截token url:{}", uri);
//操作项标志位 //操作项标志位
String isControl = request.getHeader("isControl"); String isControl = request.getHeader("isControl");
//是否是操作项请求 //是否是操作项请求
boolean isOperationItemRequest = StringUtils.isNotBlank(isControl); boolean isOperationItemRequest = StringUtils.isNotBlank(isControl);
log.info("用户接口访问标志:{}", isControl); log.info("用户接口访问标志:{}", isControl);
log.info("用户接口访问路由:{}", uri); log.info("用户接口访问路由:{}", uri);
//不需要拦截的路径 //不需要拦截的路径
if (ignoreUriBeforeLoginMap.containsKey(uri)) { if (ignoreUriBeforeLoginMap.containsKey(uri)) {
return true; return true;
} }
//判断是否有权限的路径 //判断是否有权限的路径
UserDetail userDetail = UserContext.getContext().getByRedis(); UserDetail userDetail = UserContext.getContext().getByRedis();
HandlerMethod handler = (HandlerMethod) object; HandlerMethod handler = (HandlerMethod) object;
//免登录接口路由 //免登录接口路由
IgnoreLogin ignoreLogin = handler.getMethodAnnotation(IgnoreLogin.class); IgnoreLogin ignoreLogin = handler.getMethodAnnotation(IgnoreLogin.class);
if (null != ignoreLogin) { if (null != ignoreLogin) {
//有些免登录需要用到session数据 //有些免登录需要用到session数据
request.setAttribute("user", userDetail);
return true;
}
if (userDetail == null || userDetail.getUserInfo() == null) {
throw new CommonException(ErrorCode.LOGIN_INVALID.getErrorCode(), "登录信息不存在,请登录");
}
request.setAttribute("user", userDetail); request.setAttribute("user", userDetail);
return true;
}
if (userDetail == null || userDetail.getUserInfo() == null) {
throw new CommonException(ErrorCode.LOGIN_INVALID.getErrorCode(), "登录信息不存在,请登录");
}
request.setAttribute("user", userDetail);
//不需要拦截的路径 //不需要拦截的路径
if (ignoreUriAfterLoginMap.containsKey(uri)) { if (ignoreUriAfterLoginMap.containsKey(uri)) {
return true; return true;
} }
//接口调用,操作项类型的操作,需要 获取操作模块,一级、二级的ID和名称 //接口调用,操作项类型的操作,需要 获取操作模块,一级、二级的ID和名称
if (isOperationItemRequest) { if (isOperationItemRequest) {
List<MenuInfo> menuInfoList = userDetail.getMenuInfoList(); List<MenuInfo> menuInfoList = userDetail.getMenuInfoList();
Map<String, MenuInfo> moduleUrlMap = userDetail.getModuleUrlMap(); Map<String, MenuInfo> moduleUrlMap = userDetail.getModuleUrlMap();
//页面路径 //页面路径
String moduleUrl = request.getHeader("Referer"); String moduleUrl = request.getHeader("Referer");
log.info("完整路由:{}", moduleUrl); log.info("完整路由:{}", moduleUrl);
//用于下载接口的路径 //用于下载接口的路径
request.setAttribute("moduleMenuUrl", moduleUrl); request.setAttribute("moduleMenuUrl", moduleUrl);
//project_url_for_web + menu_url //project_url_for_web + menu_url
moduleUrl = getModuleUrl(moduleUrl); moduleUrl = getModuleUrl(moduleUrl);
log.info("操作模块的路由:{}", moduleUrl); log.info("操作模块的路由:{}", moduleUrl);
//获取当前操作项数据 //获取当前操作项数据
MenuInfo menuInfo = moduleUrlMap.get(moduleUrl); MenuInfo menuInfo = moduleUrlMap.get(moduleUrl);
if (menuInfo != null) { if (menuInfo != null) {
Map<String, MenuInfo> tempMap = listToMap(menuInfoList); Map<String, MenuInfo> tempMap = listToMap(menuInfoList);
log.info("接口的菜单信息:{}", JSON.toJSONString(menuInfo)); log.info("接口的菜单信息:{}", JSON.toJSONString(menuInfo));
Integer temp = menuInfo.getMenuId(); Integer temp = menuInfo.getMenuId();
//获取第二层级的页面 //获取第二层级的页面
if (menuInfo.getLevel().intValue() > 2) { if (menuInfo.getLevel().intValue() > 2) {
temp = getOperationModule(menuInfo.getMenuId(), tempMap); temp = getOperationModule(menuInfo.getMenuId(), tempMap);
while (tempMap.get(temp.toString()).getLevel().intValue() != 2) { while (tempMap.get(temp.toString()).getLevel().intValue() != 2) {
temp = getOperationModule(tempMap.get(temp.toString()).getMenuId(), tempMap); temp = getOperationModule(tempMap.get(temp.toString()).getMenuId(), tempMap);
}
} }
int secondLevelMenuId = 0;
String secondLevelMenuName = "无";
int firstLevelMenuId;
String firstLevelMenuName;
//如果是第二级的
if (tempMap.get(temp.toString()).getLevel().intValue() == 2) {
MenuInfo secondMenuInfo = tempMap.get(temp.toString());
secondLevelMenuId = secondMenuInfo.getMenuId();
secondLevelMenuName = secondMenuInfo.getMenuName();
//获取第一层级的页面
temp = getOperationModule(secondMenuInfo.getMenuId(), tempMap);
firstLevelMenuId = tempMap.get(temp.toString()).getMenuId();
firstLevelMenuName = tempMap.get(temp.toString()).getMenuName();
} else {
//第一级
MenuInfo firstMenuInfo = tempMap.get(temp.toString());
firstLevelMenuId = firstMenuInfo.getMenuId();
secondLevelMenuId = firstMenuInfo.getMenuId();
firstLevelMenuName = firstMenuInfo.getMenuName();
}
log.info("一级页面的ID和名称:{},{}", firstLevelMenuId, firstLevelMenuName);
log.info("二级页面的ID和名称:{},{}", secondLevelMenuId, secondLevelMenuName);
request.setAttribute("moduleMenuId", secondLevelMenuId);
request.setAttribute("moduleMenuName", firstLevelMenuName + "-" + secondLevelMenuName);
} }
int secondLevelMenuId = 0;
String secondLevelMenuName = "无";
int firstLevelMenuId;
String firstLevelMenuName;
//如果是第二级的
if (tempMap.get(temp.toString()).getLevel().intValue() == 2) {
MenuInfo secondMenuInfo = tempMap.get(temp.toString());
secondLevelMenuId = secondMenuInfo.getMenuId();
secondLevelMenuName = secondMenuInfo.getMenuName();
//获取第一层级的页面
temp = getOperationModule(secondMenuInfo.getMenuId(), tempMap);
firstLevelMenuId = tempMap.get(temp.toString()).getMenuId();
firstLevelMenuName = tempMap.get(temp.toString()).getMenuName();
} else {
//第一级
MenuInfo firstMenuInfo = tempMap.get(temp.toString());
firstLevelMenuId = firstMenuInfo.getMenuId();
secondLevelMenuId = firstMenuInfo.getMenuId();
firstLevelMenuName = firstMenuInfo.getMenuName();
}
log.info("一级页面的ID和名称:{},{}", firstLevelMenuId, firstLevelMenuName);
log.info("二级页面的ID和名称:{},{}", secondLevelMenuId, secondLevelMenuName);
request.setAttribute("moduleMenuId", secondLevelMenuId);
request.setAttribute("moduleMenuName", firstLevelMenuName + "-" + secondLevelMenuName);
} }
}
//超级管理员不限制
if (userDetail.getUserInfo().getSuperAdmin().intValue() == 1) {
return true;
}
//判断是否有权限
Map<String, Object> menuUrlMap = userDetail.getMenuUrlMap();
if (menuUrlMap == null || menuUrlMap.isEmpty()) {
throw new CommonException(ErrorCode.SYSTEM_ERROR.getErrorCode(), "sorry,您无该页面的访问权限,请联系超级管理员!");
}
if (isOperationItemRequest) { //超级管理员不限制
//说明是操作项 if (userDetail.getUserInfo().getSuperAdmin().intValue() == 1) {
//如果匹配上了,说明是没权限 return true;
if (menuUrlMap.containsKey(uri)) {
throw new CommonException(ErrorCode.SYSTEM_ERROR.getErrorCode(), "sorry,您无该页面的访问权限,请联系超级管理员!");
} }
} else { //判断是否有权限
//说明是页面 Map<String, Object> menuUrlMap = userDetail.getMenuUrlMap();
//如果没有匹配上,说明没权限 if (menuUrlMap == null || menuUrlMap.isEmpty()) {
if (!menuUrlMap.containsKey(uri)) {
throw new CommonException(ErrorCode.SYSTEM_ERROR.getErrorCode(), "sorry,您无该页面的访问权限,请联系超级管理员!"); throw new CommonException(ErrorCode.SYSTEM_ERROR.getErrorCode(), "sorry,您无该页面的访问权限,请联系超级管理员!");
} }
}
return true; if (isOperationItemRequest) {
//说明是操作项
//如果匹配上了,说明是没权限
if (menuUrlMap.containsKey(uri)) {
throw new CommonException(ErrorCode.SYSTEM_ERROR.getErrorCode(), "sorry,您无该页面的访问权限,请联系超级管理员!");
}
} else {
//说明是页面
//如果没有匹配上,说明没权限
if (!menuUrlMap.containsKey(uri)) {
throw new CommonException(ErrorCode.SYSTEM_ERROR.getErrorCode(), "sorry,您无该页面的访问权限,请联系超级管理员!");
}
}
return true;
} catch (Exception e) {
log.warn("权限拦截错误:{}", e.getMessage(), e);
}
} }
private static Integer getOperationModule(Integer menuId, Map<String, MenuInfo> tempMap) { private static Integer getOperationModule(Integer menuId, Map<String, MenuInfo> tempMap) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment