您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關spring-boot-plusV1.4.0發布集成用戶角色權限部門管理的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
RBAC用戶角色權限
用戶角色權限部門管理核心接口介紹
Shiro權限配置
? Shiro權限配置
數據庫模型圖
? spring-boot-plus初始化SQL下載
獲取驗證碼
可配置是否啟用驗證碼
默認未啟用
如已啟用驗證碼校驗,登陸時,需傳入verifyToken和code
驗證碼演示
spring-boot-plus: # 是否啟用ansi控制臺輸出有顏色的字體 enable-ansi: true # 是否啟用驗證碼 enable-verify-code: true
enable-verify-code
設置為 true
啟用驗證碼驗證
兩種方式獲取驗證碼
驗證碼后臺保存在Redis中,過期時間默認為5分鐘
方式一:
輸出圖片流到瀏覽器,驗證碼token輸出到響應頭
http://localhost:8888/verificationCode/getImage
Response Headers HTTP/1.1 200 verifyToken: 6515b4b798ce49e68b1e40f98ff8eb19
方式二:
獲取Base64編碼圖片和驗證碼token
http://localhost:8888/verificationCode/getBase64Image
{ "code": 200, "msg": "操作成功", "success": true, "data": { "image": "data:image/png;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAmAG4DASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh5iJipKTlJWWl5iZmqKjpKWmp6ip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5xuUh+f1P9K4T4Zwy2/hV5WjVfNuXlRz82VwqcjkY3AjkZru0hmGJ4mlWNlADR/vGP1xj6fgKme+plim/bST1LNzoWmyTNO9lbF5HBYm3jJJJ5OSvXJzVJvDulC7jhezg2kYBWFB6kZ+X0Vs/h+BRWalLucd2aEtkbc+akjGNEO7kBhj04+vp9ea4bUvErPb3lg0G9vMdFlZhwM+mMg4HZutFFcGPqzp004u1wk3y3NbTrN7Pw/bl5MuyLIoX7uHywzx1wDx+tSq/mAgADOAcgdevp7UUV30FahH0X5I1T90kV4nUIAzcZJYAYPtioI9J021m+2Cwtmn3M5k8oK5z1+YeuTzj+dFFaqTWzM572LJVG80l5VY84yH3fU8dx/nFNUvChZZSrj7u1R+PPUdP85oopN2dhqTuQMElEwnJIkVlYY3B8jkMD1BBOetYUPgrw2siMumLvVsjMrspxzyCxBFFFXfRm7nKmnyNo3lAtQqW6LDGgGxI+AmOw9qlgnKE7WkRj/EjY/z2ooqVsczk7XP/9k=", "verifyToken": "42ba8abde7bc47b2b1397b4d6676956a" }, "time": "2019-11-01 22:40:37" }
系統用戶登陸
POST請求,Content-Type: application/json
http://127.0.0.1:8888/login
請求參數
{ "code": "驗證碼", "password": "123456", "username": "admin", "verifyToken": "驗證碼token" }
注意
如果沒有啟用驗證碼登陸,則只需傳入username
和password
前端應將密碼加密后進行傳輸
登陸成功
返回登陸用戶信息:部門/角色/權限
返回用戶token
{ "code": 200, "msg": "登陸成功", "success": true, "data": { "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJ3ZWIiLCJpc3MiOiJzcHJpbmctYm9vdC1wbHVzIiwiZXhwIjoxNTcyNjIzMDE5LCJpYXQiOjE1NzI2MTk0MTksImp0aSI6IjdlZmVlM2UwMjc2MTRiYTc5M2I2YmYwZmE4NTgzYmUwIiwidXNlcm5hbWUiOiJhZG1pbiJ9.O3w7CNRqw_Miwp8MDzPND6w490c9Q7yFlKpFJK9ubSU", "loginSysUserVo": { "id": "1", "username": "admin", "nickname": "管理員", "gender": 1, "state": 1, "departmentId": "1", "departmentName": "管理部", "roleId": "1", "roleName": "管理員", "roleCode": "admin", "permissionCodes": [ "sys:permission:codes", "system:management", "sys:department:update", "sys:department:page", "sys:role:management", "sys:permission:add", "sys:user:add", "sys:role:page", "sys:permission:page", "sys:department:delete", "sys:permission:management", "sys:user:delete", "sys:department:management", "sys:user:page", "sys:user:update", "sys:user:update:password", "sys:user:update:head", "sys:role:add", "sys:permission:menu:tree", "sys:department:info", "sys:permission:all:menu:list", "sys:permission:info", "sys:role:info", "sys:permission:all:menu:tree", "sys:permission:update", "sys:permission:menu:list", "sys:role:update", "sys:user:info", "sys:user:management", "sys:role:delete", "sys:permission:delete" ] } }, "time": "2019-11-01 22:43:39" }
token默認失效時間為1個小時
設置JWT Token失效時間
############################ JWT start ############################# jwt: # 默認過期時間1小時,單位:秒 expire-second: 3600
后臺使用Redis緩存登陸用戶信息
redis key
login:user:admin
其它需要授權訪問的接口,請求頭需攜帶token
部門樹形列表
部門可以設置為N級,后臺使用遞歸將部門列表轉換為樹形列表
SysDepartmentServiceImpl
@Override public List<SysDepartmentTreeVo> getAllDepartmentTree() { List<SysDepartment> sysDepartmentList = getAllDepartmentList(); if (CollectionUtils.isEmpty(sysDepartmentList)) { throw new IllegalArgumentException("SysDepartment列表不能為空"); } List<SysDepartmentTreeVo> list = SysDepartmentConvert.INSTANCE.listToTreeVoList(sysDepartmentList); List<SysDepartmentTreeVo> treeVos = new ArrayList<>(); for (SysDepartmentTreeVo treeVo : list) { if (treeVo.getParentId() == null) { treeVos.add(findChildren(treeVo, list)); } } return treeVos; } /** * 遞歸獲取樹形結果列表 * * @param tree * @param list * @return */ public SysDepartmentTreeVo findChildren(SysDepartmentTreeVo tree, List<SysDepartmentTreeVo> list) { for (SysDepartmentTreeVo vo : list) { if (tree.getId().equals(vo.getParentId())) { if (tree.getChildren() == null) { tree.setChildren(new ArrayList<>()); } tree.getChildren().add(findChildren(vo, list)); } } return tree; }
前端JSON結構
http://127.0.0.1:8888/sysDepartment/getAllDepartmentTree
角色管理
設置角色權限
核心代碼,刪除角色權限,新增角色權限
求集合的差集
SysRolePermissionServiceImpl
@Transactional(rollbackFor = Exception.class) @Override public boolean updateSysRole(UpdateSysRoleParam updateSysRoleParam) throws Exception { Long roleId = updateSysRoleParam.getId(); List<Long> permissionIds = updateSysRoleParam.getPermissionIds(); // 校驗角色是否存在 SysRole sysRole = getById(roleId); if (sysRole == null) { throw new BusinessException("該角色不存在"); } // 校驗權限列表是否存在 if (!sysPermissionService.isExistsByPermissionIds(permissionIds)) { throw new BusinessException("權限列表id匹配失敗"); } // 修改角色 sysRole.setName(updateSysRoleParam.getName()) .setType(updateSysRoleParam.getType()) .setRemark(updateSysRoleParam.getRemark()) .setState(updateSysRoleParam.getState()) .setUpdateTime(new Date()); boolean updateResult = updateById(sysRole); if (!updateResult) { throw new DaoException("修改系統角色失敗"); } // 獲取之前的權限id集合 List<Long> beforeList = sysRolePermissionService.getPermissionIdsByRoleId(roleId); // 差集計算 // before:1,2,3,4,5,6 // after: 1,2,3,4,7,8 // 刪除5,6 新增7,8 // 此處真實刪除,去掉deleted字段的@TableLogic注解 Set<Long> beforeSet = new HashSet<>(beforeList); Set<Long> afterSet = new HashSet<>(permissionIds); SetUtils.SetView deleteSet = SetUtils.difference(beforeSet, afterSet); SetUtils.SetView addSet = SetUtils.difference(afterSet, beforeSet); log.debug("deleteSet = " + deleteSet); log.debug("addSet = " + addSet); // 刪除權限關聯 UpdateWrapper updateWrapper = new UpdateWrapper(); updateWrapper.eq("role_id",roleId); updateWrapper.in("permission_id",deleteSet); boolean deleteResult = sysRolePermissionService.remove(updateWrapper); if (!deleteResult) { throw new DaoException("刪除角色權限關系失敗"); } // 新增權限關聯 boolean addResult = sysRolePermissionService.saveSysRolePermissionBatch(roleId, addSet); if (!addResult) { throw new DaoException("新增角色權限關系失敗"); } return true; }
權限管理
權限樹形列表
用戶設置角色權限時,選擇
權限菜單權限分為菜單和功能權限
后臺獲取三層權限樹
@Override public List<SysPermissionTreeVo> getAllMenuTree() throws Exception { List<SysPermission> list = getAllMenuList(); // 轉換成樹形菜單 List<SysPermissionTreeVo> treeVos = convertSysPermissionTreeVoList(list); return treeVos; } @Override public List<SysPermissionTreeVo> convertSysPermissionTreeVoList(List<SysPermission> list) { if (CollectionUtils.isEmpty(list)) { throw new IllegalArgumentException("SysPermission列表不能為空"); } // 按level分組獲取map Map<Integer, List<SysPermission>> map = list.stream().collect(Collectors.groupingBy(SysPermission::getLevel)); List<SysPermissionTreeVo> treeVos = new ArrayList<>(); // 循環獲取三級菜單樹形集合 for (SysPermission one : map.get(LevelEnum.ONE.getKey())) { SysPermissionTreeVo oneVo = SysPermissionConvert.INSTANCE.permissionToTreeVo(one); Long oneParentId = oneVo.getParentId(); if (oneParentId == null || oneParentId == 0) { treeVos.add(oneVo); } List<SysPermission> twoList = map.get(LevelEnum.TWO.getKey()); if (CollectionUtils.isNotEmpty(twoList)) { for (SysPermission two : twoList) { SysPermissionTreeVo twoVo = SysPermissionConvert.INSTANCE.permissionToTreeVo(two); if (two.getParentId().equals(one.getId())) { oneVo.getChildren().add(twoVo); } List<SysPermission> threeList = map.get(LevelEnum.THREE.getKey()); if (CollectionUtils.isNotEmpty(threeList)) { for (SysPermission three : threeList) { if (three.getParentId().equals(two.getId())) { SysPermissionTreeVo threeVo = SysPermissionConvert.INSTANCE.permissionToTreeVo(three); twoVo.getChildren().add(threeVo); } } } } } } return treeVos; }
前端JSON格式
http://127.0.0.1:8888/sysPermission/getAllMenuTree
權限編碼列表
返回當前用戶所有的權限編碼,方便前端展示導航菜單和功能按鈕
http://127.0.0.1:8888/sysPermission/getPermissionCodesByUserId/1
{ "code": 200, "msg": "操作成功", "success": true, "data": [ "system:management", "system:management", "sys:user:management", "sys:user:management", "sys:role:management", "sys:permission:management", "sys:department:management", "sys:user:add", "sys:user:add", "sys:user:update", "sys:user:update", "sys:user:delete", "sys:user:delete", "sys:user:info", "sys:user:info", "sys:user:page", "sys:user:page", "sys:user:update:password", "sys:user:update:head", "sys:role:add", "sys:role:update", "sys:role:delete", "sys:role:info", "sys:role:page", "sys:permission:add", "sys:permission:update", "sys:permission:delete", "sys:permission:info", "sys:permission:page", "sys:permission:all:menu:list", "sys:permission:all:menu:tree", "sys:permission:menu:list", "sys:permission:menu:tree", "sys:permission:codes", "sys:department:update", "sys:department:delete", "sys:department:info", "sys:department:page" ], "time": "2019-11-02 00:32:17" }
注意
使用Shiro注解@RequiresPermissions進行controller方法權限過濾
@RequiresPermissions("sys:department:add")
生成代碼時,可配置生成RequiresPermissions注解
// 是否生成Shiro RequiresPermissions注解 codeGenerator.setRequiresPermissions(true);
生成或新增的controller方法,需要進行權限管理,需要到sys_permission表新增權限編碼記錄,并給相應角色賦予權限
感謝各位的閱讀!關于“spring-boot-plusV1.4.0發布集成用戶角色權限部門管理的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。