34
34
import jakarta .servlet .http .HttpServletRequest ;
35
35
import jakarta .servlet .http .HttpServletResponse ;
36
36
import java .util .List ;
37
+ import java .util .Objects ;
37
38
import java .util .function .Supplier ;
38
39
import org .apache .commons .lang3 .StringUtils ;
39
40
import org .springframework .context .annotation .Bean ;
@@ -98,6 +99,15 @@ public SecurityConfiguration(
98
99
this .security = applicationProperties .getSecurity ();
99
100
}
100
101
102
+ /**
103
+ * Configures the security filter chain for the actuator endpoints, specifying authorization rules, authentication
104
+ * mechanisms, and exception handling.
105
+ *
106
+ * @param http the {@link HttpSecurity} object used to customize security settings for the actuator endpoints
107
+ * @param mvc a {@link PathPatternRequestMatcher.Builder} used to create matchers for specific URI patterns
108
+ * @return a configured {@link SecurityFilterChain} to handle security for actuator endpoints
109
+ * @throws Exception if an error occurs while configuring the security filter chain
110
+ */
101
111
@ Bean
102
112
@ Order (2 )
103
113
public SecurityFilterChain actuatorFilterChain (
@@ -135,6 +145,32 @@ public SecurityFilterChain actuatorFilterChain(
135
145
return http .build ();
136
146
}
137
147
148
+ /**
149
+ * Configures the security filter chain for API endpoints and GraphQL requests, defining authorization,
150
+ * authentication, CSRF settings, and headers for securing requests.
151
+ *
152
+ * @param http the {@link HttpSecurity} object used to customize the security settings
153
+ * for the application.
154
+ * @param mvc a {@link PathPatternRequestMatcher.Builder} used to build request
155
+ * matchers.
156
+ * @param authenticationProviderContributors a list of {@link AuthenticationProviderContributor} instances
157
+ * contributing custom {@link AuthenticationProvider}s to handle
158
+ * authentication.
159
+ * @param authorizeHttpRequestContributors a list of {@link AuthorizeHttpRequestContributor} instances providing
160
+ * paths to be configured as permit-all in the API security configuration.
161
+ * @param csrfContributors a list of {@link CsrfContributor} instances contributing request
162
+ * matchers to be ignored for CSRF protection.
163
+ * @param environment the {@link Environment} object used to retrieve profiles and
164
+ * environment properties.
165
+ * @param filterAfterContributors a list of {@link FilterAfterContributor} instances allowing additional
166
+ * filters to be added after default filters in the chain.
167
+ * @param filterBeforeContributors a list of {@link FilterBeforeContributor} instances allowing additional
168
+ * filters to be added before default filters in the chain.
169
+ * @param spaWebFilterContributors a list of {@link SpaWebFilterContributor} instances contributing to the
170
+ * customization of SPA-specific filters.
171
+ * @return a configured {@link SecurityFilterChain} for securing API and GraphQL endpoints.
172
+ * @throws Exception if an error occurs while configuring the security filter chain.
173
+ */
138
174
@ Bean
139
175
@ Order (3 )
140
176
public SecurityFilterChain apiFilterChain (
@@ -224,6 +260,22 @@ public SecurityFilterChain apiFilterChain(
224
260
return http .build ();
225
261
}
226
262
263
+ /**
264
+ * Configures the security filter chain for the web application, defining authorization, authentication, and the
265
+ * integration of SPA-specific and permit-all contributors.
266
+ *
267
+ * @param http the {@link HttpSecurity} object used to customize security settings for
268
+ * the application
269
+ * @param mvc a {@link PathPatternRequestMatcher.Builder} used to create request
270
+ * matchers for specific URI patterns
271
+ * @param authorizeHttpRequestContributors a list of {@link AuthorizeHttpRequestContributor} instances providing
272
+ * paths to be configured as permit-all in the security configuration
273
+ *
274
+ * @param spaWebFilterContributors a list of {@link SpaWebFilterContributor} instances contributing to the
275
+ * customization of SPA-specific filters
276
+ * @return a configured {@link SecurityFilterChain} for managing security in the application
277
+ * @throws Exception if an error occurs while configuring the security filter chain
278
+ */
227
279
@ Bean
228
280
@ Order (4 )
229
281
public SecurityFilterChain filterChain (
@@ -268,6 +320,15 @@ public SecurityFilterChain filterChain(
268
320
return http .build ();
269
321
}
270
322
323
+ /**
324
+ * Configures the security filter chain for GraphQL and GraphiQL endpoints in the development profile, defining
325
+ * authorization rules, authentication mechanisms, and exception handling.
326
+ *
327
+ * @param http the {@link HttpSecurity} object used to customize security settings for the GraphQL endpoints
328
+ * @param mvc a {@link PathPatternRequestMatcher.Builder} used to create request matchers for specific URI patterns
329
+ * @return a configured {@link SecurityFilterChain} for securing GraphQL and GraphiQL endpoints
330
+ * @throws Exception if an error occurs while configuring the security filter chain
331
+ */
271
332
@ Bean
272
333
@ Profile ("dev" )
273
334
@ Order (1 )
@@ -328,6 +389,12 @@ private String getRememberMeKey() {
328
389
return rememberMe .getKey ();
329
390
}
330
391
392
+ /**
393
+ * A configuration class for adding custom filters to the security filter chain after specified filters. This class
394
+ * allows customization of the filter chain by applying a list of {@link FilterAfterContributor} instances.
395
+ *
396
+ * @param <H> the type of {@link HttpSecurityBuilder} used for configuring the security filter chain
397
+ */
331
398
private static class FilterAfterContributorConfigurer <H extends HttpSecurityBuilder <HttpSecurity >>
332
399
extends AbstractHttpConfigurer <FilterBeforeContributorConfigurer <H >, HttpSecurity > {
333
400
@@ -347,6 +414,13 @@ public void configure(HttpSecurity http) {
347
414
}
348
415
}
349
416
417
+ /**
418
+ * A private configuration class for adding and positioning filters in the web security filter chain before a
419
+ * specific set of filters. This configurer uses a list of {@link FilterBeforeContributor} instances to determine
420
+ * which filters should be introduced into the chain and their corresponding positions.
421
+ *
422
+ * @param <H> the type of {@link HttpSecurityBuilder} used to configure the web security filter chain.
423
+ */
350
424
private static class FilterBeforeContributorConfigurer <H extends HttpSecurityBuilder <HttpSecurity >>
351
425
extends AbstractHttpConfigurer <FilterBeforeContributorConfigurer <H >, HttpSecurity > {
352
426
@@ -410,6 +484,21 @@ public String resolveCsrfTokenValue(HttpServletRequest request, CsrfToken csrfTo
410
484
}
411
485
}
412
486
487
+ /**
488
+ * A custom implementation of {@link BasicAuthenticationEntryPoint} used to handle unauthorized access attempts when
489
+ * basic authentication is required.
490
+ *
491
+ * This class extends the default functionality of {@link BasicAuthenticationEntryPoint} to customize the behavior
492
+ * for responding to unauthorized requests. It specifically defines the response headers and status code returned to
493
+ * the client upon an authentication failure.
494
+ *
495
+ * Key functionality: - Sets the "WWW-Authenticate" response header to indicate the required basic authentication
496
+ * with a realm. - Responds with the HTTP 401 (Unauthorized) status code to indicate that the request requires
497
+ * authentication.
498
+ *
499
+ * Method: {@link #commence(HttpServletRequest, HttpServletResponse, AuthenticationException)}: - Handles the
500
+ * response when an {@link AuthenticationException} occurs, customizing the headers and status code.
501
+ */
413
502
private static class UnauthorizedBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {
414
503
415
504
@ Override
0 commit comments