Skip to content

Commit 0d7b1bf

Browse files
committed
mcp-server: polish McpApiKeyConfigurer
Signed-off-by: Daniel Garnier-Moiroux <[email protected]>
1 parent 7e0f2b8 commit 0d7b1bf

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

mcp-server-security/src/main/java/org/springaicommunity/mcp/security/server/config/McpApiKeyConfigurer.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springaicommunity.mcp.security.server.config;
1818

19+
import org.jspecify.annotations.Nullable;
1920
import org.springaicommunity.mcp.security.server.apikey.ApiKeyEntityRepository;
2021
import org.springaicommunity.mcp.security.server.apikey.authentication.ApiKeyAuthenticationProvider;
2122
import org.springaicommunity.mcp.security.server.apikey.web.ApiKeyAuthenticationConverter;
@@ -34,14 +35,15 @@
3435
*/
3536
public class McpApiKeyConfigurer extends AbstractHttpConfigurer<McpApiKeyConfigurer, HttpSecurity> {
3637

37-
private ApiKeyEntityRepository<?> apiKeyEntityRepository;
38+
private @Nullable ApiKeyEntityRepository<?> apiKeyEntityRepository;
3839

39-
private String headerName;
40+
private @Nullable String headerName;
4041

41-
private AuthenticationConverter authenticationConverter;
42+
private @Nullable AuthenticationConverter authenticationConverter;
4243

4344
@Override
4445
public void init(HttpSecurity http) throws Exception {
46+
Assert.notNull(this.apiKeyEntityRepository, "apiKeyRepository cannot be null");
4547
http.authenticationProvider(new ApiKeyAuthenticationProvider<>(this.apiKeyEntityRepository))
4648
// TODO: improve matcher to check for API key
4749
.csrf(csrf -> csrf.ignoringRequestMatchers("/mcp"));
@@ -54,9 +56,7 @@ public void configure(HttpSecurity http) throws Exception {
5456
var authManager = http.getSharedObject(AuthenticationManager.class);
5557

5658
var authenticationConverter = getAuthenticationConverter();
57-
var filter = authenticationConverter != null
58-
? new ApiKeyAuthenticationFilter(authManager, authenticationConverter)
59-
: new ApiKeyAuthenticationFilter(authManager);
59+
var filter = new ApiKeyAuthenticationFilter(authManager, authenticationConverter);
6060
http.addFilterBefore(filter, BasicAuthenticationFilter.class);
6161
}
6262

@@ -67,7 +67,7 @@ private AuthenticationConverter getAuthenticationConverter() {
6767
if (StringUtils.hasText(this.headerName)) {
6868
return new ApiKeyAuthenticationConverter(this.headerName);
6969
}
70-
return null;
70+
return new ApiKeyAuthenticationConverter();
7171
}
7272

7373
/**
@@ -82,7 +82,8 @@ public McpApiKeyConfigurer apiKeyRepository(ApiKeyEntityRepository<?> apiKeyEnti
8282
* The name of the header from which to extract the API key. Defaults to
8383
* {@link org.springaicommunity.mcp.security.server.apikey.web.ApiKeyAuthenticationFilter#DEFAULT_API_KEY_HEADER}.
8484
* <p>
85-
* Overrides the value from {@link #authenticationConverter(AuthenticationConverter)}.
85+
* If {@link #authenticationConverter(AuthenticationConverter)} is set, then this is
86+
* ignored.
8687
*/
8788
public McpApiKeyConfigurer headerName(String headerName) {
8889
this.headerName = headerName;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2025-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
@NullMarked
18+
package org.springaicommunity.mcp.security.server.config;
19+
20+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)