Guns权限管理系统实战基于Shiro的数据权限控制终极方案【免费下载链接】gunsGuns基于SpringBoot,致力于做更简洁的后台管理系统,完美整合springmvc shiro 分页插件PageHelper 通用Mapper beetl!Guns项目代码简洁,注释丰富,上手容易,同时Guns包含许多基础模块(用户管理,角色管理,部门管理,字典管理等10个模块),可以直接作为一个后台管理系统的脚手架.项目地址: https://gitcode.com/gh_mirrors/gun/gunsGuns权限管理系统是基于SpringBoot的后台管理系统框架它完美整合了Spring MVC Shiro PageHelper 通用Mapper Beetl等技术栈。对于企业级应用开发而言数据权限控制是权限管理系统的核心功能Guns通过独创的MyBatis数据范围拦截器实现了简洁高效的数据权限控制方案。什么是数据权限控制数据权限控制是指对拥有相同角色的用户根据其所属部门的不同进行相应的数据筛选。例如两个角色都有用户管理权限但下级部门的用户不能看到上级部门的数据。Guns的数据权限控制以部门ID为单位来标识实现了灵活的数据隔离机制。Guns权限管理系统架构解析1. Shiro权限认证框架整合Guns通过ShiroConfig.java完美整合Apache Shiro框架实现了完整的认证和授权体系Bean public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager) { ShiroFilterFactoryBean shiroFilter new ShiroFilterFactoryBean(); shiroFilter.setSecurityManager(securityManager); shiroFilter.setLoginUrl(/login); shiroFilter.setSuccessUrl(/); shiroFilter.setUnauthorizedUrl(/global/error); MapString, String hashMap new LinkedHashMap(); hashMap.put(/static/**, anon); hashMap.put(/login, anon); hashMap.put(/global/sessionError, anon); hashMap.put(/kaptcha, anon); hashMap.put(/**, user); shiroFilter.setFilterChainDefinitionMap(hashMap); return shiroFilter; }2. 独创的数据范围拦截器Guns的核心创新在于DataScopeInterceptor.java这是一个MyBatis拦截器实现了数据权限的自动过滤Intercepts({Signature(type StatementHandler.class, method prepare, args {Connection.class, Integer.class})}) public class DataScopeInterceptor implements Interceptor { public Object intercept(Invocation invocation) throws Throwable { // 查找参数中包含DataScope类型的参数 DataScope dataScope findDataScopeObject(parameterObject); if (dataScope ! null) { String scopeName dataScope.getScopeName(); ListInteger deptIds dataScope.getDeptIds(); String join CollectionKit.join(deptIds, ,); originalSql select * from ( originalSql ) temp_data_scope where temp_data_scope. scopeName in ( join ); metaStatementHandler.setValue(delegate.boundSql.sql, originalSql); } return invocation.proceed(); } }3. 数据权限实体类DataScope.java定义了数据权限的核心结构public class DataScope { private String scopeName deptid; // 限制范围的字段名称 private ListInteger deptIds; // 限制范围的部门ID集合 public DataScope(String scopeName, ListInteger deptIds) { this.scopeName scopeName; this.deptIds deptIds; } }实战如何在业务中使用数据权限1. 用户管理中的数据权限应用在UserMgrController.java中数据权限被广泛应用RequestMapping(/list) Permission ResponseBody public Object list(RequestParam(required false) String name, RequestParam(required false) String beginTime, RequestParam(required false) String endTime, RequestParam(required false) Integer deptid) { Integer userId ShiroKit.getUser().getId(); User user userMapper.selectByPrimaryKey(userId); DataScope dataScope new DataScope(ShiroKit.getDeptDataScope(user)); ListMapString, Object users userMapper.selectUsers(dataScope, name, beginTime, endTime, deptid); return new UserWarpper(users).warp(); }2. Mapper接口的数据权限参数UserMapper.java中定义了支持数据权限的查询方法ListMapString, Object selectUsers(Param(dataScope) DataScope dataScope, Param(name) String name, Param(beginTime) String beginTime, Param(endTime) String endTime, Param(deptid) Integer deptid);3. 部门数据范围获取ShiroKit.java提供了获取用户数据权限范围的方法public static ListInteger getDeptDataScope(User user) { Integer deptId user null ? getUser().getDeptId() : user.getDeptid(); ListInteger subDeptIds ConstantFactory.me().getSubDeptId(deptId); subDeptIds.add(deptId); return subDeptIds; }权限注解与AOP拦截1. 权限注解定义Permission.java定义了权限检查注解Inherited Retention(RetentionPolicy.RUNTIME) Target({ElementType.METHOD}) public interface Permission { String[] value() default {}; }2. AOP权限拦截器PermissionAop.java实现了基于注解的权限检查Aspect Component public class PermissionAop { Pointcut(value annotation(com.stylefeng.guns.common.annotion.Permission)) private void cutPermission() {} Around(cutPermission()) public Object doPermission(ProceedingJoinPoint point) throws Throwable { MethodSignature ms (MethodSignature) point.getSignature(); Method method ms.getMethod(); Permission permission method.getAnnotation(Permission.class); Object[] permissions permission.value(); if (permissions null || permissions.length 0) { // 检查全体角色 boolean result PermissionCheckManager.checkAll(); if (result) { return point.proceed(); } else { throw new NoPermissionException(); } } else { // 检查指定角色 boolean result PermissionCheckManager.check(permissions); if (result) { return point.proceed(); } else { throw new NoPermissionException(); } } } }权限管理界面展示Guns提供了直观的权限管理界面让管理员可以轻松配置角色和权限系统提供了完整的用户管理功能包括用户创建、角色分配、权限设置等数据权限配置实战1. 配置数据权限拦截器在MyBatis配置中启用数据权限拦截器Bean public DataScopeInterceptor dataScopeInterceptor() { return new DataScopeInterceptor(); }2. 业务层数据权限使用在业务逻辑中只需在Mapper方法参数中添加DataScope对象即可// 查询用户列表自动应用数据权限 public ListUser findUsersByCondition(DataScope dataScope, UserQuery query) { return userMapper.selectUsers(dataScope, query.getName(), query.getBeginTime(), query.getEndTime(), query.getDeptid()); }3. 控制器层权限控制结合权限注解实现细粒度控制Permission(Const.ADMIN_NAME) // 仅管理员可访问 RequestMapping(/add) BussinessLog(value 添加管理员, key account, dict Dict.UserDict) ResponseBody public Tip add(Valid UserDto user, BindingResult result) { // 业务逻辑 }权限管理的最佳实践1. 分层权限控制策略Guns采用三层权限控制策略URL级别权限通过Shiro过滤器链控制方法级别权限通过Permission注解控制数据级别权限通过DataScope拦截器控制2. 动态数据权限根据用户角色动态生成数据权限范围public DataScope buildDataScopeByUser(User user) { if (ShiroKit.isAdmin()) { // 管理员拥有所有数据权限 return null; } else { // 普通用户只能看到自己部门及子部门的数据 ListInteger deptIds ShiroKit.getDeptDataScope(user); return new DataScope(deptid, deptIds); } }3. 权限缓存优化Guns利用Ehcache对权限数据进行缓存提升系统性能Cacheable(value Cache.CONSTANT, key CacheKey.DEPT_NAME #deptId) public String getDeptName(Integer deptId) { return this.deptMapper.getDeptName(deptId); }系统部署与配置1. 数据库初始化首先导入项目中的SQL文件guns.sql - 系统基础数据表biz.sql - 业务数据表2. 配置文件调整修改application.yml中的数据库配置spring: datasource: url: jdbc:mysql://localhost:3306/guns?autoReconnecttrueuseUnicodetruecharacterEncodingutf8zeroDateTimeBehaviorconvertToNull username: root password: 1234563. 启动项目Guns支持多种启动方式在IDE中运行GunsApplication类的main方法使用Maven打包后通过Java命令启动打包为WAR文件部署到Tomcat总结与展望Guns权限管理系统通过创新的数据范围拦截器设计解决了企业级应用中复杂的数据权限控制问题。其核心优势包括简洁高效通过MyBatis拦截器实现数据权限无需修改业务SQL灵活配置支持基于部门的数据权限控制满足多层级组织架构需求易于扩展DataScope设计可扩展支持更多维度的数据权限控制性能优秀结合Ehcache缓存权限验证性能优异通过本文的详细介绍您应该已经掌握了Guns权限管理系统的核心原理和实战应用。无论是新建项目还是现有系统改造Guns都提供了一个成熟、稳定且易于扩展的权限管理解决方案。核心关键词Guns权限管理系统、Shiro数据权限控制、SpringBoot后台管理、数据范围拦截器、企业级权限管理长尾关键词基于Shiro的数据权限控制终极方案、Guns权限管理系统实战教程、SpringBoot权限管理框架、企业级后台管理系统数据隔离、MyBatis数据范围拦截器实现【免费下载链接】gunsGuns基于SpringBoot,致力于做更简洁的后台管理系统,完美整合springmvc shiro 分页插件PageHelper 通用Mapper beetl!Guns项目代码简洁,注释丰富,上手容易,同时Guns包含许多基础模块(用户管理,角色管理,部门管理,字典管理等10个模块),可以直接作为一个后台管理系统的脚手架.项目地址: https://gitcode.com/gh_mirrors/gun/guns创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考