1616
1717package org .springaicommunity .mcp .security .server .config ;
1818
19+ import org .jspecify .annotations .Nullable ;
1920import org .springaicommunity .mcp .security .server .apikey .ApiKeyEntityRepository ;
2021import org .springaicommunity .mcp .security .server .apikey .authentication .ApiKeyAuthenticationProvider ;
2122import org .springaicommunity .mcp .security .server .apikey .web .ApiKeyAuthenticationConverter ;
3435 */
3536public 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 ;
0 commit comments