Skip to content

Commit 8c261ec

Browse files
authored
Merge pull request #204 from LeoMalik/feature/data-auth
Feature/data auth
2 parents 99f51e9 + 14d50f0 commit 8c261ec

File tree

59 files changed

+1667
-59
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1667
-59
lines changed

auth/authentication-client/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
</dependencyManagement>
4343

4444
<dependencies>
45+
<dependency>
46+
<groupId>com.springboot.cloud</groupId>
47+
<artifactId>facade</artifactId>
48+
<version>0.0.1-SNAPSHOT</version>
49+
</dependency>
4550
<dependency>
4651
<groupId>com.springboot.cloud</groupId>
4752
<artifactId>core</artifactId>

auth/authentication-client/src/main/java/com/springboot/cloud/auth/client/provider/AuthProvider.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.springboot.cloud.auth.client.provider;
22

33
import com.springboot.cloud.common.core.entity.vo.Result;
4+
import com.springboot.cloud.sysadmin.facade.dto.PermissionDTO;
45
import org.springframework.cloud.openfeign.FeignClient;
56
import org.springframework.http.HttpHeaders;
67
import org.springframework.stereotype.Component;
78
import org.springframework.web.bind.annotation.PostMapping;
9+
import org.springframework.web.bind.annotation.RequestBody;
810
import org.springframework.web.bind.annotation.RequestHeader;
911
import org.springframework.web.bind.annotation.RequestParam;
1012

@@ -29,6 +31,10 @@ public interface AuthProvider {
2931
@PostMapping(value = "/auth/permission")
3032
Result auth(@RequestHeader(HttpHeaders.AUTHORIZATION) String authentication, @RequestParam("url") String url, @RequestParam("method") String method);
3133

34+
@PostMapping(value = "/auth/data/permission")
35+
Result dataAuth(@RequestHeader(HttpHeaders.AUTHORIZATION) String authentication, @RequestBody PermissionDTO permissionDTO);
36+
37+
3238
@Component
3339
class AuthProviderFallback implements AuthProvider {
3440
/**
@@ -49,5 +55,17 @@ class AuthProviderFallback implements AuthProvider {
4955
public Result auth(String authentication, String url, String method) {
5056
return Result.fail();
5157
}
58+
59+
/**
60+
* 降级统一返回无权限
61+
*
62+
* @param authentication 身份验证
63+
* @param permissionDTO 许可dto
64+
* @return {@link Result}
65+
*/
66+
@Override
67+
public Result dataAuth(String authentication, PermissionDTO permissionDTO) {
68+
return Result.fail();
69+
}
5270
}
5371
}

auth/authentication-client/src/main/java/com/springboot/cloud/auth/client/service/IAuthService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
package com.springboot.cloud.auth.client.service;
22

33
import com.springboot.cloud.common.core.entity.vo.Result;
4+
import com.springboot.cloud.sysadmin.facade.dto.PermissionDTO;
45
import io.jsonwebtoken.Claims;
56
import io.jsonwebtoken.Jws;
67

