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
@@ -38,7 +38,7 @@ open fun filterChain(http: HttpSecurity): SecurityFilterChain {
38
38
[NOTE]
39
39
Make sure to import the `org.springframework.security.config.annotation.web.invoke` function to enable the Kotlin DSL in your class, as the IDE will not always auto-import the method, causing compilation issues.
40
40
41
-
The default configuration (shown in the preceding listing):
41
+
The default configuration (shown in the preceding example):
42
42
43
43
* Ensures that any request to our application requires the user to be authenticated
44
44
* Lets users authenticate with form-based login
@@ -55,12 +55,16 @@ Note that this configuration parallels the XML namespace configuration:
55
55
</http>
56
56
----
57
57
58
-
== Multiple HttpSecurity Instances
58
+
=== Multiple HttpSecurity Instances
59
59
60
-
We can configure multiple `HttpSecurity` instances, just as we can have multiple `<http>` blocks.
60
+
To effectively manage security in an application where certain areas need different protection, we can employ multiple filter chains alongside the `securityMatcher` DSL method.
61
+
This approach allows us to define distinct security configurations tailored to specific parts of the application, enhancing overall application security and control.
62
+
63
+
We can configure multiple `HttpSecurity` instances just as we can have multiple `<http>` blocks in XML.
61
64
The key is to register multiple `SecurityFilterChain` ``@Bean``s.
62
-
The following example has a different configuration for URLs that start with `/api/`:
65
+
The following example has a different configuration for URLs that begin with `/api/`:
open fun apiFilterChain(http: HttpSecurity): SecurityFilterChain {
83
87
http {
84
88
securityMatcher("/api/**") <3>
@@ -102,10 +106,243 @@ class MultiHttpSecurityConfig {
102
106
}
103
107
}
104
108
----
105
-
106
109
<1> Configure Authentication as usual.
107
110
<2> Create an instance of `SecurityFilterChain` that contains `@Order` to specify which `SecurityFilterChain` should be considered first.
108
-
<3> The `http.securityMatcher` states that this `HttpSecurity` is applicable only to URLs that start with `/api/`
111
+
<3> The `http.securityMatcher()` states that this `HttpSecurity` is applicable only to URLs that begin with `/api/`.
109
112
<4> Create another instance of `SecurityFilterChain`.
110
-
If the URL does not start with `/api/`, this configuration is used.
113
+
If the URL does not begin with `/api/`, this configuration is used.
111
114
This configuration is considered after `apiFilterChain`, since it has an `@Order` value after `1` (no `@Order` defaults to last).
115
+
116
+
=== Choosing `securityMatcher` or `requestMatchers`
117
+
118
+
A common question is:
119
+
120
+
> What is the difference between the `http.securityMatcher()` method and `requestMatchers()` used for request authorization (i.e. inside of `http.authorizeHttpRequests()`)?
121
+
122
+
To answer this question, it helps to understand that each `HttpSecurity` instance used to build a `SecurityFilterChain` contains a `RequestMatcher` to match incoming requests.
123
+
If a request does not match a `SecurityFilterChain` with higher priority (e.g. `@Order(1)`), the request can be tried against a filter chain with lower priority (e.g. no `@Order`).
124
+
125
+
[NOTE]
126
+
====
127
+
The matching logic for multiple filter chains is performed by the xref:servlet/architecture.adoc#servlet-filterchainproxy[`FilterChainProxy`].
128
+
====
129
+
130
+
The default `RequestMatcher` matches *any request* to ensure Spring Security protects *all requests by default*.
131
+
132
+
[NOTE]
133
+
====
134
+
Specifying a `securityMatcher` overrides this default.
135
+
====
136
+
137
+
[WARNING]
138
+
====
139
+
If no filter chain matches a particular request, the request is *not protected* by Spring Security.
140
+
====
141
+
142
+
The following example demonstrates a single filter chain that only protects requests that begin with `/secured/`:
open fun userDetailsService(): UserDetailsService {
154
+
// ...
155
+
}
156
+
157
+
@Bean
158
+
open fun securedFilterChain(http: HttpSecurity): SecurityFilterChain {
159
+
http {
160
+
securityMatcher("/secured/**") <1>
161
+
authorizeHttpRequests {
162
+
authorize("/secured/user", hasRole("USER")) <2>
163
+
authorize("/secured/admin", hasRole("ADMIN")) <3>
164
+
authorize(anyRequest, authenticated) <4>
165
+
}
166
+
httpBasic { }
167
+
formLogin { }
168
+
}
169
+
return http.build()
170
+
}
171
+
}
172
+
----
173
+
<1> Requests that begin with `/secured/` will be protected but any other requests are not protected.
174
+
<2> Requests to `/secured/user` require the `ROLE_USER` authority.
175
+
<3> Requests to `/secured/admin` require the `ROLE_ADMIN` authority.
176
+
<4> Any other requests (such as `/secured/other`) simply require an authenticated user.
177
+
178
+
[TIP]
179
+
====
180
+
It is _recommended_ to provide a `SecurityFilterChain` that does not specify any `securityMatcher` to ensure the entire application is protected, as demonstrated in the <<multiple-httpsecurity-instances-kotlin,earlier example>>.
181
+
====
182
+
183
+
Notice that the `requestMatchers` method only applies to individual authorization rules.
184
+
Each request listed there must also match the overall `securityMatcher` for this particular `HttpSecurity` instance used to create the `SecurityFilterChain`.
185
+
Using `anyRequest()` in this example matches all other requests within this particular `SecurityFilterChain` (which must begin with `/secured/`).
186
+
187
+
[NOTE]
188
+
====
189
+
See xref:servlet/authorization/authorize-http-requests.adoc[Authorize HttpServletRequests] for more information on `requestMatchers`.
190
+
====
191
+
192
+
=== `SecurityFilterChain` Endpoints
193
+
194
+
Several filters in the `SecurityFilterChain` directly provide endpoints, such as the `UsernamePasswordAuthenticationFilter` which is set up by `http.formLogin()` and provides the `POST /login` endpoint.
195
+
In the <<choosing-security-matcher-request-matchers-kotlin,above example>>, the `/login` endpoint is not matched by `http.securityMatcher("/secured/**")` and therefore that application would not have any `GET /login` or `POST /login` endpoint.
196
+
Such requests would return `404 Not Found`.
197
+
This is often surprising to users.
198
+
199
+
Specifying `http.securityMatcher()` affects what requests are matched by that `SecurityFilterChain`.
200
+
However, it does not automatically affect endpoints provided by the filter chain.
201
+
In such cases, you may need to customize the URL of any endpoints you would like the filter chain to provide.
202
+
203
+
The following example demonstrates a configuration that secures requests that begin with `/secured/` and denies all other requests, while also customizing endpoints provided by the `SecurityFilterChain`:
open fun userDetailsService(): UserDetailsService {
215
+
// ...
216
+
}
217
+
218
+
@Bean
219
+
@Order(1)
220
+
open fun securedFilterChain(http: HttpSecurity): SecurityFilterChain {
221
+
http {
222
+
securityMatcher("/secured/**") <1>
223
+
authorizeHttpRequests {
224
+
authorize(anyRequest, authenticated) <2>
225
+
}
226
+
formLogin { <3>
227
+
loginPage = "/secured/login"
228
+
loginProcessingUrl = "/secured/login"
229
+
permitAll = true
230
+
}
231
+
logout { <4>
232
+
logoutUrl = "/secured/logout"
233
+
logoutSuccessUrl = "/secured/login?logout"
234
+
permitAll = true
235
+
}
236
+
}
237
+
return http.build()
238
+
}
239
+
240
+
@Bean
241
+
open fun defaultFilterChain(http: HttpSecurity): SecurityFilterChain {
242
+
http {
243
+
authorizeHttpRequests {
244
+
authorize(anyRequest, denyAll) <5>
245
+
}
246
+
}
247
+
return http.build()
248
+
}
249
+
}
250
+
----
251
+
<1> Requests that begin with `/secured/` will be protected by this filter chain.
252
+
<2> Requests that begin with `/secured/` require an authenticated user.
253
+
<3> Customize form login to prefix URLs with `/secured/`.
254
+
<4> Customize logout to prefix URLs with `/secured/`.
255
+
<5> All other requests will be denied.
256
+
257
+
[NOTE]
258
+
====
259
+
This example customizes the login and logout pages, which disables Spring Security's generated pages.
260
+
You must xref:servlet/authentication/passwords/form.adoc#servlet-authentication-form-custom[provide your own] custom endpoints for `GET /secured/login` and `GET /secured/logout`.
261
+
Note that Spring Security still provides `POST /secured/login` and `POST /secured/logout` endpoints for you.
262
+
====
263
+
264
+
=== Real World Example
265
+
266
+
The following example demonstrates a slightly more real-world configuration putting all of these elements together:
open fun defaultSecurityFilterChain(http: HttpSecurity): SecurityFilterChain {
317
+
val allowedPaths = arrayOf("/", "/user-login", "/user-logout", "/notices", "/contact", "/register")
318
+
http {
319
+
authorizeHttpRequests {
320
+
authorize(allowedPaths, permitAll)
321
+
authorize(anyRequest, authenticated)
322
+
}
323
+
formLogin {
324
+
loginPage = "/user-login"
325
+
loginProcessingUrl = "/user-login"
326
+
}
327
+
logout {
328
+
logoutUrl = "/user-logout"
329
+
logoutSuccessUrl = "/?logout"
330
+
}
331
+
}
332
+
return http.build()
333
+
}
334
+
}
335
+
----
336
+
<1> Begin by configuring authentication settings.
337
+
<2> Define a `SecurityFilterChain` instance with `@Order(1)`, which means that this filter chain will have the highest priority.
338
+
This filter chain applies only to requests that begin with `/accounts/approvals/`, `/loans/approvals/` or `/credit-cards/approvals/`.
339
+
Requests to this filter chain require the `ROLE_ADMIN` authority and allow HTTP Basic Authentication.
340
+
<3> Next, create another `SecurityFilterChain` instance with `@Order(2)` which will be considered second.
341
+
This filter chain applies only to requests that begin with `/accounts/`, `/loans/`, `/credit-cards/`, or `/balances/`.
342
+
Notice that because this filter chain is second, any requests that include `/approvals/` will match the previous filter chain and will *not* be matched by this filter chain.
343
+
Requests to this filter chain require the `ROLE_USER` authority.
344
+
This filter chain does not define any authentication because the next (default) filter chain contains that configuration.
345
+
<4> Lastly, create an additional `SecurityFilterChain` instance without an `@Order` annotation.
346
+
This configuration will handle requests not covered by the other filter chains and will be processed last (no `@Order` defaults to last).
347
+
Requests that match `/`, `/user-login`, `/user-logout`, `/notices`, `/contact` and `/register` allow access without authentication.
348
+
Any other requests require the user to be authenticated to access any URL not explicitly allowed or protected by other filter chains.
0 commit comments