# privilege **Repository Path**: mirrors_houbb/privilege ## Basic Information - **Project Name**: privilege - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-09-18 - **Last Updated**: 2026-05-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # privilege [privilege](https://github.com/houbb/privilege) 是一款为 java 设计的权限控制框架。 [![Build Status](https://travis-ci.com/houbb/privilege.svg?branch=master)](https://travis-ci.com/houbb/privilege) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.houbb/privilege/badge.svg)](http://mvnrepository.com/artifact/com.github.houbb/privilege) [![](https://img.shields.io/badge/license-Apache2-FF0080.svg)](https://github.com/houbb/privilege/blob/master/LICENSE.txt) [![Open Source Love](https://badges.frapsoft.com/os/v2/open-source.svg?v=103)](https://github.com/houbb/privilege) ## 创作目的 便于基础工具开发,后期考虑统一的版本管理。 > [变更日志](https://github.com/houbb/privilege/blob/master/CHANGELOG.md) ## 特性 - 支持 proxy - 支持 spring aop 整合 - 支持 spring mvc 整合 # 快速开始 ## maven 引入 ```xml com.github.houbb privilege-core ${最新版本} ``` ## 入门案例 测试代码见 [PrivilegeBsTest.java]() ```java boolean hasPrivilege = PrivilegeBs.newInstance().hasPrivilege(); Assert.assertTrue(hasPrivilege); ``` ## 指定拥有的权限 ### 自定义实现 实现 `IPrivilegeOwn` 接口即可。 可以查询数据库,文件等。 ```java public class PrivilegeOwnOne implements IPrivilegeOwn { @Override public List ownPrivilege() { IPrivilegeInfo info = PrivilegeInfo.newInstance().code("001"); return Collections.singletonList(info); } } ``` ### 指定 ```java IPrivilegeOwn own = new PrivilegeOwnOne(); boolean hasPrivilege = PrivilegeBs.newInstance() .own(own) .hasPrivilege(); Assert.assertTrue(hasPrivilege); ``` ## 指定需要的权限 ### 实现 实现接口 `IPrivilegeAcquire` 即可。 ```java public class PrivilegeAcquireTwo implements IPrivilegeAcquire { @Override public List acquirePrivilege() { IPrivilegeInfo one = PrivilegeInfo.newInstance().code("001"); IPrivilegeInfo two = PrivilegeInfo.newInstance().code("002"); return Arrays.asList(one, two); } } ``` ### 指定 ```java IPrivilegeOwn own = new PrivilegeOwnOne(); IPrivilegeAcquire acquire = new PrivilegeAcquireTwo(); boolean hasPrivilege = PrivilegeBs.newInstance() .own(own) .acquire(acquire) .hasPrivilege(); Assert.assertFalse(hasPrivilege); ``` ## 指定实现策略 默认的实现策略为需要的 code 全部都拥有才算拥有权限,我们也可以指定其他实现策略。 ### 内置策略 内置的策略,可以通过 `Privileges` 直接获取。 | 策略 | 说明 | |:---|:---| | all() | 需要的权限编码,全部都拥有,才认为拥有权限。(默认策略)| | any() | 需要的权限编码,拥有任何一个,则认为拥有权限。| | allow() | 白名单,直接返回拥有权限。| | deny() | 黑名单,直接返回不拥有权限。| ### 测试 ```java IPrivilegeOwn own = new PrivilegeOwnOne(); IPrivilegeAcquire acquire = new PrivilegeAcquireTwo(); IPrivilege privilege = Privileges.any(); boolean hasPrivilege = PrivilegeBs.newInstance() .own(own) .acquire(acquire) .privilege(privilege) .hasPrivilege(); Assert.assertTrue(hasPrivilege); ``` # 代理实现 ## maven 依赖 ```xml com.github.houbb privilege-proxy ${最新版本} ``` ## 注解 引入 `@PrivilegeAcquire` 注解,用于声明在方法上,指定访问需要的权限。 ```java @PrivilegeAcquire(code = "1001") public boolean removeInfo() { return true; } ``` 这里表明如果想执行删除操作,必须拥有权限 1001 ## 测试案例 测试案例见 [PrivilegeProxyTest.java]() ### 不指定用户权限 ```java try { UserService userService = PrivilegeProxy.proxy(new UserService()); userService.removeInfo(); } catch (Exception e) { e.printStackTrace(); } ``` 这里会抛出异常 PrivilegeRuntimeException: ``` com.github.houbb.privilege.api.exception.PrivilegeRuntimeException: Has no privilege! Acquire: <[1001]>, Own: <[]> ``` 实际开发中,可以根据实际的业务进行逻辑处理。 ### 指定用户权限 类似于 shiro,我们在调用之前,首先指定用户的权限信息。 后续将结合 springmvc,做到自动设置权限信息。 ```java ISubject subject = Subject.newInstance().privileges("1001"); SubjectUtils.put(subject); UserService userService = PrivilegeProxy.proxy(new UserService()); userService.removeInfo(); ``` # spring 整合 ## maven 引入 ```xml com.github.houbb privilege-spring ${最新版本} ``` ## 测试代码 所有测试代码,参见 [SpringTest.java]() ### 测试类 类似于 proxy 中的实现,只不过是通过 spring-aop 实现代码增强。 ```java @Service public class UserService { @PrivilegeAcquire(code = "1001") public boolean removeInfo() { return true; } } ``` ### 测试代码 测试效果和 proxy 中类似。 ```java @ContextConfiguration(classes = SpringConfig.class) @RunWith(SpringJUnit4ClassRunner.class) public class SpringTest { @Autowired private UserService userService; @Test public void guestRemoveTest() { try { userService.removeInfo(); } catch (Exception e) { e.printStackTrace(); } } @Test public void adminRemoveTest() { ISubject subject = Subject.newInstance().privileges("1001"); SubjectUtils.put(subject); userService.removeInfo(); } } ``` 其中 SpringConfig 实现如下: ```java @Configurable @ComponentScan(basePackages = "com.github.houbb.privilege.test.service") @EnablePrivilege public class SpringConfig { } ``` 主要指定扫描的包,并且通过注解 `@EnablePrivilege` 启动权限校验。 # spring mvc 整合 ## maven 引入 ```xml com.github.houbb privilege-mvc ${最新版本} ``` ## 代码实现 - 自定义拦截器 ```java @Component public class MyPrivilegeInterceptor extends AbstractPrivilegeInterceptor { @Override protected void fillSubject(ISubject subject, HttpServletRequest request) { String id = request.getParameter("id"); if("admin".equals(id)) { subject.privileges("1001"); } } } ``` - 注册拦截器 ```java @Configuration public class InterceptorConfig extends WebMvcConfigurerAdapter { @Autowired private MyPrivilegeInterceptor myPrivilegeInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myPrivilegeInterceptor) .addPathPatterns("/hello"); } } ``` 其他和以前保持一样 ## 测试 页面访问 [http://localhost:8080/hello](http://localhost:8080/hello) 报错权限不足; 页面访问 [http://localhost:8080/hello?id=admin](http://localhost:8080/hello?id=admin) 正常访问。 # Road-Map - [ ] 基本路线 api=>core=>proxy=>spring=>sprig-mvc=>springboot admin 的完整微服务实现 menu => role => privilege