78
public interface IAuthService {
9+
10+
/**
11+
* 数据权限验证
12+
*
13+
* @param authentication 身份验证
14+
* @param permissionDTO 许可dto
15+
* @param groupCode 组织代码
16+
* @return {@link Result}
17+
*/
18+
Result dataAuthenticate(String authentication,String groupCode, PermissionDTO permissionDTO);
819
/**
920
* 调用签权服务,判断用户是否有权限
1021
*

auth/authentication-client/src/main/java/com/springboot/cloud/auth/client/service/impl/AuthService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.springboot.cloud.auth.client.provider.AuthProvider;
44
import com.springboot.cloud.auth.client.service.IAuthService;
55
import com.springboot.cloud.common.core.entity.vo.Result;
6+
import com.springboot.cloud.sysadmin.facade.dto.PermissionDTO;
67
import io.jsonwebtoken.*;
78
import lombok.extern.slf4j.Slf4j;
89
import org.apache.commons.lang.StringUtils;
@@ -36,6 +37,11 @@ public class AuthService implements IAuthService {
3637
@Value("${gate.ignore.authentication.startWith}")
3738
private String ignoreUrls = "/oauth";
3839

40+
@Override
41+
public Result dataAuthenticate(String authentication, String groupCode, PermissionDTO permissionDTO) {
42+
return authProvider.dataAuth(authentication,permissionDTO);
43+
}
44+
3945
@Override
4046
public Result authenticate(String authentication, String url, String method) {
4147
return authProvider.auth(authentication, url, method);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 权限校验
2+
3+
## 核心理念
4+
5+
建立用户-组-权限资源的关系,对用户进行鉴权
6+
7+
[![o4WeAI.png](https://s1.ax1x.com/2021/12/10/o4WeAI.png)](https://imgtu.com/i/o4WeAI)
8+
9+
10+
11+
## 鉴权流程
12+
13+
### 在权限表(permission)中插入资源信息
14+
15+
```sql
16+
INSERT INTO permission (id, res_type, area, res_full_path, res_full_name, operation_bit, expire_date, created_time,
17+
updated_time, created_by, updated_by)
18+
VALUES (101, 'hive', 'china', '/test.db/test', '/测试库/测试表', 'select', '2099-12-26 10:45:26', now(), now(),
19+
'system', 'system'),
20+
(102, 'hive', 'china', '/test.db/test1', '/测试库/测试表1', 'select', '2099-12-26 10:45:26', now(), now(),
21+
'system', 'system'),
22+
(103, 'hive', 'china', '/test.db', '/测试库', 'select', '2099-12-26 10:45:26', now(), now(),
23+
'system', 'system');
24+
```
25+
26+
### 在组-权限表建立连接
27+
28+
```sql
29+
INSERT INTO group_permission_relation (id, group_id, permission_id, created_time, updated_time, created_by, updated_by)
30+
VALUES (1, 101, 101, now(), now(), 'system', 'system'),
31+
(2, 101, 102, now(), now(), 'system', 'system'),
32+
(3, 101, 103, now(), now(), 'system', 'system'),
33+
(4, 102, 101, now(), now(), 'system', 'system'),
34+
(5, 102, 102, now(), now(), 'system', 'system'),
35+
(6, 103, 101, now(), now(), 'system', 'system'),
36+
(7, 103, 102, now(), now(), 'system', 'system'),
37+
(8, 103, 103, now(), now(), 'system', 'system');
38+
```
39+
40+
### 访问接口进行鉴权
41+
42+
```shell
43+
curl --location --request POST 'http://127.0.0.1:8443/authentication-server/auth/data/permission' \
44+
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbInJlYWQiXSwib3JnYW5pemF0aW9uIjoiYWRtaW4iLCJleHAiOjE2MzkwNzQ1MTMsImF1dGhvcml0aWVzIjpbIkFETUlOIl0sImp0aSI6IllsVVhlV2d1VFBRbGdmSHYyY0VGOC1seEVGRSIsImNsaWVudF9pZCI6InRlc3RfY2xpZW50In0.1JqUvfv5i9wd9F7hWYW-Xafoc5bh9tFEupIoVYW09nU' \
45+
--header 'User-Agent: apifox/1.0.0 (https://www.apifox.cn)' \
46+
--header 'Content-Type: application/json' \
47+
--data-raw '{
48+
"resFullName": "/测试库/测试表",
49+
"area": "china",
50+
"resType": "hive",
51+
"operationBit": "select",
52+
"resFullPath": "/test.db/test",
53+
"groupCode": "101"
54+
}'
55+
```
56+
57+
[![o4WzrQ.png](https://s1.ax1x.com/2021/12/10/o4WzrQ.png)](https://imgtu.com/i/o4WzrQ)
58+

auth/authentication-server/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
</parent>
1919

2020
<dependencies>
21+
<dependency>
22+
<groupId>com.springboot.cloud</groupId>
23+
<artifactId>facade</artifactId>
24+
<version>0.0.1-SNAPSHOT</version>
25+
</dependency>
2126
<dependency>
2227
<groupId>com.springboot.cloud</groupId>
2328
<artifactId>web</artifactId>

auth/authentication-server/src/main/java/com/springboot/cloud/auth/authentication/Oauth2AuthenticationApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.springboot.cloud.auth.authentication;
22

33
import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
4+
import com.alicp.jetcache.anno.config.EnableMethodCache;
45
import org.springframework.boot.SpringApplication;
56
import org.springframework.boot.autoconfigure.SpringBootApplication;
67
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@@ -9,6 +10,7 @@
910
@SpringBootApplication
1011
@EnableDiscoveryClient
1112
@EnableFeignClients
13+
@EnableMethodCache(basePackages = "com.springboot.cloud")
1214
@EnableCreateCacheAnnotation
1315
public class Oauth2AuthenticationApplication {
1416
public static void main(String[] args) {

auth/authentication-server/src/main/java/com/springboot/cloud/auth/authentication/config/BusConfig.java

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.fasterxml.jackson.annotation.JsonAutoDetect;
44
import com.fasterxml.jackson.annotation.PropertyAccessor;
55
import com.fasterxml.jackson.databind.ObjectMapper;
6-
import com.springboot.cloud.auth.authentication.events.BusReceiver;
6+
import com.springboot.cloud.auth.authentication.events.ResourceBusReceiver;
77
import lombok.extern.slf4j.Slf4j;
88
import org.springframework.amqp.core.*;
99
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
@@ -12,6 +12,7 @@
1212
import org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter;
1313
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
1414
import org.springframework.amqp.support.converter.MessageConverter;
15+
import org.springframework.beans.factory.annotation.Qualifier;
1516
import org.springframework.beans.factory.annotation.Value;
1617
import org.springframework.context.annotation.Bean;
1718
import org.springframework.context.annotation.Configuration;
@@ -21,49 +22,74 @@
2122
public class BusConfig {
2223

2324
private static final String EXCHANGE_NAME = "spring-boot-exchange";
24-
private static final String ROUTING_KEY = "organization-resource";
25+
private static final String RESOURCE_ROUTING_KEY = "organization-resource";
26+
private static final String RESOURCE_QUEUE_SUFFIX = "resource";
2527

2628
@Value("${spring.application.name}")
2729
private String appName;
2830

2931
@Bean
30-
Queue queue() {
31-
String queueName = new Base64UrlNamingStrategy(appName + ".").generateName();
32-
log.info("queue name:{}", queueName);
32+
Queue resourceQueue() {
33+
String queueName = new Base64UrlNamingStrategy(appName + ".").generateName() + RESOURCE_QUEUE_SUFFIX;
34+
log.info("resource queue name:{}", queueName);
3335
return new Queue(queueName, false);
3436
}
3537

38+
39+
/**
40+
* 交换机
41+
*
42+
* @return {@link TopicExchange}
43+
*/
3644
@Bean
3745
TopicExchange exchange() {
3846
log.info("exchange:{}", EXCHANGE_NAME);
3947
return new TopicExchange(EXCHANGE_NAME);
4048
}
4149

50+
51+
/**
52+
* 绑定资源更新的队列
53+
*
54+
* @param queue 队列
55+
* @param exchange 交换机
56+
* @return {@link Binding}
57+
*/
4258
@Bean
43-
Binding binding(Queue queue, TopicExchange exchange) {
44-
log.info("binding {} to {} with {}", queue, exchange, ROUTING_KEY);
45-
return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
59+
Binding resourceBinding(@Qualifier("resourceQueue") Queue queue, TopicExchange exchange) {
60+
log.info("binding {} to {} with {}", queue, exchange, RESOURCE_ROUTING_KEY);
61+
return BindingBuilder.bind(queue).to(exchange).with(RESOURCE_ROUTING_KEY);
4662
}
4763

64+
65+
66+
67+
////////////////////
68+
//////////////////// 资源更新相关配置
69+
////////////////////
4870
@Bean
49-
SimpleMessageListenerContainer simpleMessageListenerContainer(ConnectionFactory connectionFactory, MessageListenerAdapter messageListenerAdapter, Queue queue) {
50-
log.info("init simpleMessageListenerContainer {}", queue.getName());
71+
SimpleMessageListenerContainer resourceMessageListenerContainer(ConnectionFactory connectionFactory, @Qualifier("resourceMessageListenerAdapter") MessageListenerAdapter messageListenerAdapter, @Qualifier("resourceQueue") Queue queue) {
72+
log.info("init resourceMessageListenerContainer {}", queue.getName());
5173
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
5274
container.setQueueNames(queue.getName());
5375
container.setMessageListener(messageListenerAdapter);
5476
return container;
5577
}
5678

79+
5780
@Bean
58-
MessageListenerAdapter messageListenerAdapter(BusReceiver busReceiver, MessageConverter messageConverter) {
81+
MessageListenerAdapter resourceMessageListenerAdapter(ResourceBusReceiver resourceBusReceiver, @Qualifier("resourceMessageConverter") MessageConverter messageConverter) {
5982
log.info("new listener");
60-
return new MessageListenerAdapter(busReceiver, messageConverter);
83+
return new MessageListenerAdapter(resourceBusReceiver, messageConverter);
6184
}
6285

6386
@Bean
64-
public MessageConverter messageConverter() {
87+
public MessageConverter resourceMessageConverter() {
6588
ObjectMapper objectMapper = new ObjectMapper();
6689
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
6790
return new ContentTypeDelegatingMessageConverter(new Jackson2JsonMessageConverter(objectMapper));
6891
}
92+
93+
94+
6995
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
@Component
1010
@Slf4j
11-
public class BusReceiver {
11+
public class ResourceBusReceiver {
1212

1313
@Autowired
1414
private ResourceService resourceService;
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.springboot.cloud.auth.authentication.provider;
22

3-
import com.springboot.cloud.sysadmin.organization.entity.po.Resource;
43
import com.springboot.cloud.common.core.entity.vo.Result;
4+
import com.springboot.cloud.sysadmin.facade.dto.GroupDTO;
5+
import com.springboot.cloud.sysadmin.facade.dto.PermissionDTO;
6+
import com.springboot.cloud.sysadmin.organization.entity.po.Resource;
57
import org.springframework.cloud.openfeign.FeignClient;
6-
import org.springframework.web.bind.annotation.GetMapping;
7-
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.*;
89

10+
import java.util.List;
911
import java.util.Set;
1012

1113
@FeignClient(name = "organization", fallback = ResourceProviderFallback.class)
@@ -16,4 +18,10 @@ public interface ResourceProvider {
1618

1719
@GetMapping(value = "/resource/user/{username}")
1820
Result<Set<Resource>> resources(@PathVariable("username") String username);
21+
22+
@PostMapping(value = "/permission/group")
23+
Result<List<PermissionDTO>> permissions(@RequestBody PermissionDTO permissionDTO);
24+
25+
@GetMapping(value = "/group/user/{username}")
26+
Result<List<GroupDTO>> groups(@PathVariable("username") String username);
1927
}

0 commit comments

Comments
 (0)