Skip to content

Commit 12b264b

Browse files
Msquittto陈潇文
andauthored
[app-platform] x86环境北向接口不需要传递鉴权信息 (#365)
* [app-platform] x86环境北向接口不需要传递鉴权信息 * [app-platform] 检视意见修改 * [app-platform] 检视意见修改 --------- Co-authored-by: 陈潇文 <[email protected]>
1 parent da0f1a7 commit 12b264b

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

common/plugins/apikey-auth-default/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@
2323
<artifactId>apikey-service</artifactId>
2424
<version>1.0.0-SNAPSHOT</version>
2525
</dependency>
26+
<dependency>
27+
<groupId>modelengine.fit.jade.service</groupId>
28+
<artifactId>authentication-service</artifactId>
29+
</dependency>
2630
<dependency>
2731
<groupId>org.fitframework</groupId>
2832
<artifactId>fit-api</artifactId>
2933
</dependency>
34+
<dependency>
35+
<groupId>org.fitframework.service</groupId>
36+
<artifactId>fit-http-classic</artifactId>
37+
</dependency>
3038
<dependency>
3139
<groupId>org.junit.jupiter</groupId>
3240
<artifactId>junit-jupiter</artifactId>

common/plugins/http-interceptor/src/main/java/modelengine/jade/common/filter/support/NorthFilter.java renamed to common/plugins/apikey-auth-default/src/main/java/modelengine/jade/apikey/filter/NorthFilter.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*/
66

7-
package modelengine.jade.common.filter.support;
7+
package modelengine.jade.apikey.filter;
88

99
import modelengine.fit.http.protocol.HttpResponseStatus;
1010
import modelengine.fit.http.server.HttpClassicServerRequest;
@@ -14,6 +14,7 @@
1414
import modelengine.fitframework.annotation.Component;
1515
import modelengine.fitframework.annotation.Order;
1616
import modelengine.fitframework.annotation.Scope;
17+
import modelengine.fitframework.annotation.Value;
1718
import modelengine.fitframework.inspection.Validation;
1819
import modelengine.fitframework.log.Logger;
1920
import modelengine.jade.apikey.ApikeyAuthService;
@@ -23,7 +24,6 @@
2324

2425
import java.util.Collections;
2526
import java.util.List;
26-
import java.util.Optional;
2727

2828
/**
2929
* 用于北向接口的过滤器类。
@@ -34,19 +34,23 @@
3434
@Component
3535
public class NorthFilter implements HttpServerFilter {
3636
private static final Logger log = Logger.get(NorthFilter.class);
37-
private static final String USER_NAME_PREFIX = "sys_api_";
38-
private static final int ME_SK_START_POS = 13;
39-
private static final int ME_SK_END_POS = 21;
4037

4138
private final ApikeyAuthService apikeyAuthService;
39+
private final String apikey;
40+
private final String userName;
4241

4342
/**
4443
* 用 apikey 鉴权服务 {@link ApikeyAuthService} 构造 {@link NorthFilter}。
4544
*
4645
* @param apikeyAuthService 表示 apikey 鉴权服务的 {@link ApikeyAuthService}。
46+
* @param apikey 表示默认 apikey 的 {@link String}。
47+
* @param userName 表示默认用户名的 {@link String}。
4748
*/
48-
public NorthFilter(ApikeyAuthService apikeyAuthService) {
49+
public NorthFilter(ApikeyAuthService apikeyAuthService, @Value("${apikey}") String apikey,
50+
@Value("${userName}") String userName) {
4951
this.apikeyAuthService = Validation.notNull(apikeyAuthService, "The auth service cannot be null.");
52+
this.apikey = apikey;
53+
this.userName = userName;
5054
}
5155

5256
@Override
@@ -72,20 +76,16 @@ public List<String> mismatchPatterns() {
7276
@Override
7377
public void doFilter(HttpClassicServerRequest request, HttpClassicServerResponse response,
7478
HttpServerFilterChain chain) {
75-
Optional<String> token = request.headers().first("Authorization");
76-
log.info("Received request with Authorization token.");
7779

78-
if (token.isEmpty() || !this.apikeyAuthService.authApikeyInfo(token.get())) {
80+
if (!this.apikeyAuthService.authApikeyInfo(this.apikey)) {
7981
// 认证失败,返回 401 错误
8082
response.statusCode(HttpResponseStatus.UNAUTHORIZED.statusCode());
8183
log.error("Authentication failed: Token is null or invalid.");
8284
response.send();
8385
return;
8486
}
8587

86-
String userName = this.generateUniqueNameForApiKey(token.get());
87-
88-
UserContext operationContext = new UserContext(userName,
88+
UserContext operationContext = new UserContext(this.userName,
8989
HttpRequestUtils.getUserIp(request),
9090
HttpRequestUtils.getAcceptLanguages(request));
9191
UserContextHolder.apply(operationContext, () -> chain.doFilter(request, response));
@@ -95,8 +95,4 @@ public void doFilter(HttpClassicServerRequest request, HttpClassicServerResponse
9595
public Scope scope() {
9696
return Scope.GLOBAL;
9797
}
98-
99-
private String generateUniqueNameForApiKey(String apiKey) {
100-
return USER_NAME_PREFIX + apiKey.substring(ME_SK_START_POS, ME_SK_END_POS);
101-
}
10298
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
fit:
22
beans:
33
packages:
4-
- 'modelengine.jade.apikey.impl'
4+
- 'modelengine.jade.apikey'
5+
6+
apikey: 'Jade'
7+
userName: 'Jade'

0 commit comments

Comments
 (0)