Skip to content

Commit

Permalink
bugfix/访问已创建任务404、体验账号项目详情404、编辑按钮操作没权限
Browse files Browse the repository at this point in the history
  • Loading branch information
freestylefly committed Jun 1, 2024
1 parent 8c5353a commit 84f0de2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.laigeoffer.pmhub.base.security.interceptor;

import com.laigeoffer.pmhub.base.core.constant.Constants;
import com.laigeoffer.pmhub.base.core.constant.SecurityConstants;
import com.laigeoffer.pmhub.base.core.context.SecurityContextHolder;
import com.laigeoffer.pmhub.base.core.core.domain.entity.SysUser;
import com.laigeoffer.pmhub.base.core.core.domain.model.LoginUser;
import com.laigeoffer.pmhub.base.core.utils.ServletUtils;
import com.laigeoffer.pmhub.base.core.utils.StringUtils;
Expand All @@ -12,20 +14,34 @@

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.Set;

/**
* 自定义请求头拦截器,将Header数据封装到线程变量中方便获取
* 注意:此拦截器会同时验证当前用户有效期自动刷新有效期
*
* @author canghe
*/
public class HeaderInterceptor implements AsyncHandlerInterceptor
{
public class HeaderInterceptor implements AsyncHandlerInterceptor {

// 需要免登录的路径集合
private static final Set<String> EXEMPTED_PATHS = new HashSet<>();

static {
// 在这里添加所有需要免登录默认展示首页的的路径
EXEMPTED_PATHS.add("/system/user/getInfo");
EXEMPTED_PATHS.add("/project/statistics");
EXEMPTED_PATHS.add("/project/doing");
EXEMPTED_PATHS.add("/project/queryMyTaskList");
EXEMPTED_PATHS.add("/project/select");
EXEMPTED_PATHS.add("/system/menu/getRouters");

}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
{
if (!(handler instanceof HandlerMethod))
{
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (!(handler instanceof HandlerMethod)) {
return true;
}

Expand All @@ -34,22 +50,51 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
SecurityContextHolder.setUserKey(ServletUtils.getHeader(request, SecurityConstants.USER_KEY));

String token = SecurityUtils.getToken();
if (StringUtils.isNotEmpty(token))
{
if (StringUtils.isNotEmpty(token)) {
LoginUser loginUser = AuthUtil.getLoginUser(token);
if (StringUtils.isNotNull(loginUser))
{
if (StringUtils.isNotNull(loginUser)) {
AuthUtil.verifyLoginUserExpire(loginUser);
SecurityContextHolder.set(SecurityConstants.LOGIN_USER, loginUser);
}
} else {
// 首页免登场景展示
// 检查请求路径是否匹配特定路径
String requestURI = request.getRequestURI();
if (isExemptedPath(requestURI)) {
// 创建一个默认的 LoginUser 对象
LoginUser defaultLoginUser = createDefaultLoginUser();
SecurityContextHolder.set(SecurityConstants.LOGIN_USER, defaultLoginUser);
}
}
return true;
}

// 判断请求路径是否匹配特定路径
private boolean isExemptedPath(String requestURI) {
// 你可以根据需要调整特定路径的匹配逻辑
return EXEMPTED_PATHS.stream().anyMatch(requestURI::startsWith);
}

// 创建一个默认的 LoginUser 对象
private LoginUser createDefaultLoginUser() {
LoginUser defaultLoginUser = new LoginUser();
defaultLoginUser.setUserId(173L); // 设置默认的用户ID
defaultLoginUser.setUsername(Constants.DEMO_ACCOUNT); // 设置默认的用户名

SysUser demoSysUser = new SysUser();
demoSysUser.setUserId(173L);
demoSysUser.setUserName(Constants.DEMO_ACCOUNT);
demoSysUser.setDeptId(100L);
demoSysUser.setStatus("0");

defaultLoginUser.setUser(demoSysUser);
// 设置其他必要的默认属性
return defaultLoginUser;
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception
{
throws Exception {
SecurityContextHolder.remove();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.laigeoffer.pmhub.base.core.constant.Constants;
import com.laigeoffer.pmhub.base.core.core.domain.entity.SysUser;
import com.laigeoffer.pmhub.base.core.core.domain.model.LoginUser;
import com.laigeoffer.pmhub.base.core.enums.LogTypeEnum;
Expand Down Expand Up @@ -62,14 +61,6 @@ public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> impl
public List<ProjectRankVO> queryProjectRankList() {
List<ProjectRankVO> projectRankVOList = new ArrayList<>(10);
LoginUser loginUser = SecurityUtils.getLoginUser();
// 对于免登场景
if (Objects.isNull(loginUser)) {
loginUser = new LoginUser();
loginUser.setUsername(Constants.DEMO_ACCOUNT);
SysUser sysUser = new SysUser();
sysUser.setNickName(Constants.DEMO_ACCOUNT);
loginUser.setUser(sysUser);
}
LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Project::getDeleted, 0);
List<Project> list = projectMapper.selectList(queryWrapper);
Expand All @@ -79,25 +70,20 @@ public List<ProjectRankVO> queryProjectRankList() {
// 对项目进度降序
List<Project> collect = list.stream().sorted(Comparator.comparing(Project::getProjectProcess).reversed())
.collect(Collectors.toList());
LoginUser finalLoginUser = loginUser;
collect.forEach(project -> {
ProjectRankVO projectRankVO = new ProjectRankVO();
projectRankVO.setProjectId(project.getId());
projectRankVO.setProjectName(project.getProjectName());
projectRankVO.setProcess(project.getProjectProcess());
projectRankVO.setUserName(finalLoginUser.getUsername());
projectRankVO.setNickName(finalLoginUser.getUser().getNickName());
projectRankVO.setUserName(loginUser.getUsername());
projectRankVO.setNickName(loginUser.getUser().getNickName());
projectRankVOList.add(projectRankVO);
});
return projectRankVOList;
}

@Override
public List<ProjectVO> queryMyProjectList() {
// 对于免登场景
if (Objects.isNull(SecurityUtils.getLoginUser())) {
return new ArrayList<>();
}
List<ProjectVO> projects = projectMapper.queryMyProjectList(SecurityUtils.getUserId());
projects.forEach( project -> {
project.setStatusName(ProjectStatusEnum.getStatusNameByStatus(project.getStatus()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,6 @@ public List<TaskStatisticsVO> queryTaskStatisticsList() {

@Override
public PageInfo<TaskResVO> queryMyTaskList(TaskReqVO taskReqVO) {
// 免登录场景
if (com.laigeoffer.pmhub.base.core.utils.StringUtils.isNull(SecurityUtils.getLoginUser())) {
return new PageInfo<>();
}
PageInfo<TaskResVO> pageInfo;
PageHelper.startPage(taskReqVO.getPageNum(), taskReqVO.getPageSize());
switch (taskReqVO.getType()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.laigeoffer.pmhub.system.controller;

import com.laigeoffer.pmhub.base.core.annotation.Log;
import com.laigeoffer.pmhub.base.core.constant.Constants;
import com.laigeoffer.pmhub.base.core.constant.UserConstants;
import com.laigeoffer.pmhub.base.core.core.controller.BaseController;
import com.laigeoffer.pmhub.base.core.core.domain.AjaxResult;
import com.laigeoffer.pmhub.base.core.core.domain.entity.SysMenu;
import com.laigeoffer.pmhub.base.core.core.domain.entity.SysUser;
import com.laigeoffer.pmhub.base.core.core.domain.model.LoginUser;
import com.laigeoffer.pmhub.base.core.enums.BusinessType;
import com.laigeoffer.pmhub.base.core.utils.StringUtils;
Expand Down Expand Up @@ -135,14 +133,6 @@ public AjaxResult remove(@PathVariable("menuId") Long menuId) {
@GetMapping("getRouters")
public AjaxResult getRouters() {
LoginUser loginUser = SecurityUtils.getLoginUser();
// 默认免登录场景下
if (StringUtils.isNull(loginUser)) {
// 根据用户名查出体验账号
SysUser DemoSysUser = userService.selectUserByUserName(Constants.DEMO_ACCOUNT);
loginUser = new LoginUser();
loginUser.setUserId(DemoSysUser.getUserId());
loginUser.setUser(DemoSysUser);
}
Long userId = loginUser.getUserId();
List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
return success(menuService.buildMenus(menus));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.laigeoffer.pmhub.system.controller;

import com.laigeoffer.pmhub.base.core.annotation.Log;
import com.laigeoffer.pmhub.base.core.constant.Constants;
import com.laigeoffer.pmhub.base.core.constant.UserConstants;
import com.laigeoffer.pmhub.base.core.core.controller.BaseController;
import com.laigeoffer.pmhub.base.core.core.domain.AjaxResult;
Expand Down Expand Up @@ -139,12 +138,6 @@ public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long
@GetMapping("/getInfo")
public AjaxResult getInfo() {
LoginUser loginUser = SecurityUtils.getLoginUser();
if (StringUtils.isNull(loginUser)) {
// 根据用户名查出体验账号
SysUser DemoSysUser = userService.selectUserByUserName(Constants.DEMO_ACCOUNT);
loginUser = new LoginUser();
loginUser.setUser(DemoSysUser);
}
SysUser user = loginUser.getUser();
// 角色集合
Set<String> roles = permissionService.getRolePermission(user);
Expand Down
3 changes: 0 additions & 3 deletions pmhub-ui/src/api/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ export function register(data) {
export function getInfo() {
return request({
url: '/system/user/getInfo',
headers: {
isToken: false
},
method: 'get'
})
}
Expand Down

0 comments on commit 84f0de2

Please sign in to comment.