DjServiceAccessConfig.java
7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package cn.com.glsx.dj.rop.security;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import cn.com.glsx.dj.system.dto.RopAppAccessDTO;
import cn.com.glsx.dj.system.dto.RopAppServiceDTO;
import cn.com.glsx.dj.system.enums.RopAppStatusEnum;
import cn.com.glsx.dj.system.enums.RopServiceStatusEnum;
import cn.com.glsx.dj.system.model.RopAppAccess;
import cn.com.glsx.dj.system.model.RopAppService;
import cn.com.glsx.dj.system.model.RopService;
import cn.com.glsx.dj.system.remote.RopAppAccessRemote;
import cn.com.glsx.dj.system.remote.RopAppServiceRemote;
import cn.com.glsx.dj.system.remote.RopServiceRemote;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
/**
* @Title: DjServiceAccessConfig.java
* @Description: 嘀加服务授权配置
* @author Alvin
* @date 2020年1月6日 上午10:31:22
* @version V1.0
* @Company: didihu.com.cn
* @Copyright Copyright (c) 2020
*/
@Component
public class DjServiceAccessConfig implements ApplicationListener<ContextRefreshedEvent>{
private Logger logger = LoggerFactory.getLogger(getClass());
private Map<String, String> availableAppKeySecretMap = new ConcurrentHashMap<String, String>();
private Map<String, ConcurrentHashMap<String, Long>> availableAppKeyServiceMap = new ConcurrentHashMap<String, ConcurrentHashMap<String, Long>>();
@Autowired
private RopAppAccessRemote ropAppAccessRemote;
@Autowired
private RopAppServiceRemote ropAppServiceRemote;
@Autowired
private RopServiceRemote ropServiceRemote;
/*监听器初始化*/
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
initAccessData();
}
/**
* 初始化授权数据
* @author Alvin
* @date 2020年1月6日 上午10:44:38
*/
private void initAccessData(){
RpcResponse<List<RopAppAccessDTO>> response = ropAppAccessRemote.getAllAppAccessService();
if(response.hasResult()){
List<RopAppAccessDTO> ropAppAccessList = response.getResult();
ropAppAccessList.stream().filter(appAccess -> RopAppStatusEnum.ON.getValue().equals(appAccess.getAppStatus())).forEach(new Consumer<RopAppAccessDTO>() {
@Override
public void accept(RopAppAccessDTO ropAppAccessDTO) {
initAppServiceData(ropAppAccessDTO);
}
});
}
}
private void initAppServiceData(RopAppAccessDTO ropAppAccessDTO){
availableAppKeySecretMap.put(ropAppAccessDTO.getAppKey(), ropAppAccessDTO.getAppSecret());
ConcurrentHashMap<String, Long> appServiceMap = new ConcurrentHashMap<>();
List<RopAppServiceDTO> ropAppServiceList = ropAppAccessDTO.getRopAppServiceList();
ropAppServiceList.stream().filter(appService -> RopServiceStatusEnum.ON.getValue().equals(appService.getServiceStatus())).forEach(new Consumer<RopAppServiceDTO>() {
@Override
public void accept(RopAppServiceDTO ropAppServiceDTO) {
appServiceMap.put(ropAppServiceDTO.getServiceMethod().concat(".").concat(ropAppServiceDTO.getServiceVersion()), ropAppServiceDTO.getId());
}
});
availableAppKeyServiceMap.put(ropAppAccessDTO.getAppKey(), appServiceMap);
}
/**
* 获取秘钥
* @param appKey
* @return
* @author Alvin
* @date 2020年1月6日 下午5:15:17
*/
public String getSecret(String appKey){
return availableAppKeySecretMap.get(appKey);
}
/**
* 判断服务是否授权
* @param appKey
* @param method
* @param version
* @return 授权服务ID
* @author Alvin
* @date 2020年1月6日 下午3:43:39
*/
public Long isGrantedService(String appKey, String method, String version){
if(StringUtils.isEmpty(appKey) || StringUtils.isEmpty(method) || StringUtils.isEmpty(version)){
logger.error("appKey:{} and method:{} and version:{} must not null", appKey, method, version);
return null;
}
ConcurrentHashMap<String, Long> appServiceMap = availableAppKeyServiceMap.get(appKey);
if(appServiceMap == null){
logger.debug("appkey not granted");
return null;
}
return appServiceMap.get(method.concat(".").concat(version));
}
/**
* 更新应用授权
* @param appAccessId
* @author Alvin
* @date 2020年1月6日 下午5:40:13
*/
public void updateAppAccess(Long appAccessId){
logger.info("update app access by id:{}", appAccessId);
RpcResponse<RopAppAccess> response = ropAppAccessRemote.getById(appAccessId);
if(response.hasResult()){
RopAppAccess ropAppAccess = response.getResult();
if(RopAppStatusEnum.ON.getValue().equals(ropAppAccess.getAppStatus())){
RpcResponse<RopAppAccessDTO> appAccessServiceResponse = ropAppAccessRemote.getAppAccessServiceByAppId(ropAppAccess.getAppId());
if(appAccessServiceResponse.hasResult()){
initAppServiceData(appAccessServiceResponse.getResult());
}
}else{
availableAppKeySecretMap.remove(ropAppAccess.getAppKey());
availableAppKeyServiceMap.remove(ropAppAccess.getAppKey());
}
}
logger.info("available appKey secret map:{}", availableAppKeySecretMap);
logger.info("available appKey service map:{}", availableAppKeyServiceMap);
}
/**
* 更新应用服务授权
* @param appServiceId
* @author Alvin
* @date 2020年1月6日 下午5:40:44
*/
public void updateAppService(Long appServiceId){
logger.info("update app service by id:{}", appServiceId);
RpcResponse<RopAppService> appServiceResponse = ropAppServiceRemote.getById(appServiceId);
if(!appServiceResponse.hasResult()){
return;
}
RopAppService ropAppService = appServiceResponse.getResult();
updateAppService(ropAppService);
}
/**
* 更新应用服务
* @param serviceId
* @author Alvin
* @date 2020年1月6日 下午5:40:52
*/
public void updateService(Long serviceId){
logger.info("update service by id:{}", serviceId);
RpcResponse<List<RopAppService>> appServiceResponse = ropAppServiceRemote.getByServiceId(serviceId);
if(!appServiceResponse.hasResult()){
return;
}
List<RopAppService> ropAppServiceList = appServiceResponse.getResult();
ropAppServiceList.forEach(new Consumer<RopAppService>() {
@Override
public void accept(RopAppService ropAppService) {
updateAppService(ropAppService);
}
});
}
private void updateAppService(RopAppService ropAppService){
RpcResponse<RopAppAccess> appAccessResponse = ropAppAccessRemote.getById(ropAppService.getAppAccessId());
RpcResponse<RopService> serviceResponse = ropServiceRemote.getById(ropAppService.getServiceId());
if(!appAccessResponse.hasResult() || !serviceResponse.hasResult()){
return;
}
RopAppAccess ropAppAccess = appAccessResponse.getResult();
RopService ropService = serviceResponse.getResult();
if(RopServiceStatusEnum.ON.getValue().equals(ropService.getServiceStatus()) && RopServiceStatusEnum.ON.getValue().equals(ropAppService.getServiceStatus())){
ConcurrentHashMap<String, Long> appServiceMap = availableAppKeyServiceMap.get(ropAppAccess.getAppKey());
if(appServiceMap == null){
appServiceMap = new ConcurrentHashMap<>();
}
appServiceMap.put(ropService.getServiceMethod().concat(".").concat(ropAppService.getServiceVersion()), ropAppService.getId());
}else{
ConcurrentHashMap<String, Long> appServiceMap = availableAppKeyServiceMap.get(ropAppAccess.getAppKey());
if(appServiceMap != null){
appServiceMap.remove(ropService.getServiceMethod().concat(".").concat(ropAppService.getServiceVersion()));
}
}
}
}