You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/modules/ROOT/pages/servlet/configuration/java.adoc
+94Lines changed: 94 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -250,6 +250,100 @@ public class MultiHttpSecurityConfig {
250
250
If the URL does not start with `/api/`, this configuration is used.
251
251
This configuration is considered after `apiFilterChain`, since it has an `@Order` value after `1` (no `@Order` defaults to last).
252
252
253
+
To effectively manage security in an application where certain areas and the entire app need protection, we can employ multiple filter chains alongside the securityMatcher. This approach allows us to define distinct security configurations tailored to specific parts while also ensuring overall application security. The provided example showcases distinct configurations for URLs starting with "/account/", "/balance", "/loans-approval/", "/credit-cards-approval/", "/loans", "/cards", "/notices", "/contact", "/login", "/logout" and "/register". This approach allows tailored security settings for specific endpoints, enhancing overall application security and control.
254
+
255
+
[source,java]
256
+
----
257
+
@Configuration
258
+
@EnableWebSecurity
259
+
public class CustomSecurityFilterChainConfig {
260
+
261
+
@Bean <1>
262
+
public UserDetailsService userDetailsService() {
263
+
// ensure the passwords are encoded properly
264
+
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
<2> Define a SecurityFilterChain instance with @Order(1), which means that this chain will have the highest priority.
332
+
<3> Specify that the http.securityMatcher applies only to "/account", "/loans", and "/cards" URLs.
333
+
<4> Requires the user to have the role "USER" to access the URLs "/account", "/loans", and "/cards".
334
+
<5> Next, create another SecurityFilterChain instance with @Order(2), this chain will be considered second.
335
+
<6> Indicate that the http.securityMatcher applies only to "/balance" URL.
336
+
<7> Requires the user to have the role "USER" or the role "ADMIN" to access the URL "/balance"
337
+
<8> Next, create another SecurityFilterChain instance with @Order(3), this particular security filter chain will be the third in the order of execution.
338
+
<9> The http.securityMatcher applies only to "/loans-approval" and "/credit-cards-approval" URLs.
339
+
<10> The user must have the role "ADMIN" to access the URLs "/loans-approval" and "/credit-cards-approval"
340
+
<11> Define a SecurityFilterChain instance with @Order(4) this chain will be considered fourth.
341
+
<12> The http.securityMatcher applies only to "/login", "/logout", "/notices", "/contact", "/register" URLs.
342
+
<13> Allows access to these specific URLs without authentication.
343
+
<14> Lastly, create an additional SecurityFilterChain instance without an @Order annotation. This configuration will handle requests not covered by the other filter chains and will be processed last (no @Order defaults to last).
344
+
<15> Requires the user to be authenticated to access any URL not explicitly allowed or protected by other filter chains.
0 commit comments