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
99import modelengine .fit .http .protocol .HttpResponseStatus ;
1010import modelengine .fit .http .server .HttpClassicServerRequest ;
1414import modelengine .fitframework .annotation .Component ;
1515import modelengine .fitframework .annotation .Order ;
1616import modelengine .fitframework .annotation .Scope ;
17+ import modelengine .fitframework .annotation .Value ;
1718import modelengine .fitframework .inspection .Validation ;
1819import modelengine .fitframework .log .Logger ;
1920import modelengine .jade .apikey .ApikeyAuthService ;
2324
2425import java .util .Collections ;
2526import java .util .List ;
26- import java .util .Optional ;
2727
2828/**
2929 * 用于北向接口的过滤器类。
3434@ Component
3535public 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}
0 commit comments