Skip to content

Commit 6aa98f8

Browse files
authored
Merge pull request #40 from CJP2004/dev
Dev
2 parents 8376bdf + 6ae169c commit 6aa98f8

1 file changed

Lines changed: 96 additions & 66 deletions

File tree

auth-service/src/main/java/com/servicegovernance/auth/service/impl/AuthorizationServiceImpl.java

Lines changed: 96 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,36 @@
3737
@Service
3838
public class AuthorizationServiceImpl implements AuthorizationService {
3939

40+
/**
41+
* 前端交互和内部 Map 使用的字段名。
42+
*/
43+
private static final String KEY_CALLER_APP_ID = "callerAppId";
44+
private static final String KEY_CALLEE_APP_ID = "calleeAppId";
45+
private static final String KEY_CURRENT_APIS = "currentApis";
46+
private static final String KEY_LEGACY_APIS = "legacyApis";
47+
private static final String KEY_CALLEE_CURRENT_VERSION = "calleeCurrentVersion";
48+
private static final String KEY_CURRENT_API_COUNT = "currentApiCount";
49+
private static final String KEY_LEGACY_API_COUNT = "legacyApiCount";
50+
private static final String KEY_CHECKED_API_IDS = "checkedApiIds";
51+
private static final String KEY_CALLER_APP_CODE = "callerAppCode";
52+
private static final String KEY_CALLEE_APP_CODE = "calleeAppCode";
53+
private static final String KEY_API_PATHS = "apiPaths";
54+
private static final String KEY_PAGE_NUM = "pageNum";
55+
private static final String KEY_PAGE_SIZE = "pageSize";
56+
private static final String KEY_VERSION = "version";
57+
58+
/**
59+
* Mapper 返回结果中数据库字段名对应的 Map key。
60+
*/
61+
private static final String COLUMN_CALLER_APP_ID = "caller_app_id";
62+
private static final String COLUMN_CALLEE_APP_ID = "callee_app_id";
63+
private static final String COLUMN_CALLER_APP_CODE = "caller_app_code";
64+
private static final String COLUMN_CALLEE_APP_CODE = "callee_app_code";
65+
private static final String COLUMN_API_ID = "api_id";
66+
private static final String COLUMN_APP_ID = "app_id";
67+
private static final String COLUMN_CALLEE_CURRENT_VERSION = "callee_current_version";
68+
private static final String COLUMN_API_PATH = "api_path";
69+
4070
private final AuthorizationMapper mapper;
4171

4272
public AuthorizationServiceImpl(AuthorizationMapper mapper) {
@@ -52,24 +82,24 @@ public Map<String, Object> singleAppList(Map<String, Object> query) {
5282

5383
@Override
5484
public Map<String, Object> singleAppDetail(Map<String, Object> payload) {
55-
Long callerAppId = longValue(payload.get("callerAppId"));
56-
Long calleeAppId = longValue(payload.get("calleeAppId"));
85+
Long callerAppId = longValue(payload.get(KEY_CALLER_APP_ID));
86+
Long calleeAppId = longValue(payload.get(KEY_CALLEE_APP_ID));
5787
if (callerAppId == null || calleeAppId == null) {
5888
throw new BusinessException(40001, "callerAppId/calleeAppId不能为空");
5989
}
6090
Map<String, Object> pair = MapKeyUtil.camelKeys(mapper.selectPairApps(callerAppId, calleeAppId));
6191
String currentVersion = mapper.selectCurrentVersion(calleeAppId);
6292
Map<String, Object> data = editorData(calleeAppId, callerAppId);
6393
@SuppressWarnings("unchecked")
64-
List<Map<String, Object>> currentApis = (List<Map<String, Object>>) data.get("currentApis");
94+
List<Map<String, Object>> currentApis = (List<Map<String, Object>>) data.get(KEY_CURRENT_APIS);
6595
@SuppressWarnings("unchecked")
66-
List<Map<String, Object>> legacyApis = (List<Map<String, Object>>) data.get("legacyApis");
67-
pair.put("calleeCurrentVersion", StringUtils.hasText(currentVersion) ? currentVersion : "-");
68-
pair.put("currentApiCount", currentApis.size());
69-
pair.put("legacyApiCount", legacyApis.size());
70-
pair.put("currentApis", currentApis);
71-
pair.put("legacyApis", legacyApis);
72-
pair.put("checkedApiIds", data.get("checkedApiIds"));
96+
List<Map<String, Object>> legacyApis = (List<Map<String, Object>>) data.get(KEY_LEGACY_APIS);
97+
pair.put(KEY_CALLEE_CURRENT_VERSION, StringUtils.hasText(currentVersion) ? currentVersion : "-");
98+
pair.put(KEY_CURRENT_API_COUNT, currentApis.size());
99+
pair.put(KEY_LEGACY_API_COUNT, legacyApis.size());
100+
pair.put(KEY_CURRENT_APIS, currentApis);
101+
pair.put(KEY_LEGACY_APIS, legacyApis);
102+
pair.put(KEY_CHECKED_API_IDS, data.get(KEY_CHECKED_API_IDS));
73103
return pair;
74104
}
75105

@@ -81,14 +111,14 @@ public Map<String, Object> singleAppDetail(Map<String, Object> payload) {
81111
@Override
82112
@Transactional(rollbackFor = Exception.class)
83113
public void saveSingleApp(Map<String, Object> payload) {
84-
Long callerAppId = longValue(payload.get("callerAppId"));
85-
Long calleeAppId = longValue(payload.get("calleeAppId"));
114+
Long callerAppId = longValue(payload.get(KEY_CALLER_APP_ID));
115+
Long calleeAppId = longValue(payload.get(KEY_CALLEE_APP_ID));
86116
if (callerAppId == null || calleeAppId == null) {
87-
callerAppId = appId(required(payload, "caller_app_code", "callerAppCode"));
88-
calleeAppId = appId(required(payload, "callee_app_code", "calleeAppCode"));
117+
callerAppId = appId(required(payload, COLUMN_CALLER_APP_CODE, KEY_CALLER_APP_CODE));
118+
calleeAppId = appId(required(payload, COLUMN_CALLEE_APP_CODE, KEY_CALLEE_APP_CODE));
89119
}
90120
List<Long> apiIds = longList(payload.getOrDefault("apiIds",
91-
payload.getOrDefault("checkedApiIds", payload.get("checked_api_ids"))));
121+
payload.getOrDefault(KEY_CHECKED_API_IDS, payload.get("checked_api_ids"))));
92122
List<Map<String, Object>> oldDetails = mapper.selectActiveAuthLogDetails(callerAppId, calleeAppId);
93123
Set<Long> oldApiIds = apiIdSet(oldDetails);
94124
Set<Long> newApiIds = new HashSet<>(apiIds);
@@ -97,7 +127,7 @@ public void saveSingleApp(Map<String, Object> payload) {
97127
mapper.insertAuth(callerAppId, calleeAppId, apiId);
98128
}
99129
for (Map<String, Object> detail : oldDetails) {
100-
if (!newApiIds.contains(longValue(MapKeyUtil.lowerKeys(detail).get("api_id")))) {
130+
if (!newApiIds.contains(longValue(MapKeyUtil.lowerKeys(detail).get(COLUMN_API_ID)))) {
101131
insertAuthLog(detail, 1);
102132
}
103133
}
@@ -151,7 +181,7 @@ public void saveReverse(Map<String, Object> payload) {
151181
}
152182
Long calleeAppId = appId(required(selectedApis.get(0), "appCode", "app_code"));
153183
List<Long> apiIds = selectedApis.stream()
154-
.map(item -> longValue(item.getOrDefault("apiId", item.getOrDefault("id", item.get("api_id")))))
184+
.map(item -> longValue(item.getOrDefault("apiId", item.getOrDefault("id", item.get(COLUMN_API_ID)))))
155185
.filter(Objects::nonNull)
156186
.toList();
157187
for (String appCode : checkedAppCodes) {
@@ -179,14 +209,14 @@ public void saveReverse(Map<String, Object> payload) {
179209

180210
@Override
181211
public Map<String, Object> check(Map<String, Object> payload) {
182-
Map<String, Object> caller = appCredential(required(payload, "callerAppCode"));
183-
Map<String, Object> callee = appCredential(required(payload, "calleeAppCode"));
212+
Map<String, Object> caller = appCredential(required(payload, KEY_CALLER_APP_CODE));
213+
Map<String, Object> callee = appCredential(required(payload, KEY_CALLEE_APP_CODE));
184214
boolean callerValid = credentialValid(caller, required(payload, "callerPwd"));
185215
boolean calleeValid = credentialValid(callee, required(payload, "calleePwd"));
186216
String reason = "应用身份错误或无调用权限";
187217
int result = 1;
188218
if (callerValid && calleeValid) {
189-
Long count = mapper.countAnyActiveAuth(longValue(caller.get("app_id")), longValue(callee.get("app_id")));
219+
Long count = mapper.countAnyActiveAuth(longValue(caller.get(COLUMN_APP_ID)), longValue(callee.get(COLUMN_APP_ID)));
190220
if (count != null && count > 0) {
191221
result = 0;
192222
reason = "调用关系校验通过";
@@ -198,22 +228,22 @@ public Map<String, Object> check(Map<String, Object> payload) {
198228

199229
@Override
200230
public Map<String, Object> resourceList(String authorization, Map<String, Object> payload) {
201-
String callerAppCode = required(payload, "callerAppCode");
202-
String calleeAppCode = required(payload, "calleeAppCode");
231+
String callerAppCode = required(payload, KEY_CALLER_APP_CODE);
232+
String calleeAppCode = required(payload, KEY_CALLEE_APP_CODE);
203233
Long callerAppId = appId(callerAppCode);
204234
Map<String, Object> rawCallee = mapper.selectAppCredentialByCode(calleeAppCode);
205235
if (rawCallee == null) {
206236
throw new BusinessException(40001, "权限资源查询失败");
207237
}
208238
Map<String, Object> callee = MapKeyUtil.lowerKeys(rawCallee);
209-
Long calleeAppId = longValue(callee.get("app_id"));
239+
Long calleeAppId = longValue(callee.get(COLUMN_APP_ID));
210240
String token = authorization == null ? "" : authorization.replaceFirst("(?i)^Basic\\s+", "").trim();
211241
String pwd1 = text(callee.get("app_pwd1"));
212242
String pwd2 = text(callee.get("app_pwd2"));
213243
if (!matchesBasicPassword(token, pwd1) && !matchesBasicPassword(token, pwd2)) {
214244
throw new BusinessException(40001, "权限资源查询失败");
215245
}
216-
return Map.of("version", value(mapper.selectResourceVersion(calleeAppId)),
246+
return Map.of(KEY_VERSION, value(mapper.selectResourceVersion(calleeAppId)),
217247
"urlList", mapper.selectAuthorizedUrls(callerAppId, calleeAppId));
218248
}
219249

@@ -227,10 +257,10 @@ public void exportExcel(Map<String, Object> query, HttpServletResponse response)
227257
Map<String, int[]> pairApiCounts = new LinkedHashMap<>();
228258
for (Map<String, Object> raw : allRows) {
229259
Map<String, Object> row = MapKeyUtil.lowerKeys(raw);
230-
String key = row.get("caller_app_id") + "->" + row.get("callee_app_id");
260+
String key = row.get(COLUMN_CALLER_APP_ID) + "->" + row.get(COLUMN_CALLEE_APP_ID);
231261
int[] counts = pairApiCounts.computeIfAbsent(key, unused -> new int[2]);
232-
String version = text(row.get("version"));
233-
String currentVersion = text(row.get("callee_current_version"));
262+
String version = text(row.get(KEY_VERSION));
263+
String currentVersion = text(row.get(COLUMN_CALLEE_CURRENT_VERSION));
234264
// counts[0] = 当前版本 API 数量, counts[1] = 废弃版本 API 数量
235265
if (Objects.equals(version, currentVersion) || "-".equals(currentVersion)) {
236266
counts[0]++;
@@ -291,25 +321,25 @@ public void exportExcel(Map<String, Object> query, HttpServletResponse response)
291321
int rowIndex = 1;
292322
for (Map<String, Object> raw : allRows) {
293323
Map<String, Object> row = MapKeyUtil.lowerKeys(raw);
294-
String pairKey = row.get("caller_app_id") + "->" + row.get("callee_app_id");
324+
String pairKey = row.get(COLUMN_CALLER_APP_ID) + "->" + row.get(COLUMN_CALLEE_APP_ID);
295325
int[] counts = pairApiCounts.getOrDefault(pairKey, new int[]{0, 0});
296-
String version = text(row.get("version"));
297-
String currentVersion = text(row.get("callee_current_version"));
326+
String version = text(row.get(KEY_VERSION));
327+
String currentVersion = text(row.get(COLUMN_CALLEE_CURRENT_VERSION));
298328
// 判断版本类型:与当前版本一致则为"当前版本",否则为"兼容旧版本"
299329
String versionType = (Objects.equals(version, currentVersion) || "-".equals(currentVersion))
300330
? "当前版本" : "兼容旧版本";
301331

302332
Row dataRow = sheet.createRow(rowIndex++);
303333
String[] values = {
304-
text(row.get("caller_app_code")),
334+
text(row.get(COLUMN_CALLER_APP_CODE)),
305335
text(row.get("caller_app_name")),
306-
text(row.get("callee_app_code")),
336+
text(row.get(COLUMN_CALLEE_APP_CODE)),
307337
text(row.get("callee_app_name")),
308338
currentVersion,
309339
String.valueOf(counts[0]),
310340
String.valueOf(counts[1]),
311341
text(row.get("api_name")),
312-
text(row.get("api_path")),
342+
text(row.get(COLUMN_API_PATH)),
313343
version,
314344
versionType
315345
};
@@ -338,52 +368,52 @@ public void exportExcel(Map<String, Object> query, HttpServletResponse response)
338368
private Map<String, Object> editorData(Long calleeAppId, Long callerAppId) {
339369
List<Long> checkedIds = callerAppId == null ? List.of() : mapper.selectCheckedApiIds(callerAppId, calleeAppId);
340370
String currentVersion = mapper.selectCurrentVersion(calleeAppId);
341-
return Map.of("currentApis", MapKeyUtil.camelKeys(mapper.selectCurrentApis(calleeAppId, currentVersion)),
342-
"legacyApis", MapKeyUtil.camelKeys(mapper.selectLegacyApis(calleeAppId, currentVersion)),
343-
"checkedApiIds", checkedIds);
371+
return Map.of(KEY_CURRENT_APIS, MapKeyUtil.camelKeys(mapper.selectCurrentApis(calleeAppId, currentVersion)),
372+
KEY_LEGACY_APIS, MapKeyUtil.camelKeys(mapper.selectLegacyApis(calleeAppId, currentVersion)),
373+
KEY_CHECKED_API_IDS, checkedIds);
344374
}
345375

346376
private Map<String, Object> currentAuthorization(Long callerAppId, Long calleeAppId) {
347377
Map<String, Object> pair = new LinkedHashMap<>(MapKeyUtil.camelKeys(mapper.selectPairApps(callerAppId, calleeAppId)));
348378
pair.put("id", callerAppId * 1000000 + calleeAppId);
349-
pair.put("apiPaths", mapper.selectPairApiPaths(callerAppId, calleeAppId));
379+
pair.put(KEY_API_PATHS, mapper.selectPairApiPaths(callerAppId, calleeAppId));
350380
return pair;
351381
}
352382

353383
private List<Map<String, Object>> groupAuthRows(List<Map<String, Object>> rows) {
354384
Map<String, Map<String, Object>> grouped = new LinkedHashMap<>();
355385
for (Map<String, Object> raw : rows) {
356386
Map<String, Object> row = MapKeyUtil.lowerKeys(raw);
357-
String key = row.get("caller_app_id") + "->" + row.get("callee_app_id");
387+
String key = row.get(COLUMN_CALLER_APP_ID) + "->" + row.get(COLUMN_CALLEE_APP_ID);
358388
Map<String, Object> target = grouped.computeIfAbsent(key, unused -> {
359389
Map<String, Object> item = new LinkedHashMap<>();
360-
Long callerAppId = longValue(row.get("caller_app_id"));
361-
Long calleeAppId = longValue(row.get("callee_app_id"));
390+
Long callerAppId = longValue(row.get(COLUMN_CALLER_APP_ID));
391+
Long calleeAppId = longValue(row.get(COLUMN_CALLEE_APP_ID));
362392
item.put("id", callerAppId * 1000000 + calleeAppId);
363-
item.put("callerAppId", callerAppId);
364-
item.put("calleeAppId", calleeAppId);
365-
item.put("callerAppCode", row.get("caller_app_code"));
393+
item.put(KEY_CALLER_APP_ID, callerAppId);
394+
item.put(KEY_CALLEE_APP_ID, calleeAppId);
395+
item.put(KEY_CALLER_APP_CODE, row.get(COLUMN_CALLER_APP_CODE));
366396
item.put("callerAppName", row.get("caller_app_name"));
367-
item.put("calleeAppCode", row.get("callee_app_code"));
397+
item.put(KEY_CALLEE_APP_CODE, row.get(COLUMN_CALLEE_APP_CODE));
368398
item.put("calleeAppName", row.get("callee_app_name"));
369-
String version = text(row.get("callee_current_version"));
370-
item.put("calleeCurrentVersion", StringUtils.hasText(version) ? version : "-");
371-
item.put("apiPaths", new ArrayList<String>());
399+
String version = text(row.get(COLUMN_CALLEE_CURRENT_VERSION));
400+
item.put(KEY_CALLEE_CURRENT_VERSION, StringUtils.hasText(version) ? version : "-");
401+
item.put(KEY_API_PATHS, new ArrayList<String>());
372402
item.put("apiRows", new ArrayList<Map<String, Object>>());
373-
item.put("currentApiCount", 0);
374-
item.put("legacyApiCount", 0);
403+
item.put(KEY_CURRENT_API_COUNT, 0);
404+
item.put(KEY_LEGACY_API_COUNT, 0);
375405
return item;
376406
});
377407
@SuppressWarnings("unchecked")
378-
List<String> paths = (List<String>) target.get("apiPaths");
408+
List<String> paths = (List<String>) target.get(KEY_API_PATHS);
379409
@SuppressWarnings("unchecked")
380410
List<Map<String, Object>> apiRows = (List<Map<String, Object>>) target.get("apiRows");
381-
paths.add(text(row.get("api_path")));
382-
apiRows.add(Map.of("apiName", row.get("api_name"), "apiPath", row.get("api_path"), "version", row.get("version")));
383-
if (Objects.equals(text(row.get("version")), text(target.get("calleeCurrentVersion")))) {
384-
target.put("currentApiCount", intValue(target.get("currentApiCount"), 0) + 1);
411+
paths.add(text(row.get(COLUMN_API_PATH)));
412+
apiRows.add(Map.of("apiName", row.get("api_name"), "apiPath", row.get(COLUMN_API_PATH), KEY_VERSION, row.get(KEY_VERSION)));
413+
if (Objects.equals(text(row.get(KEY_VERSION)), text(target.get(KEY_CALLEE_CURRENT_VERSION)))) {
414+
target.put(KEY_CURRENT_API_COUNT, intValue(target.get(KEY_CURRENT_API_COUNT), 0) + 1);
385415
} else {
386-
target.put("legacyApiCount", intValue(target.get("legacyApiCount"), 0) + 1);
416+
target.put(KEY_LEGACY_API_COUNT, intValue(target.get(KEY_LEGACY_API_COUNT), 0) + 1);
387417
}
388418
}
389419
return new ArrayList<>(grouped.values());
@@ -409,7 +439,7 @@ private Long appId(String appCode) {
409439

410440
private Long validatedAppId(String appCode, String password) {
411441
Map<String, Object> app = appCredential(appCode);
412-
return credentialValid(app, password) ? longValue(app.get("app_id")) : null;
442+
return credentialValid(app, password) ? longValue(app.get(COLUMN_APP_ID)) : null;
413443
}
414444

415445
private Map<String, Object> appCredential(String appCode) {
@@ -418,7 +448,7 @@ private Map<String, Object> appCredential(String appCode) {
418448
}
419449

420450
private boolean credentialValid(Map<String, Object> app, String password) {
421-
return app.get("app_id") != null
451+
return app.get(COLUMN_APP_ID) != null
422452
&& (Objects.equals(password, text(app.get("app_pwd1")))
423453
|| Objects.equals(password, text(app.get("app_pwd2"))));
424454
}
@@ -437,7 +467,7 @@ private void insertCallDecisionLog(Map<String, Object> caller, Map<String, Objec
437467
private static Set<Long> apiIdSet(List<Map<String, Object>> details) {
438468
Set<Long> ids = new HashSet<>();
439469
for (Map<String, Object> detail : details) {
440-
Long apiId = longValue(MapKeyUtil.lowerKeys(detail).get("api_id"));
470+
Long apiId = longValue(MapKeyUtil.lowerKeys(detail).get(COLUMN_API_ID));
441471
if (apiId != null) {
442472
ids.add(apiId);
443473
}
@@ -446,8 +476,8 @@ private static Set<Long> apiIdSet(List<Map<String, Object>> details) {
446476
}
447477

448478
private static Map<String, Object> page(List<Map<String, Object>> records, Map<String, Object> query) {
449-
int pageNum = intValue(query.getOrDefault("pageNum", query.get("page")), 1);
450-
int pageSize = intValue(query.get("pageSize"), 10);
479+
int pageNum = intValue(query.getOrDefault(KEY_PAGE_NUM, query.get("page")), 1);
480+
int pageSize = intValue(query.get(KEY_PAGE_SIZE), 10);
451481
int from = Math.max(0, (pageNum - 1) * pageSize);
452482
int to = Math.min(records.size(), from + pageSize);
453483
List<Map<String, Object>> pageRecords = from >= records.size() ? List.of() : records.subList(from, to);
@@ -456,17 +486,17 @@ private static Map<String, Object> page(List<Map<String, Object>> records, Map<S
456486

457487
private static Map<String, Object> pageResult(List<Map<String, Object>> records, long total,
458488
Map<String, Object> query) {
459-
int pageNum = intValue(query.getOrDefault("pageNum", query.get("page")), 1);
460-
int pageSize = intValue(query.get("pageSize"), 10);
489+
int pageNum = intValue(query.getOrDefault(KEY_PAGE_NUM, query.get("page")), 1);
490+
int pageSize = intValue(query.get(KEY_PAGE_SIZE), 10);
461491
return Map.of("total", total, "current", pageNum, "size", pageSize, "records", records);
462492
}
463493

464494
private static Map<String, Object> pageQuery(Map<String, Object> query) {
465495
Map<String, Object> normalized = new LinkedHashMap<>(query == null ? Map.of() : query);
466-
int pageNum = intValue(normalized.getOrDefault("pageNum", normalized.get("page")), 1);
467-
int pageSize = intValue(normalized.get("pageSize"), 10);
468-
normalized.put("pageNum", pageNum);
469-
normalized.put("pageSize", pageSize);
496+
int pageNum = intValue(normalized.getOrDefault(KEY_PAGE_NUM, normalized.get("page")), 1);
497+
int pageSize = intValue(normalized.get(KEY_PAGE_SIZE), 10);
498+
normalized.put(KEY_PAGE_NUM, pageNum);
499+
normalized.put(KEY_PAGE_SIZE, pageSize);
470500
normalized.put("offset", Math.max(0, (pageNum - 1) * pageSize));
471501
return normalized;
472502
}

0 commit comments

Comments
 (0)