Browse Source

优化说明

zhouhao 6 years ago
parent
commit
3fa2a5a2d3

+ 1 - 1
hsweb-commons/hsweb-commons-controller/README.md

@@ -7,7 +7,7 @@
 | ------------- | -------------| ------------- | ----|
 |查询|GET /user|HTTP Status:200 {"status":200,"result":{"data":[],"total":0}} |可进行[动态查询](#动态查询)|
 |不分页查询|GET /user/no-paging|HTTP Status:200 {"status":200,"result":[]} |可进行[动态查询](#动态查询)|
-|获取指定id的数据|GET /user/id|HTTP Status:200 {"status":200,"result":{"name":""} |可进行[动态查询](#动态查询)|
+|获取指定id的数据|GET /user/{id}|HTTP Status:200 {"status":200,"result":{"name":""} |可进行[动态查询](#动态查询)|
 |新增|POST /user|HTTP Status:201 {"status":201,"result":"{id}"} |contentType='application/json' |
 |更新|PUT /user/{id}|HTTP Status:200 {"status":200} |contentType='application/json'|
 |新增或者更新|PATCH /user|HTTP Status:200 {"status":200,"result":"{id}"} |contentType='application/json' |

+ 140 - 0
quick-start/AUTZ.md

@@ -0,0 +1,140 @@
+# 权限控制
+
+hsweb提供了一个灵活的权限控制,设置方式,实现了多维度,可自定义的RBAC和数据权限控制.
+
+## 使用
+
+以下例子在[快速入门](README.md)的基础上建立
+
+1. 引入依赖
+```xml
+    <!--权限控制-->
+    <dependency>
+        <groupId>org.hswebframework.web</groupId>
+        <artifactId>hsweb-authorization-basic</artifactId>
+        <version>${hsweb.framework.version}</version>
+    </dependency>
+```
+
+2. 在启动类上注解:`@EnableAopAuthorize`
+```java
+@SpringBootApplication
+@MapperScan(basePackages = "com.mycompany.dao", markerInterface = Dao.class)
+@EnableAopAuthorize //开启AOP权限控制
+public class MyProjectApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(MyProjectApplication.class, args);
+    }
+}
+```
+
+3. 在`application.yml`中加入配置
+```yaml
+hsweb:
+    users:
+        admin:
+          name: 超级管理员
+          username: admin
+          password: admin
+          roles: #用户的角色
+            - id: admin
+              name: 管理员
+            - id: user
+              name: 用户
+          permissions:  # 用户的权限
+            - id: test #权限标识,可以理解为资源
+              actions: query,get,update,delete #用户持有对该资源的操作
+              dataAccesses:   # 数据权限配置,此配置表示在对test进行query操作的时候,不能查询password和salt字段
+                - action: query
+                  type: DENY_FIELDS
+                  fields:     
+                    - password
+                    - salt
+```
+
+4. 在`TestController`中加入权限控制,在类上注解`@Authorize(permission = "test")`
+
+```java
+@RestController
+@RequestMapping("/test")
+@Authorize(permission = "test")
+public class TestController implements SimpleGenericEntityController<TestEntity, String, QueryParamEntity> {
+
+    @Autowired
+    TestService testService;
+
+    @Override
+    public CrudService<TestEntity, String> getService() {
+        return testService;
+    }
+}
+```
+
+5. 启动服务,使用postman访问:`GET http://localhost:8080/test`.返回
+```json
+{
+    "message": "用户未登录",
+    "result": {
+        "index": 3,
+        "text": "用户未登录",
+        "value": "expired",
+        "mask": 8
+    },
+    "status": 401,
+    "timestamp": 1537342065201
+}
+```
+
+6. 调用登录接口登录`POST http://localhost:8080/authorize/login`
+
+username=admin&password=admin
+
+返回:
+```json
+{
+    "result": {
+        "userId": "admin"
+    },
+    "status": 200,
+    "timestamp": 1537342179265
+}
+```
+
+7.再次访问`GET http://localhost:8080/test`.返回正常数据.
+
+通过接口`GET http://localhost:8080/authorize/me`可查看当前登录用户的权限信息.
+
+通过接口`GET http://localhost:8080/authorize/exit`可退出登录.
+
+![test-auth](./img/test-auth.gif "test-auth")
+
+
+# 编程式
+
+可通过在Controller方法参数中直接注入`org.hswebframework.web.authorization.Authentication`接口获取当前登录用户权限信息.
+也可以通过静态方法方式获取:
+
+```java
+Authentication authentication= Authentication.current().orElseThrow(UnAuthorizedException::new);
+```
+
+可调用此接口的方法进行用户权限获取以及判断:
+```java
+//用户是否可以进行test的query操作
+authentication.hasPermission("test", "query");
+
+//用户是否拥有admin角色
+authentication.hasRole("admin");
+
+//获取用户在进行test的query操作时,不能操作的字段列表
+Set<String> denyFiledList= authentication.getPermission("test")
+            .map(permission->permission.findDenyFields("query"))
+            .orElseThrow(AccessDenyException::new);
+
+//获取用户在进行test的query操作时,能访问部门(department)(含子部门(children))数据的范围
+Set<Object> canQueryDepartment= authentication.getPermission("test")
+            .map(permission->permission.findScope("query","department","children"))
+            .orElseThrow(AccessDenyException::new);
+
+```

+ 3 - 2
quick-start/README.md

@@ -43,9 +43,10 @@ hsweb 目前未提供前端支持,仅有一个[demo](https://github.com/hs-web/h
         <java.version>1.8</java.version>
         <project.build.jdk>${java.version}</project.build.jdk>
 
-        <hsweb.framework.version>3.0.0-RC-SNAPSHOT</hsweb.framework.version>
+        <hsweb.framework.version>3.0.0</hsweb.framework.version>
+
+        <hsweb.expands.version>3.0.1</hsweb.expands.version>
 
-        <hsweb.expands.version>3.0.1-SNAPSHOT</hsweb.expands.version>
 
     </properties>
 

+ 12 - 3
quick-start/USE-CRUD.md

@@ -73,12 +73,17 @@ hsweb提供了通用增删改查功能,并实现使用mybatis和easyorm实现了
    createDelete().where("id",id).exec();
 ```
 
-支持的条件类型方法:
-is(eq),not,in,notIn,isNull,notNull,like,notLike,lt,gte,lte,自定义等等.
+如果想调用其他dao进行dsl操作,可使用`DefaultDSLQueryService.createQuery(dao)`
+,`DefaultDSLUpdateService.createUpdate(dap)`
+,`DefaultDSLDeleteService.createDelete(dao)` 进行操作
 
-可以拓展支持一些[自定义的通用的查询条件](../hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/README.md#拓展动态条件),例如: 
+
+默认支持的条件类型方法:is(eq),not,in,notIn,isNull,notNull,like,notLike,lt,gte,lte,等等.
+
+还可以[自定义的通用的查询条件](../hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/README.md#拓展动态条件): 
 
 以在组织架构模块[中自定义的查询条件](../hsweb-system/hsweb-system-organizational/README.md#SQL条件)为例:
+
 ```java
 //user-in-org为自定义的查询条件类型
 //userId字段为指定机构中的用户的数据
@@ -87,3 +92,7 @@ createQuery()
     .list();
 ```
 这样就无需在需要用到相关查询的地方重复的编写mybatis mapper xml.
+
+3. 通用Controller
+
+参照[这里](../hsweb-commons/hsweb-commons-controller/README.md)

+ 18 - 0
quick-start/UTILS.md

@@ -0,0 +1,18 @@
+# 实用工具包
+
+hsweb提供了一些实用的工具类
+
+1. 实体类属性拷贝
+
+```java
+//拷贝source的属性到target
+FastBeanCopier.copy(source,target);
+//id和createTime属性不拷贝
+FastBeanCopier.copy(source,target,"id","createTime");
+//只拷贝name和age属性
+FastBeanCopier.copy(source,target,FastBeanCopier.include("name","age"));
+
+```
+
+2. 枚举数据字典,[点击查看](../hsweb-core/README.md#数据字典])
+

BIN
quick-start/img/test-auth.gif