diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/admin/BrandController.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/admin/BrandController.java index b03cb4479..c7a3354a4 100644 --- a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/admin/BrandController.java +++ b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/admin/BrandController.java @@ -39,7 +39,7 @@ public class BrandController { }) @GetMapping public Result list(String queryMode, Integer page, Integer limit, String name) { - QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode); + QueryModeEnum queryModeEnum = QueryModeEnum.getByCode(queryMode); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); switch (queryModeEnum) { case PAGE: diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/admin/CategoryController.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/admin/CategoryController.java index 7755a1257..c5e7bead8 100644 --- a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/admin/CategoryController.java +++ b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/admin/CategoryController.java @@ -39,7 +39,7 @@ public class CategoryController { }) @GetMapping public Result list(String queryMode) { - QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode); + QueryModeEnum queryModeEnum = QueryModeEnum.getByCode(queryMode); PmsCategory category = new PmsCategory(); List list; switch (queryModeEnum) { diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/admin/SpuController.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/admin/SpuController.java index f52cfbbe2..5e9247540 100644 --- a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/admin/SpuController.java +++ b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/admin/SpuController.java @@ -45,7 +45,7 @@ public class SpuController { String name, Long categoryId ) { - QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode); + QueryModeEnum queryModeEnum = QueryModeEnum.getByCode(queryMode); switch (queryModeEnum) { case PAGE: IPage result = iPmsSpuService.list( diff --git a/mall-sms/sms-boot/src/main/java/com/youlai/mall/sms/controller/admin/AdvertController.java b/mall-sms/sms-boot/src/main/java/com/youlai/mall/sms/controller/admin/AdvertController.java index 980e7420c..ce4846d64 100644 --- a/mall-sms/sms-boot/src/main/java/com/youlai/mall/sms/controller/admin/AdvertController.java +++ b/mall-sms/sms-boot/src/main/java/com/youlai/mall/sms/controller/admin/AdvertController.java @@ -42,7 +42,7 @@ public class AdvertController { Integer page, Integer limit, String name) { - QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode); + QueryModeEnum queryModeEnum = QueryModeEnum.getByCode(queryMode); switch (queryModeEnum) { default: LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper() diff --git a/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/controller/admin/MemberController.java b/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/controller/admin/MemberController.java index 2338367a9..dc37b551b 100644 --- a/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/controller/admin/MemberController.java +++ b/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/controller/admin/MemberController.java @@ -43,7 +43,7 @@ public class MemberController { Integer limit, String nickname ) { - QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode); + QueryModeEnum queryModeEnum = QueryModeEnum.getByCode(queryMode); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.ne(UmsMember::getDeleted, GlobalConstants.DELETED_VALUE); switch (queryModeEnum) { diff --git a/youlai-admin/admin-api/src/main/java/com/youlai/admin/api/UserFeignClient.java b/youlai-admin/admin-api/src/main/java/com/youlai/admin/api/UserFeignClient.java index 2c938852c..3e25ae3cd 100644 --- a/youlai-admin/admin-api/src/main/java/com/youlai/admin/api/UserFeignClient.java +++ b/youlai-admin/admin-api/src/main/java/com/youlai/admin/api/UserFeignClient.java @@ -2,6 +2,7 @@ package com.youlai.admin.api; import com.youlai.admin.api.fallback.UserFeignFallbackClient; import com.youlai.admin.pojo.dto.UserDTO; +import com.youlai.admin.pojo.entity.SysUser; import com.youlai.common.result.Result; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @@ -10,6 +11,6 @@ import org.springframework.web.bind.annotation.PathVariable; @FeignClient(value = "youlai-admin", fallback = UserFeignFallbackClient.class) public interface UserFeignClient { - @GetMapping("/v1/users/username/{username}") - Result getUserByUsername(@PathVariable String username); + @GetMapping("/users/username/{username}") + Result getUserByUsername(@PathVariable String username); } diff --git a/youlai-admin/admin-api/src/main/java/com/youlai/admin/api/fallback/UserFeignFallbackClient.java b/youlai-admin/admin-api/src/main/java/com/youlai/admin/api/fallback/UserFeignFallbackClient.java index 9682b28b6..29ed4f1be 100644 --- a/youlai-admin/admin-api/src/main/java/com/youlai/admin/api/fallback/UserFeignFallbackClient.java +++ b/youlai-admin/admin-api/src/main/java/com/youlai/admin/api/fallback/UserFeignFallbackClient.java @@ -2,6 +2,7 @@ package com.youlai.admin.api.fallback; import com.youlai.admin.api.UserFeignClient; import com.youlai.admin.pojo.dto.UserDTO; +import com.youlai.admin.pojo.entity.SysUser; import com.youlai.common.result.Result; import com.youlai.common.result.ResultCode; import lombok.extern.slf4j.Slf4j; @@ -16,7 +17,7 @@ import org.springframework.stereotype.Component; public class UserFeignFallbackClient implements UserFeignClient { @Override - public Result getUserByUsername(String username) { + public Result getUserByUsername(String username) { log.error("feign远程调用系统用户服务异常后的降级方法"); return Result.failed(ResultCode.DEGRADATION); } diff --git a/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/dto/UserDTO.java b/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/dto/UserDTO.java index c39a19dc1..77efe03ee 100644 --- a/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/dto/UserDTO.java +++ b/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/dto/UserDTO.java @@ -13,6 +13,6 @@ public class UserDTO { private String username; private String password; private Integer status; - private List roleIds; + private List roles; } diff --git a/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/entity/SysUser.java b/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/entity/SysUser.java index b44cd3177..664cfc2a7 100644 --- a/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/entity/SysUser.java +++ b/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/entity/SysUser.java @@ -29,6 +29,8 @@ public class SysUser extends BaseEntity { private String password; + private String email; + private Integer status; private Long deptId; @@ -46,4 +48,8 @@ public class SysUser extends BaseEntity { @TableField(exist = false) private String roleNames; + @TableField(exist = false) + private List roles; + + } diff --git a/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/vo/UserVO.java b/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/vo/UserVO.java index fecae7578..36b0850a7 100644 --- a/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/vo/UserVO.java +++ b/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/vo/UserVO.java @@ -13,7 +13,7 @@ public class UserVO { private String avatar; - private List roles; + private List roles; private List perms ; diff --git a/youlai-admin/admin-boot/pom.xml b/youlai-admin/admin-boot/pom.xml index c1c606ab3..0d6e2905b 100644 --- a/youlai-admin/admin-boot/pom.xml +++ b/youlai-admin/admin-boot/pom.xml @@ -96,6 +96,12 @@ common-es + + com.youlai + common-log + ${youlai.version} + + diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/common/constant/SystemConstants.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/common/constant/SystemConstants.java index 413870105..1a11ad5fd 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/common/constant/SystemConstants.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/common/constant/SystemConstants.java @@ -12,6 +12,6 @@ public interface SystemConstants { String ROOT_ROLE_CODE = "ROOT"; - String BTN_PERM_ROLES_PREFIX="system:btn_perm_roles:"; - String URL_PERM_ROLES_PREFIX="system:url_perm_roles:"; + String BTN_PERM_ROLES_KEY="system:btn_perm_roles:"; + String URL_PERM_ROLES_KEY="system:url_perm_roles:"; } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/component/InitPermissionRoles.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/component/InitPermissionRoles.java index 018a27325..dd2a91373 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/component/InitPermissionRoles.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/component/InitPermissionRoles.java @@ -18,6 +18,6 @@ public class InitPermissionRoles implements CommandLineRunner { @Override public void run(String... args) { - iSysPermissionService.refreshPermissionRolesCache(); + iSysPermissionService.refreshPermRolesCache(); } } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DeptController.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DeptController.java index a21dc86d1..4f68a4a86 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DeptController.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DeptController.java @@ -50,7 +50,7 @@ public class DeptController { .orderByAsc(SysDept::getSort) .orderByDesc(SysDept::getUpdateTime) .orderByDesc(SysDept::getCreateTime); - QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode); + QueryModeEnum queryModeEnum = QueryModeEnum.getByCode(queryMode); switch (queryModeEnum) { case LIST: diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DictController.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DictController.java index 8ea944632..fa8f6da88 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DictController.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DictController.java @@ -46,7 +46,7 @@ public class DictController { Integer page, Integer limit, String name) { - QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode); + QueryModeEnum queryModeEnum = QueryModeEnum.getByCode(queryMode); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper() .like(StrUtil.isNotBlank(name), SysDict::getName, StrUtil.trimToNull(name)) .orderByDesc(SysDict::getUpdateTime) diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DictItemController.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DictItemController.java index 9dda5851e..47c4607ca 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DictItemController.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DictItemController.java @@ -46,7 +46,7 @@ public class DictItemController { String name, String dictCode ) { - QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode); + QueryModeEnum queryModeEnum = QueryModeEnum.getByCode(queryMode); switch (queryModeEnum) { case PAGE: IPage result = iSysDictItemService.list(new Page<>(page, limit), new SysDictItem().setName(name).setDictCode(dictCode)); diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/MenuController.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/MenuController.java index 72b12c943..b30165e2a 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/MenuController.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/MenuController.java @@ -42,7 +42,7 @@ public class MenuController { @GetMapping public Result list(String queryMode, String name) { - QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode); + QueryModeEnum queryModeEnum = QueryModeEnum.getByCode(queryMode); LambdaQueryWrapper baseQuery = new LambdaQueryWrapper() .orderByAsc(SysMenu::getSort) diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/PermissionController.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/PermissionController.java index a273fabeb..58f2fef17 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/PermissionController.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/PermissionController.java @@ -42,7 +42,7 @@ public class PermissionController { String name, Long menuId ) { - QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode); + QueryModeEnum queryModeEnum = QueryModeEnum.getByCode(queryMode); switch (queryModeEnum) { case PAGE: IPage result = iSysPermissionService.list( @@ -72,7 +72,7 @@ public class PermissionController { public Result add(@RequestBody SysPermission permission) { boolean result = iSysPermissionService.save(permission); if (result) { - iSysPermissionService.refreshPermissionRolesCache(); + iSysPermissionService.refreshPermRolesCache(); } return Result.judge(result); } @@ -88,7 +88,7 @@ public class PermissionController { @RequestBody SysPermission permission) { boolean result = iSysPermissionService.updateById(permission); if (result) { - iSysPermissionService.refreshPermissionRolesCache(); + iSysPermissionService.refreshPermRolesCache(); } return Result.judge(result); } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/RoleController.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/RoleController.java index 2e9989150..660cee9b0 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/RoleController.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/RoleController.java @@ -1,5 +1,6 @@ package com.youlai.admin.controller; +import cn.hutool.core.lang.Assert; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; @@ -14,6 +15,7 @@ import com.youlai.common.constant.GlobalConstants; import com.youlai.common.enums.QueryModeEnum; import com.youlai.common.result.Result; import com.youlai.common.result.ResultCode; +import com.youlai.common.web.util.JwtUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; @@ -21,7 +23,6 @@ import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; - import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -33,6 +34,7 @@ import java.util.stream.Collectors; @AllArgsConstructor public class RoleController { + private ISysRoleService iSysRoleService; private ISysRoleMenuService iSysRoleMenuService; @@ -47,19 +49,24 @@ public class RoleController { @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long"), @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long"), @ApiImplicitParam(name = "name", value = "角色名称", paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "tenantId", value = "租户编码", paramType = "query", dataType = "Long") }) @GetMapping public Result list( String queryMode, Integer page, Integer limit, - String name + String name, + Integer tenantId ) { - QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode); + QueryModeEnum queryModeEnum = QueryModeEnum.getByCode(queryMode); + List roles = JwtUtils.getRoles(); + boolean isRoot = roles.contains(GlobalConstants.ROOT_ROLE_CODE); // 判断是否是超级管理员 switch (queryModeEnum) { case PAGE: LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper() .like(StrUtil.isNotBlank(name), SysRole::getName, name) + .ne(!isRoot, SysRole::getCode, GlobalConstants.ROOT_ROLE_CODE) .orderByAsc(SysRole::getSort) .orderByDesc(SysRole::getUpdateTime) .orderByDesc(SysRole::getCreateTime); @@ -67,7 +74,10 @@ public class RoleController { return Result.success(result.getRecords(), result.getTotal()); case LIST: List list = iSysRoleService.list(new LambdaQueryWrapper() - .eq(SysRole::getStatus, GlobalConstants.STATUS_YES)); + .eq(SysRole::getStatus, GlobalConstants.STATUS_YES) + .ne(!isRoot, SysRole::getCode, GlobalConstants.ROOT_ROLE_CODE) + .orderByAsc(SysRole::getSort) + ); return Result.success(list); default: return Result.failed(ResultCode.QUERY_MODE_IS_NULL); @@ -80,9 +90,14 @@ public class RoleController { @ApiImplicitParam(name = "role", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysRole") @PostMapping public Result add(@RequestBody SysRole role) { + int count = iSysRoleService.count(new LambdaQueryWrapper() + .eq(SysRole::getCode, role.getCode()) + .ne(SysRole::getDeleted, 1) + ); + Assert.isTrue(count == 0, "角色编码已存在"); boolean result = iSysRoleService.save(role); if (result) { - iSysPermissionService.refreshPermissionRolesCache(); + iSysPermissionService.refreshPermRolesCache(); } return Result.judge(result); } @@ -96,9 +111,15 @@ public class RoleController { public Result update( @PathVariable Long id, @RequestBody SysRole role) { + int count = iSysRoleService.count(new LambdaQueryWrapper() + .eq(SysRole::getCode, role.getCode()) + .ne(SysRole::getId, id) + .ne(SysRole::getDeleted, 1) + ); + Assert.isTrue(count == 0, "角色编码已存在"); boolean result = iSysRoleService.updateById(role); if (result) { - iSysPermissionService.refreshPermissionRolesCache(); + iSysPermissionService.refreshPermRolesCache(); } return Result.judge(result); } @@ -110,7 +131,7 @@ public class RoleController { boolean result = iSysRoleService.delete(Arrays.asList(ids.split(",")).stream() .map(id -> Long.parseLong(id)).collect(Collectors.toList())); if (result) { - iSysPermissionService.refreshPermissionRolesCache(); + iSysPermissionService.refreshPermRolesCache(); } return Result.judge(result); } @@ -127,38 +148,37 @@ public class RoleController { .set(role.getStatus() != null, SysRole::getStatus, role.getStatus()); boolean result = iSysRoleService.update(updateWrapper); if (result) { - iSysPermissionService.refreshPermissionRolesCache(); + iSysPermissionService.refreshPermRolesCache(); } return Result.judge(result); } @ApiOperation(value = "角色拥有的菜单ID集合") @ApiImplicitParam(name = "id", value = "角色id", required = true, paramType = "path", dataType = "Long") - @GetMapping("/{id}/menu_ids") - public Result roleMenuIds(@PathVariable("id") Long roleId) { + @GetMapping("/{id}/menus") + public Result listRoleMenu(@PathVariable("id") Long roleId) { List menuIds = iSysRoleMenuService.listMenuIds(roleId); return Result.success(menuIds); } @ApiOperation(value = "角色拥有的权限ID集合") @ApiImplicitParams({ - @ApiImplicitParam(name = "id", value = "角色id", required = true, paramType = "path", dataType = "Long"), - @ApiImplicitParam(name = "type", value = "权限类型", paramType = "query", dataType = "Integer"), + @ApiImplicitParam(name = "id", value = "角色id", required = true, paramType = "path", dataType = "Integer"), + @ApiImplicitParam(name = "menuId", value = "菜单ID", paramType = "query", dataType = "Integer"), }) - @GetMapping("/{id}/permission_ids") - public Result rolePermissionIds(@PathVariable("id") Long roleId, @RequestParam Integer type) { - List permissionIds = iSysRolePermissionService.listPermissionIds(roleId, type); + @GetMapping("/{id}/permissions") + public Result listRolePermission(@PathVariable("id") Long roleId, Long menuId) { + List permissionIds = iSysRolePermissionService.listPermissionIds(menuId, roleId); return Result.success(permissionIds); } - @ApiOperation(value = "修改角色菜单") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "角色id", required = true, paramType = "path", dataType = "Long"), @ApiImplicitParam(name = "role", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysRole") }) - @PutMapping(value = "/{id}/menu_ids") - public Result updateRoleMenuIds( + @PutMapping(value = "/{id}/menus") + public Result updateRoleMenu( @PathVariable("id") Long roleId, @RequestBody SysRole role) { @@ -172,14 +192,14 @@ public class RoleController { @ApiImplicitParam(name = "id", value = "角色id", required = true, paramType = "path", dataType = "Long"), @ApiImplicitParam(name = "rolePermission", value = "实体JSON对象", required = true, paramType = "body", dataType = "RolePermissionDTO") }) - @PutMapping(value = "/{id}/permission_ids") - public Result updateRolePermissionIds( + @PutMapping(value = "/{id}/permissions") + public Result updateRolePermission( @PathVariable("id") Long roleId, @RequestBody RolePermissionDTO rolePermission) { rolePermission.setRoleId(roleId); boolean result = iSysRolePermissionService.update(rolePermission); if (result) { - iSysPermissionService.refreshPermissionRolesCache(); + iSysPermissionService.refreshPermRolesCache(); } return Result.judge(result); } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/UserController.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/UserController.java index 37a51168c..4895b69fb 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/UserController.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/UserController.java @@ -139,26 +139,9 @@ public class UserController { @ApiOperation(value = "根据用户名获取用户信息") @ApiImplicitParam(name = "username", value = "用户名", required = true, paramType = "path", dataType = "String") @GetMapping("/username/{username}") - public Result getUserByUsername(@PathVariable String username) { - SysUser user = iSysUserService.getOne(new LambdaQueryWrapper() - .eq(SysUser::getUsername, username)); - - // 用户不存在,返回自定义异常,让调用端处理后续逻辑 - if (user == null) { - return Result.failed(ResultCode.USER_NOT_EXIST); - } - - // Entity->DTO - UserDTO userDTO = new UserDTO(); - BeanUtil.copyProperties(user, userDTO); - - // 获取用户的角色ID集合 - List roleIds = iSysUserRoleService.list(new LambdaQueryWrapper() - .eq(SysUserRole::getUserId, user.getId()) - ).stream().map(item -> item.getRoleId()).collect(Collectors.toList()); - userDTO.setRoleIds(roleIds); - - return Result.success(userDTO); + public Result getUserByUsername(@PathVariable String username) { + SysUser user = iSysUserService.getByUsername(username); + return Result.success(user); } @@ -175,11 +158,11 @@ public class UserController { BeanUtil.copyProperties(user, userVO); // 用户角色信息 - List roleIds = JwtUtils.getRoleIds(); - userVO.setRoles(roleIds); + List roles = JwtUtils.getRoles(); + userVO.setRoles(roles); // 用户按钮权限信息 - List perms = iSysPermissionService.listPermsByRoleIds(roleIds, PermTypeEnum.BUTTON.getValue()); + List perms = iSysPermissionService.listPermByRoles(roles); userVO.setPerms(perms); return Result.success(userVO); diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysPermissionMapper.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysPermissionMapper.java index a634b8bd1..e825171c0 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysPermissionMapper.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysPermissionMapper.java @@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.youlai.admin.pojo.entity.SysPermission; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Mapper; - import java.util.List; @Mapper @@ -37,20 +36,6 @@ public interface SysPermissionMapper extends BaseMapper { List list(Page page, SysPermission permission); - @Select({ - "" - }) - List listPermsByRoleIds(List roleIds, Integer type); + + List listPermByRoles(List roles); } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysRolePermissionMapper.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysRolePermissionMapper.java index bf519de7f..2d465b987 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysRolePermissionMapper.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysRolePermissionMapper.java @@ -18,17 +18,14 @@ public interface SysRolePermissionMapper extends BaseMapper { " sys_role_permission t1", " INNER JOIN sys_permission t2 ON t1.permission_id = t2.id ", " WHERE 1=1 ", - " ", - " AND t2.module_id = #{moduleId} ", + " ", + " AND t2.menu_id = #{menuId} ", " ", " ", " AND t1.role_id = #{roleId} ", " ", - " ", - " AND t2.type = #{type} ", - " ", ""}) - List listPermissionIds(Long moduleId, Long roleId, Integer type); + List listPermissionIds(Long moduleId, Long roleId); } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysUserMapper.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysUserMapper.java index f39e5a29e..272d3910e 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysUserMapper.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysUserMapper.java @@ -41,4 +41,5 @@ public interface SysUserMapper extends BaseMapper { }) List list(Page page, SysUser user); + SysUser getByUsername(String username); } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysPermissionService.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysPermissionService.java index 7118189e5..b4c17cacc 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysPermissionService.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysPermissionService.java @@ -9,12 +9,11 @@ import java.util.List; public interface ISysPermissionService extends IService { - List listPermissionRoles(); + List listPermRoles(); IPage list(Page page, SysPermission permission); - boolean refreshPermissionRolesCache(); - - List listPermsByRoleIds(List roleIds, Integer type); + boolean refreshPermRolesCache(); + List listPermByRoles(List roles); } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysRolePermissionService.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysRolePermissionService.java index 10935b6e5..ccef2bb22 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysRolePermissionService.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysRolePermissionService.java @@ -9,8 +9,8 @@ import java.util.List; public interface ISysRolePermissionService extends IService { - List listPermissionIds(Long moduleId,Long roleId, Integer type); - List listPermissionIds(Long roleId, Integer type); + List listPermissionIds(Long moduleId,Long roleId); + List listPermissionIds(Long roleId); boolean update(RolePermissionDTO rolePermission); diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysUserService.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysUserService.java index 516b32632..f520b69f7 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysUserService.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysUserService.java @@ -14,4 +14,5 @@ public interface ISysUserService extends IService { boolean updateUser(SysUser user); + SysUser getByUsername(String username); } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysPermissionServiceImpl.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysPermissionServiceImpl.java index 8a9bf524b..79adb723c 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysPermissionServiceImpl.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysPermissionServiceImpl.java @@ -1,5 +1,4 @@ package com.youlai.admin.service.impl; - import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.metadata.IPage; @@ -13,19 +12,17 @@ import com.youlai.common.constant.AuthConstants; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; - import java.util.*; import java.util.stream.Collectors; @Service public class SysPermissionServiceImpl extends ServiceImpl implements ISysPermissionService { - @Autowired private RedisTemplate redisTemplate; @Override - public List listPermissionRoles() { + public List listPermRoles() { return this.baseMapper.listPermRoles(); } @@ -37,77 +34,45 @@ public class SysPermissionServiceImpl extends ServiceImpl permissions = this.listPermissionRoles(); - Map> permissionRoles = new TreeMap<>(); - Optional.ofNullable(permissions).orElse(new ArrayList<>()).forEach(permission -> { - // 转换 roleId -> ROLE_{roleId} - List roles = Optional.ofNullable(permission.getRoles()) - .orElse(new ArrayList<>()) - .stream() - .map(roleId -> AuthConstants.AUTHORITY_PREFIX + roleId) + List permissions = this.listPermRoles(); + if (CollectionUtil.isNotEmpty(permissions)) { + // 初始化URL【权限->角色(集合)】规则 + List urlPermList = permissions.stream() + .filter(item -> StrUtil.isNotBlank(item.getUrlPerm())) .collect(Collectors.toList()); - - if (CollectionUtil.isNotEmpty(roles)&& StrUtil.isNotBlank(permission.getUrlPerm())) { - permissionRoles.put(permission.getUrlPerm(), roles); + if (CollectionUtil.isNotEmpty(urlPermList)) { + Map> urlPermRoles = new HashMap<>(); + urlPermList.stream().forEach(item -> { + String perm = item.getUrlPerm(); + List roles = item.getRoles(); + roles.add(SystemConstants.ROOT_ROLE_CODE); + urlPermRoles.put(perm, roles); + }); + redisTemplate.opsForHash().putAll(SystemConstants.URL_PERM_ROLES_KEY, urlPermRoles); + } + // 初始化URL【按钮->角色(集合)】规则 + List btnPermList = permissions.stream() + .filter(item -> StrUtil.isNotBlank(item.getUrlPerm())) + .collect(Collectors.toList()); + if (CollectionUtil.isNotEmpty(btnPermList)) { + Map> btnPermRoles = new HashMap<>(); + btnPermList.stream().forEach(item -> { + String perm = item.getUrlPerm(); + List roles = item.getRoles(); + roles.add(SystemConstants.ROOT_ROLE_CODE); + btnPermRoles.put(perm, roles); + }); + redisTemplate.opsForHash().putAll(SystemConstants.BTN_PERM_ROLES_KEY, btnPermRoles); } - redisTemplate.opsForHash().putAll(AuthConstants.PERMISSION_ROLES_KEY, permissionRoles); - }); - return true; - } - - /* private boolean initUrlPermissionRoles() { - redisTemplate.delete(SystemConstants.URL_PERM_ROLES_PREFIX + "*"); - List permissions = this.listPermissionRoles(1); - Map> map = permissions.stream().collect(Collectors.groupingBy(SysPermission::getTenantId)); - Iterator>> iterator = map.entrySet().iterator(); - while (iterator.hasNext()) { - Map.Entry> entry = iterator.next(); - Integer tenantId = entry.getKey(); - List perms = entry.getValue(); - Map> permRoles = new TreeMap<>(); - Optional.ofNullable(perms).orElse(new ArrayList<>()).forEach(perm -> { - // 转换 roleId -> ROLE_{角色编码} - List roles = Optional.ofNullable(perm.getRoles()) - .orElse(new ArrayList<>()) - .stream() - .map(role -> AuthConstants.AUTHORITY_PREFIX + role) - .collect(Collectors.toList()); - permRoles.put(perm.getMethod() + "_" + perm.getPerm(), roles); - redisTemplate.opsForHash().putAll(AuthConstants.URL_PERM_ROLES_PREFIX + tenantId, permRoles); - }); } return true; } - private boolean initBtnPermissionRoles() { - redisTemplate.delete(AuthConstants.BTN_PERM_ROLES_PREFIX + "*"); - List permissions = this.listPermissionRoles(2); - Map> map = permissions.stream().collect(Collectors.groupingBy(SysPermission::getTenantId)); - Iterator>> iterator = map.entrySet().iterator(); - while (iterator.hasNext()) { - Map.Entry> entry = iterator.next(); - Integer tenantId = entry.getKey(); - List perms = entry.getValue(); - Map> permRoles = new TreeMap<>(); - Optional.ofNullable(perms).orElse(new ArrayList<>()).forEach(perm -> { - // 转换 roleId -> ROLE_{角色编码} - List roles = Optional.ofNullable(perm.getRoles()) - .orElse(new ArrayList<>()) - .stream() - .collect(Collectors.toList()); - permRoles.put(perm.getPerm(), roles); - redisTemplate.opsForHash().putAll(AuthConstants.BTN_PERM_ROLES_PREFIX + tenantId, permRoles); - }); - } - return true; - }*/ - - @Override - public List listPermsByRoleIds(List roleIds, Integer type) { - return this.baseMapper.listPermsByRoleIds(roleIds, type); + public List listPermByRoles(List roles) { + return this.baseMapper.listPermByRoles(roles); } } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysRolePermissionServiceImpl.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysRolePermissionServiceImpl.java index 25d06c5b3..d926ae364 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysRolePermissionServiceImpl.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysRolePermissionServiceImpl.java @@ -18,13 +18,13 @@ public class SysRolePermissionServiceImpl extends ServiceImpl listPermissionIds(Long roleId, Integer type) { - return this.baseMapper.listPermissionIds(null, roleId, type); + public List listPermissionIds(Long roleId) { + return this.baseMapper.listPermissionIds(null, roleId); } @Override - public List listPermissionIds(Long moduleId, Long roleId, Integer type) { - return this.baseMapper.listPermissionIds(moduleId, roleId, type); + public List listPermissionIds(Long moduleId, Long roleId) { + return this.baseMapper.listPermissionIds(moduleId, roleId); } @Override @@ -34,7 +34,7 @@ public class SysRolePermissionServiceImpl extends ServiceImpl dbPermissionIds = this.baseMapper.listPermissionIds(moduleId, roleId, type); + List dbPermissionIds = this.baseMapper.listPermissionIds(moduleId, roleId); // 删除数据库存在此次提交不存在的 if (CollectionUtil.isNotEmpty(dbPermissionIds)) { diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysUserServiceImpl.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysUserServiceImpl.java index 7476475c9..1ac3cbb67 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysUserServiceImpl.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysUserServiceImpl.java @@ -78,5 +78,10 @@ public class SysUserServiceImpl extends ServiceImpl impl return result; } + @Override + public SysUser getByUsername(String username) { + return this.baseMapper.getByUsername(username); + } + } diff --git a/youlai-admin/admin-boot/src/main/resources/mapper/SysPermissionMapper.xml b/youlai-admin/admin-boot/src/main/resources/mapper/SysPermissionMapper.xml index f625a696d..6b52a7714 100644 --- a/youlai-admin/admin-boot/src/main/resources/mapper/SysPermissionMapper.xml +++ b/youlai-admin/admin-boot/src/main/resources/mapper/SysPermissionMapper.xml @@ -12,10 +12,8 @@ - - - - + + @@ -26,7 +24,7 @@ update_time - select t1.id, t1.name, t1.menu_id, t1.url_perm, t1.btn_perm, t3.code from sys_permission t1, sys_role_permission t2, @@ -34,6 +32,15 @@ where t1.id = t2.permission_id and t2.role_id = t3.id - - + diff --git a/youlai-admin/admin-boot/src/main/resources/mapper/SysUserMapper.xml b/youlai-admin/admin-boot/src/main/resources/mapper/SysUserMapper.xml new file mode 100644 index 000000000..10b0eb5f3 --- /dev/null +++ b/youlai-admin/admin-boot/src/main/resources/mapper/SysUserMapper.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + id + ,username,nickname, + gender,password,dept_id, + avatar,mobile,status, + email,create_time,update_time, + deleted + + + diff --git a/youlai-admin/admin-boot/src/test/java/com/youlai/admin/service/impl/SysPermissionServiceImplTest.java b/youlai-admin/admin-boot/src/test/java/com/youlai/admin/service/impl/SysPermissionServiceImplTest.java index 48e4e4ccf..647e6f5bb 100644 --- a/youlai-admin/admin-boot/src/test/java/com/youlai/admin/service/impl/SysPermissionServiceImplTest.java +++ b/youlai-admin/admin-boot/src/test/java/com/youlai/admin/service/impl/SysPermissionServiceImplTest.java @@ -2,6 +2,7 @@ package com.youlai.admin.service.impl; import com.youlai.admin.mapper.SysPermissionMapper; import com.youlai.admin.pojo.entity.SysPermission; +import com.youlai.admin.service.ISysPermissionService; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -23,9 +24,16 @@ class SysPermissionServiceImplTest { @Autowired SysPermissionMapper sysPermissionMapper; + ISysPermissionService iSysPermissionService; + @Test void listPermissionRoles() { List sysPermissions = sysPermissionMapper.listPermRoles(); log.info(sysPermissions.toString()); } + + @Test + void refreshPermRolesCache(){ + + } } diff --git a/youlai-auth/pom.xml b/youlai-auth/pom.xml index e95f362f5..5183a9211 100644 --- a/youlai-auth/pom.xml +++ b/youlai-auth/pom.xml @@ -125,6 +125,12 @@ org.springframework.boot spring-boot-starter-actuator + + + com.youlai + common-log + ${youlai.version} + diff --git a/youlai-auth/src/main/java/com/youlai/auth/config/oauth2/WebSecurityConfig.java b/youlai-auth/src/main/java/com/youlai/auth/config/oauth2/WebSecurityConfig.java index c58f294f6..679296992 100644 --- a/youlai-auth/src/main/java/com/youlai/auth/config/oauth2/WebSecurityConfig.java +++ b/youlai-auth/src/main/java/com/youlai/auth/config/oauth2/WebSecurityConfig.java @@ -21,7 +21,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { http .authorizeRequests().requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll() .and() - .authorizeRequests().antMatchers("/getPublicKey","/oauth/logout").permitAll() + .authorizeRequests().antMatchers("/oauth/public-key","/oauth/logout").permitAll() // @link https://gitee.com/xiaoym/knife4j/issues/I1Q5X6 (Security放行url) .antMatchers("/webjars/**","/doc.html","/swagger-resources/**","/v2/api-docs").permitAll() diff --git a/youlai-auth/src/main/java/com/youlai/auth/controller/LogoutController.java b/youlai-auth/src/main/java/com/youlai/auth/controller/LogoutController.java deleted file mode 100644 index c8f76f1c8..000000000 --- a/youlai-auth/src/main/java/com/youlai/auth/controller/LogoutController.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.youlai.auth.controller; - -import cn.hutool.json.JSONObject; -import com.youlai.common.constant.AuthConstants; -import com.youlai.common.result.Result; -import com.youlai.common.web.util.JwtUtils; -import io.swagger.annotations.Api; -import lombok.AllArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import java.util.concurrent.TimeUnit; - - -@Api(tags = "注销") -@RestController -@RequestMapping("/oauth") -@AllArgsConstructor -@Slf4j -public class LogoutController { - - private RedisTemplate redisTemplate; - - @DeleteMapping("/logout") - public Result logout() { - JSONObject jsonObject = JwtUtils.getJwtPayload(); - String jti = jsonObject.getStr(AuthConstants.JWT_JTI); // JWT唯一标识 - Long exp = jsonObject.getLong(AuthConstants.JWT_EXP); // JWT过期时间戳 - if (exp != null) { - long currentTimeSeconds = System.currentTimeMillis() / 1000; - if (exp < currentTimeSeconds) { // token已过期,无需加入黑名单 - return Result.success(); - } - redisTemplate.opsForValue().set(AuthConstants.TOKEN_BLACKLIST_PREFIX + jti, null, (exp - currentTimeSeconds), TimeUnit.SECONDS); - } else { // token 永不过期则永久加入黑名单 - redisTemplate.opsForValue().set(AuthConstants.TOKEN_BLACKLIST_PREFIX + jti, null); - } - return Result.success(); - } - -} diff --git a/youlai-auth/src/main/java/com/youlai/auth/controller/OAuthController.java b/youlai-auth/src/main/java/com/youlai/auth/controller/OAuthController.java index 3efee353c..7e3b38ebb 100644 --- a/youlai-auth/src/main/java/com/youlai/auth/controller/OAuthController.java +++ b/youlai-auth/src/main/java/com/youlai/auth/controller/OAuthController.java @@ -1,7 +1,12 @@ package com.youlai.auth.controller; +import cn.hutool.json.JSONObject; +import com.nimbusds.jose.jwk.JWKSet; +import com.nimbusds.jose.jwk.RSAKey; +import com.youlai.auth.enums.OAuthClientEnum; import com.youlai.auth.service.WeAppService; import com.youlai.common.constant.AuthConstants; +import com.youlai.common.result.Result; import com.youlai.common.web.util.JwtUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; @@ -9,17 +14,18 @@ import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint; import org.springframework.web.HttpRequestMethodNotSupportedException; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import springfox.documentation.annotations.ApiIgnore; +import java.security.KeyPair; import java.security.Principal; +import java.security.interfaces.RSAPublicKey; import java.util.Map; +import java.util.concurrent.TimeUnit; @Api(tags = "认证中心") @RestController @@ -30,7 +36,8 @@ public class OAuthController { private TokenEndpoint tokenEndpoint; private WeAppService weAppService; - + private RedisTemplate redisTemplate; + private KeyPair keyPair; @ApiOperation(value = "OAuth2认证", notes = "login") @ApiImplicitParams({ @@ -42,7 +49,7 @@ public class OAuthController { @ApiImplicitParam(name = "password", defaultValue = "123456", value = "登录密码"), }) @PostMapping("/token") - public OAuth2AccessToken postAccessToken( + public Object postAccessToken( @ApiIgnore Principal principal, @ApiIgnore @RequestParam Map parameters ) throws HttpRequestMethodNotSupportedException { @@ -56,16 +63,44 @@ public class OAuthController { * 方式二:放在请求头(Request Headers)中的Authorization字段,且经过加密,例如 Basic Y2xpZW50OnNlY3JldA== 明文等于 client:secret */ String clientId = JwtUtils.getAuthClientId(); - switch (clientId) { - case AuthConstants.WEAPP_CLIENT_ID: // 微信认证 + OAuthClientEnum client = OAuthClientEnum.getByClientId(clientId); + + switch (client) { + case WEAPP: // 微信小程序 oAuth2AccessToken = weAppService.login(principal, parameters); break; + case CLIENT: // knife4j接口测试文档使用 client_id/client_secret : client/123456 + return tokenEndpoint.postAccessToken(principal, parameters).getBody(); default: oAuth2AccessToken = tokenEndpoint.postAccessToken(principal, parameters).getBody(); break; } - return oAuth2AccessToken; + return Result.success(oAuth2AccessToken); } + @ApiOperation(value = "注销", notes = "logout") + @DeleteMapping("/logout") + public Result logout() { + JSONObject jsonObject = JwtUtils.getJwtPayload(); + String jti = jsonObject.getStr(AuthConstants.JWT_JTI); // JWT唯一标识 + Long exp = jsonObject.getLong(AuthConstants.JWT_EXP); // JWT过期时间戳 + if (exp != null) { + long currentTimeSeconds = System.currentTimeMillis() / 1000; + if (exp < currentTimeSeconds) { // token已过期,无需加入黑名单 + return Result.success(); + } + redisTemplate.opsForValue().set(AuthConstants.TOKEN_BLACKLIST_PREFIX + jti, null, (exp - currentTimeSeconds), TimeUnit.SECONDS); + } else { // token 永不过期则永久加入黑名单 + redisTemplate.opsForValue().set(AuthConstants.TOKEN_BLACKLIST_PREFIX + jti, null); + } + return Result.success(); + } + @ApiOperation(value = "获取公钥", notes = "login") + @GetMapping("/public-key") + public Map getPublicKey() { + RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); + RSAKey key = new RSAKey.Builder(publicKey).build(); + return new JWKSet(key).toJSONObject(); + } } diff --git a/youlai-auth/src/main/java/com/youlai/auth/controller/PublicKeyController.java b/youlai-auth/src/main/java/com/youlai/auth/controller/PublicKeyController.java deleted file mode 100644 index 3fab860d6..000000000 --- a/youlai-auth/src/main/java/com/youlai/auth/controller/PublicKeyController.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.youlai.auth.controller; - -import com.nimbusds.jose.jwk.JWKSet; -import com.nimbusds.jose.jwk.RSAKey; -import io.swagger.annotations.Api; -import lombok.AllArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import java.security.KeyPair; -import java.security.interfaces.RSAPublicKey; -import java.util.Map; - -@Api(tags = "获取公钥接口") -@RestController -@RequestMapping -@AllArgsConstructor -@Slf4j -public class PublicKeyController { - - private KeyPair keyPair; - - @GetMapping("/getPublicKey") - public Map loadPublicKey() { - RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); - RSAKey key = new RSAKey.Builder(publicKey).build(); - return new JWKSet(key).toJSONObject(); - } - -} diff --git a/youlai-auth/src/main/java/com/youlai/auth/domain/OAuthUserDetails.java b/youlai-auth/src/main/java/com/youlai/auth/domain/OAuthUserDetails.java index 4b2d4bdb4..119f4d529 100644 --- a/youlai-auth/src/main/java/com/youlai/auth/domain/OAuthUserDetails.java +++ b/youlai-auth/src/main/java/com/youlai/auth/domain/OAuthUserDetails.java @@ -1,7 +1,7 @@ package com.youlai.auth.domain; import cn.hutool.core.collection.CollectionUtil; -import com.youlai.admin.pojo.dto.UserDTO; +import com.youlai.admin.pojo.entity.SysUser; import com.youlai.common.constant.AuthConstants; import com.youlai.mall.ums.pojo.dto.AuthMemberDTO; import lombok.Data; @@ -33,14 +33,14 @@ public class OAuthUserDetails implements UserDetails { private Collection authorities; - public OAuthUserDetails(UserDTO user) { + public OAuthUserDetails(SysUser user) { this.setId(user.getId()); this.setUsername(user.getUsername()); this.setPassword(AuthConstants.BCRYPT + user.getPassword()); this.setEnabled(Integer.valueOf(1).equals(user.getStatus())); - if (CollectionUtil.isNotEmpty(user.getRoleIds())) { + if (CollectionUtil.isNotEmpty(user.getRoles())) { authorities = new ArrayList<>(); - user.getRoleIds().forEach(roleId -> authorities.add(new SimpleGrantedAuthority(String.valueOf(roleId)))); + user.getRoles().forEach(role -> authorities.add(new SimpleGrantedAuthority(role))); } } diff --git a/youlai-auth/src/main/java/com/youlai/auth/enums/OAuthClientEnum.java b/youlai-auth/src/main/java/com/youlai/auth/enums/OAuthClientEnum.java new file mode 100644 index 000000000..f4058a5b5 --- /dev/null +++ b/youlai-auth/src/main/java/com/youlai/auth/enums/OAuthClientEnum.java @@ -0,0 +1,38 @@ +package com.youlai.auth.enums; +import lombok.Getter; + + +/** + * @author haoxr + * @description TODO + * @createTime 2021/5/31 23:55 + */ +public enum OAuthClientEnum { + + CLIENT("client", "测试客户端"), + ADMIN("youlai-admin", "系统管理端"), + WEAPP("youlai-mall-weapp", "微信小程序端"); + + + @Getter + private String clientId; + + @Getter + private String desc; + + OAuthClientEnum(String clientId,String desc){ + this.clientId=clientId; + this.desc=desc; + } + + public static OAuthClientEnum getByClientId(String clientId) { + for (OAuthClientEnum client : OAuthClientEnum.values()) { + if(client.getClientId().equals(clientId)){ + return client; + } + } + return null; + } + + +} diff --git a/youlai-auth/src/main/java/com/youlai/auth/service/CustomUserDetailsService.java b/youlai-auth/src/main/java/com/youlai/auth/service/CustomUserDetailsService.java index 1c5642eec..86b3fc05f 100644 --- a/youlai-auth/src/main/java/com/youlai/auth/service/CustomUserDetailsService.java +++ b/youlai-auth/src/main/java/com/youlai/auth/service/CustomUserDetailsService.java @@ -2,7 +2,9 @@ package com.youlai.auth.service; import com.youlai.admin.api.UserFeignClient; import com.youlai.admin.pojo.dto.UserDTO; +import com.youlai.admin.pojo.entity.SysUser; import com.youlai.auth.domain.OAuthUserDetails; +import com.youlai.auth.enums.OAuthClientEnum; import com.youlai.common.constant.AuthConstants; import com.youlai.common.result.Result; import com.youlai.common.result.ResultCode; @@ -33,36 +35,36 @@ public class CustomUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { String clientId = JwtUtils.getAuthClientId(); + OAuthClientEnum client = OAuthClientEnum.getByClientId(clientId); - OAuthUserDetails OAuthUserDetails = null; Result result; - switch (clientId) { - case AuthConstants.ADMIN_CLIENT_ID: // 后台用户 - result = userFeignClient.getUserByUsername(username); - log.info("获取用户信息:{}", result.toString()); - if (ResultCode.SUCCESS.getCode().equals(result.getCode())) { - UserDTO userDTO = (UserDTO) result.getData(); - OAuthUserDetails = new OAuthUserDetails(userDTO); - } - break; - case AuthConstants.WEAPP_CLIENT_ID: // 小程序会员 + OAuthUserDetails oauthUser = null; + switch (client) { + case WEAPP: // 小程序会员 result = memberFeignClient.getUserByOpenid(username); if (ResultCode.SUCCESS.getCode().equals(result.getCode())) { AuthMemberDTO authMemberDTO = (AuthMemberDTO) result.getData(); - OAuthUserDetails = new OAuthUserDetails(authMemberDTO); + oauthUser = new OAuthUserDetails(authMemberDTO); + } + break; + default: + result = userFeignClient.getUserByUsername(username); + if (ResultCode.SUCCESS.getCode().equals(result.getCode())) { + SysUser sysUser = (SysUser)result.getData(); + oauthUser = new OAuthUserDetails(sysUser); } break; } - if (OAuthUserDetails == null || OAuthUserDetails.getId() == null) { + if (oauthUser == null || oauthUser.getId() == null) { throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg()); - } else if (!OAuthUserDetails.isEnabled()) { + } else if (!oauthUser.isEnabled()) { throw new DisabledException("该账户已被禁用!"); - } else if (!OAuthUserDetails.isAccountNonLocked()) { + } else if (!oauthUser.isAccountNonLocked()) { throw new LockedException("该账号已被锁定!"); - } else if (!OAuthUserDetails.isAccountNonExpired()) { + } else if (!oauthUser.isAccountNonExpired()) { throw new AccountExpiredException("该账号已过期!"); } - return OAuthUserDetails; + return oauthUser; } } diff --git a/youlai-common/common-core/src/main/java/com/youlai/common/constant/AuthConstants.java b/youlai-common/common-core/src/main/java/com/youlai/common/constant/AuthConstants.java index 38b127cb6..f324f0f97 100644 --- a/youlai-common/common-core/src/main/java/com/youlai/common/constant/AuthConstants.java +++ b/youlai-common/common-core/src/main/java/com/youlai/common/constant/AuthConstants.java @@ -48,7 +48,7 @@ public interface AuthConstants { + "authorized_grant_types, web_server_redirect_uri, authorities, access_token_validity, " + "refresh_token_validity, additional_information, autoapprove"; - String BASE_CLIENT_DETAILS_SQL = "select " + CLIENT_DETAILS_FIELDS + " from oauth_client_details"; + String BASE_CLIENT_DETAILS_SQL = "select " + CLIENT_DETAILS_FIELDS + " from sys_oauth_client"; String FIND_CLIENT_DETAILS_SQL = BASE_CLIENT_DETAILS_SQL + " order by client_id"; diff --git a/youlai-common/common-core/src/main/java/com/youlai/common/constant/GlobalConstants.java b/youlai-common/common-core/src/main/java/com/youlai/common/constant/GlobalConstants.java index 3776efaf5..72fb72a11 100644 --- a/youlai-common/common-core/src/main/java/com/youlai/common/constant/GlobalConstants.java +++ b/youlai-common/common-core/src/main/java/com/youlai/common/constant/GlobalConstants.java @@ -9,4 +9,10 @@ public interface GlobalConstants { Integer DELETED_VALUE = 1; + String ROOT_ROLE_CODE = "ROOT"; + + + String URL_PERM_ROLES_PREFIX = "system:permission:url_perm_roles:"; + String BTN_PERM_ROLES_PREFIX = "system:permission:btn_perm_roles:"; + } diff --git a/youlai-common/common-core/src/main/java/com/youlai/common/enums/QueryModeEnum.java b/youlai-common/common-core/src/main/java/com/youlai/common/enums/QueryModeEnum.java index d16c72b76..08280b72d 100644 --- a/youlai-common/common-core/src/main/java/com/youlai/common/enums/QueryModeEnum.java +++ b/youlai-common/common-core/src/main/java/com/youlai/common/enums/QueryModeEnum.java @@ -19,7 +19,7 @@ public enum QueryModeEnum { this.code=code; } - public static QueryModeEnum getValue(String code){ + public static QueryModeEnum getByCode(String code){ for (QueryModeEnum value : values()) { if (value.getCode().equals(code)) { return value; diff --git a/youlai-common/common-log/src/main/resources/logback-spring.xml b/youlai-common/common-log/src/main/resources/logback-spring.xml index 9f9a2dd04..f726d22f8 100644 --- a/youlai-common/common-log/src/main/resources/logback-spring.xml +++ b/youlai-common/common-log/src/main/resources/logback-spring.xml @@ -24,6 +24,11 @@ + + + + + diff --git a/youlai-common/common-web/src/main/java/com/youlai/common/web/util/JwtUtils.java b/youlai-common/common-web/src/main/java/com/youlai/common/web/util/JwtUtils.java index e7d0e8e91..766beb747 100644 --- a/youlai-common/common-web/src/main/java/com/youlai/common/web/util/JwtUtils.java +++ b/youlai-common/common-web/src/main/java/com/youlai/common/web/util/JwtUtils.java @@ -67,10 +67,15 @@ public class JwtUtils { return clientId; } - - public static List getRoleIds() { - List list = getJwtPayload().get(AuthConstants.JWT_AUTHORITIES_KEY, List.class); - List authorities = list.stream().map(Long::valueOf).collect(Collectors.toList()); - return authorities; + public static List getRoles() { + JSONObject payload = getJwtPayload(); + if (payload != null && payload.size() > 0) { + List list = payload.get(AuthConstants.JWT_AUTHORITIES_KEY, List.class); + List roles = list.stream().collect(Collectors.toList()); + return roles; + } + return null; } + + } diff --git a/youlai-gateway/pom.xml b/youlai-gateway/pom.xml index 5fce8ff5e..bc9630c0e 100644 --- a/youlai-gateway/pom.xml +++ b/youlai-gateway/pom.xml @@ -98,6 +98,12 @@ com.youlai common-redis + + + com.youlai + common-log + ${youlai.version} +