权限控制
hsweb提供了一个灵活的权限控制,设置方式,实现了多维度,可自定义的RBAC和数据权限控制.

引入依赖

1
<!--权限控制-->
2
<dependency>
3
<groupId>org.hswebframework.web</groupId>
4
<artifactId>hsweb-authorization-basic</artifactId>
5
<version>${hsweb.framework.version}</version>
6
</dependency>
Copied!

添加注解

在启动类上注解:@EnableAopAuthorize
1
@SpringBootApplication
2
@MapperScan(basePackages = "com.mycompany.dao", markerInterface = Dao.class)
3
@EnableAopAuthorize //开启AOP权限控制
4
public class MyProjectApplication {
5
6
public static void main(String[] args) {
7
SpringApplication.run(MyProjectApplication.class, args);
8
}
9
}
Copied!

定义用户和权限

application.yml中加入配置
1
hsweb:
2
users:
3
admin:
4
name: 超级管理员
5
username: admin
6
password: admin
7
roles: #用户的角色
8
- id: admin
9
name: 管理员
10
- id: user
11
name: 用户
12
permissions: # 用户的权限
13
- id: test #权限标识,可以理解为资源
14
actions: query,get,update,delete #用户持有对该资源的操作
15
dataAccesses: # 数据权限配置,此配置表示在对test进行query操作的时候,不能查询password和salt字段
16
- action: query
17
type: DENY_FIELDS
18
fields:
19
- password
20
- salt
Copied!

定义要控制的接口

TestController中加入权限控制,在类上注解@Authorize(permission = "test")
1
@RestController
2
@RequestMapping("/test")
3
@Authorize(permission = "test")
4
public class TestController implements SimpleGenericEntityController<TestEntity, String, QueryParamEntity> {
5
6
@Autowired
7
TestService testService;
8
9
@Override
10
public CrudService<TestEntity, String> getService() {
11
return testService;
12
}
13
}
Copied!
Last modified 2yr ago