src/main/java/com/ruoyi/common/utils/SecurityUtils.java
@@ -14,6 +14,37 @@ */ public class SecurityUtils { /** * 用户ID **/ public static Long getUserId() { try { return getLoginUser().getUserId(); } catch (Exception e) { throw new CustomException("获取用户ID异常", HttpStatus.UNAUTHORIZED); } } /** * 获取部门ID **/ public static Long getDeptId() { try { return getLoginUser().getDeptId(); } catch (Exception e) { throw new CustomException("获取部门ID异常", HttpStatus.UNAUTHORIZED); } } /** * 获取用户账户 **/ src/main/java/com/ruoyi/framework/security/LoginUser.java
@@ -17,6 +17,16 @@ private static final long serialVersionUID = 1L; /** * 用户ID */ private Long userId; /** * 部门ID */ private Long deptId; /** * 用户唯一标识 */ private String token; @@ -61,6 +71,26 @@ */ private SysUser user; public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } public Long getDeptId() { return deptId; } public void setDeptId(Long deptId) { this.deptId = deptId; } public String getToken() { return token; @@ -81,6 +111,14 @@ this.permissions = permissions; } public LoginUser(Long userId, Long deptId, SysUser user, Set<String> permissions) { this.userId = userId; this.deptId = deptId; this.user = user; this.permissions = permissions; } @JsonIgnore @Override public String getPassword() src/main/java/com/ruoyi/framework/security/service/UserDetailsServiceImpl.java
@@ -55,6 +55,6 @@ public UserDetails createLoginUser(SysUser user) { return new LoginUser(user, permissionService.getMenuPermission(user)); return new LoginUser(user.getUserId(), user.getDeptId(), user, permissionService.getMenuPermission(user)); } } src/main/java/com/ruoyi/framework/web/controller/BaseController.java
@@ -11,8 +11,10 @@ import com.github.pagehelper.PageInfo; import com.ruoyi.common.constant.HttpStatus; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.sql.SqlUtil; import com.ruoyi.framework.security.LoginUser; import com.ruoyi.framework.web.domain.AjaxResult; import com.ruoyi.framework.web.page.PageDomain; import com.ruoyi.framework.web.page.TableDataInfo; @@ -87,17 +89,6 @@ } /** * 响应返回结果 * * @param result 结果 * @return 操作结果 */ protected AjaxResult toAjax(boolean result) { return result ? success() : error(); } /** * 返回成功 */ public AjaxResult success() @@ -139,4 +130,47 @@ { return rows > 0 ? AjaxResult.success() : AjaxResult.error(); } /** * 响应返回结果 * * @param result 结果 * @return 操作结果 */ protected AjaxResult toAjax(boolean result) { return result ? success() : error(); } /** * 获取用户缓存信息 */ public LoginUser getLoginUser() { return SecurityUtils.getLoginUser(); } /** * 获取登录用户id */ public Long getUserId() { return getLoginUser().getUserId(); } /** * 获取登录部门id */ public Long getDeptId() { return getLoginUser().getDeptId(); } /** * 获取登录用户名 */ public String getUsername() { return getLoginUser().getUsername(); } } src/main/java/com/ruoyi/project/monitor/controller/SysJobController.java
@@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.exception.job.TaskException; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.job.CronUtils; import com.ruoyi.common.utils.poi.ExcelUtil; @@ -93,7 +92,7 @@ { return error("新增任务'" + job.getJobName() + "'失败,目标字符串不允许'http(s)//'调用"); } job.setCreateBy(SecurityUtils.getUsername()); job.setCreateBy(getUsername()); return toAjax(jobService.insertJob(job)); } @@ -117,7 +116,7 @@ { return error("修改任务'" + job.getJobName() + "'失败,目标字符串不允许'http(s)//'调用"); } job.setUpdateBy(SecurityUtils.getUsername()); job.setUpdateBy(getUsername()); return toAjax(jobService.updateJob(job)); } src/main/java/com/ruoyi/project/system/controller/SysConfigController.java
@@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.aspectj.lang.annotation.Log; import com.ruoyi.framework.aspectj.lang.enums.BusinessType; @@ -88,7 +87,7 @@ { return AjaxResult.error("新增参数'" + config.getConfigName() + "'失败,参数键名已存在"); } config.setCreateBy(SecurityUtils.getUsername()); config.setCreateBy(getUsername()); return toAjax(configService.insertConfig(config)); } @@ -104,7 +103,7 @@ { return AjaxResult.error("修改参数'" + config.getConfigName() + "'失败,参数键名已存在"); } config.setUpdateBy(SecurityUtils.getUsername()); config.setUpdateBy(getUsername()); return toAjax(configService.updateConfig(config)); } src/main/java/com/ruoyi/project/system/controller/SysDeptController.java
@@ -15,7 +15,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.framework.aspectj.lang.annotation.Log; import com.ruoyi.framework.aspectj.lang.enums.BusinessType; @@ -113,7 +112,7 @@ { return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在"); } dept.setCreateBy(SecurityUtils.getUsername()); dept.setCreateBy(getUsername()); return toAjax(deptService.insertDept(dept)); } @@ -138,7 +137,7 @@ { return AjaxResult.error("该部门包含未停用的子部门!"); } dept.setUpdateBy(SecurityUtils.getUsername()); dept.setUpdateBy(getUsername()); return toAjax(deptService.updateDept(dept)); } src/main/java/com/ruoyi/project/system/controller/SysDictDataController.java
@@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.aspectj.lang.annotation.Log; @@ -91,7 +90,7 @@ @PostMapping public AjaxResult add(@Validated @RequestBody SysDictData dict) { dict.setCreateBy(SecurityUtils.getUsername()); dict.setCreateBy(getUsername()); return toAjax(dictDataService.insertDictData(dict)); } @@ -103,7 +102,7 @@ @PutMapping public AjaxResult edit(@Validated @RequestBody SysDictData dict) { dict.setUpdateBy(SecurityUtils.getUsername()); dict.setUpdateBy(getUsername()); return toAjax(dictDataService.updateDictData(dict)); } src/main/java/com/ruoyi/project/system/controller/SysDictTypeController.java
@@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.aspectj.lang.annotation.Log; import com.ruoyi.framework.aspectj.lang.enums.BusinessType; @@ -76,7 +75,7 @@ { return AjaxResult.error("新增字典'" + dict.getDictName() + "'失败,字典类型已存在"); } dict.setCreateBy(SecurityUtils.getUsername()); dict.setCreateBy(getUsername()); return toAjax(dictTypeService.insertDictType(dict)); } @@ -92,7 +91,7 @@ { return AjaxResult.error("修改字典'" + dict.getDictName() + "'失败,字典类型已存在"); } dict.setUpdateBy(SecurityUtils.getUsername()); dict.setUpdateBy(getUsername()); return toAjax(dictTypeService.updateDictType(dict)); } src/main/java/com/ruoyi/project/system/controller/SysLoginController.java
@@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.ServletUtils; import com.ruoyi.framework.security.LoginBody; import com.ruoyi.framework.security.LoginUser; @@ -85,10 +86,8 @@ @GetMapping("getRouters") public AjaxResult getRouters() { LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest()); // 用户信息 SysUser user = loginUser.getUser(); List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId()); Long userId = SecurityUtils.getUserId(); List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId); return AjaxResult.success(menuService.buildMenus(menus)); } } src/main/java/com/ruoyi/project/system/controller/SysMenuController.java
@@ -13,13 +13,9 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.ServletUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.framework.aspectj.lang.annotation.Log; import com.ruoyi.framework.aspectj.lang.enums.BusinessType; import com.ruoyi.framework.security.LoginUser; import com.ruoyi.framework.security.service.TokenService; import com.ruoyi.framework.web.controller.BaseController; import com.ruoyi.framework.web.domain.AjaxResult; import com.ruoyi.project.system.domain.SysMenu; @@ -37,9 +33,6 @@ @Autowired private ISysMenuService menuService; @Autowired private TokenService tokenService; /** * 获取菜单列表 */ @@ -47,8 +40,7 @@ @GetMapping("/list") public AjaxResult list(SysMenu menu) { LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest()); Long userId = loginUser.getUser().getUserId(); Long userId = getUserId(); List<SysMenu> menus = menuService.selectMenuList(menu, userId); return AjaxResult.success(menus); } @@ -69,8 +61,7 @@ @GetMapping("/treeselect") public AjaxResult treeselect(SysMenu menu) { LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest()); Long userId = loginUser.getUser().getUserId(); Long userId = getUserId(); List<SysMenu> menus = menuService.selectMenuList(menu, userId); return AjaxResult.success(menuService.buildMenuTreeSelect(menus)); } @@ -81,8 +72,7 @@ @GetMapping(value = "/roleMenuTreeselect/{roleId}") public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId) { LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest()); List<SysMenu> menus = menuService.selectMenuList(loginUser.getUser().getUserId()); List<SysMenu> menus = menuService.selectMenuList(getUserId()); AjaxResult ajax = AjaxResult.success(); ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId)); ajax.put("menus", menuService.buildMenuTreeSelect(menus)); @@ -105,7 +95,7 @@ { return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头"); } menu.setCreateBy(SecurityUtils.getUsername()); menu.setCreateBy(getUsername()); return toAjax(menuService.insertMenu(menu)); } @@ -129,7 +119,7 @@ { return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,上级菜单不能选择自己"); } menu.setUpdateBy(SecurityUtils.getUsername()); menu.setUpdateBy(getUsername()); return toAjax(menuService.updateMenu(menu)); } src/main/java/com/ruoyi/project/system/controller/SysNoticeController.java
@@ -12,7 +12,6 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.framework.aspectj.lang.annotation.Log; import com.ruoyi.framework.aspectj.lang.enums.BusinessType; import com.ruoyi.framework.web.controller.BaseController; @@ -63,7 +62,7 @@ @PostMapping public AjaxResult add(@Validated @RequestBody SysNotice notice) { notice.setCreateBy(SecurityUtils.getUsername()); notice.setCreateBy(getUsername()); return toAjax(noticeService.insertNotice(notice)); } @@ -75,7 +74,7 @@ @PutMapping public AjaxResult edit(@Validated @RequestBody SysNotice notice) { notice.setUpdateBy(SecurityUtils.getUsername()); notice.setUpdateBy(getUsername()); return toAjax(noticeService.updateNotice(notice)); } src/main/java/com/ruoyi/project/system/controller/SysPostController.java
@@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.aspectj.lang.annotation.Log; import com.ruoyi.framework.aspectj.lang.enums.BusinessType; @@ -83,7 +82,7 @@ { return AjaxResult.error("新增岗位'" + post.getPostName() + "'失败,岗位编码已存在"); } post.setCreateBy(SecurityUtils.getUsername()); post.setCreateBy(getUsername()); return toAjax(postService.insertPost(post)); } @@ -103,7 +102,7 @@ { return AjaxResult.error("修改岗位'" + post.getPostName() + "'失败,岗位编码已存在"); } post.setUpdateBy(SecurityUtils.getUsername()); post.setUpdateBy(getUsername()); return toAjax(postService.updatePost(post)); } src/main/java/com/ruoyi/project/system/controller/SysRoleController.java
@@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.ServletUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.poi.ExcelUtil; @@ -97,7 +96,7 @@ { return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败,角色权限已存在"); } role.setCreateBy(SecurityUtils.getUsername()); role.setCreateBy(getUsername()); return toAjax(roleService.insertRole(role)); } @@ -119,7 +118,7 @@ { return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色权限已存在"); } role.setUpdateBy(SecurityUtils.getUsername()); role.setUpdateBy(getUsername()); if (roleService.updateRole(role) > 0) { @@ -157,7 +156,7 @@ public AjaxResult changeStatus(@RequestBody SysRole role) { roleService.checkRoleAllowed(role); role.setUpdateBy(SecurityUtils.getUsername()); role.setUpdateBy(getUsername()); return toAjax(roleService.updateRoleStatus(role)); } src/main/java/com/ruoyi/project/system/controller/SysUserController.java
@@ -137,7 +137,7 @@ { return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,邮箱账号已存在"); } user.setCreateBy(SecurityUtils.getUsername()); user.setCreateBy(getUsername()); user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); return toAjax(userService.insertUser(user)); } @@ -161,7 +161,7 @@ { return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在"); } user.setUpdateBy(SecurityUtils.getUsername()); user.setUpdateBy(getUsername()); return toAjax(userService.updateUser(user)); } @@ -186,7 +186,7 @@ { userService.checkUserAllowed(user); user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); user.setUpdateBy(SecurityUtils.getUsername()); user.setUpdateBy(getUsername()); return toAjax(userService.resetPwd(user)); } @@ -199,7 +199,7 @@ public AjaxResult changeStatus(@RequestBody SysUser user) { userService.checkUserAllowed(user); user.setUpdateBy(SecurityUtils.getUsername()); user.setUpdateBy(getUsername()); return toAjax(userService.updateUserStatus(user)); }