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
@@ -22,231 +22,58 @@ This means that, if you use more advanced options, such as integrating with `Web
22
22
====
23
23
24
24
[[mvc-requestmatcher]]
25
-
== MvcRequestMatcher
25
+
== PathPatternRequestMatcher
26
26
27
-
Spring Security provides deep integration with how Spring MVC matches on URLs with `MvcRequestMatcher`.
27
+
Spring Security provides deep integration with how Spring MVC matches on URLs with `PathPatternRequestMatcher`.
28
28
This is helpful to ensure that your Security rules match the logic used to handle your requests.
29
29
30
-
To use `MvcRequestMatcher`, you must place the Spring Security Configuration in the same `ApplicationContext` as your `DispatcherServlet`.
31
-
This is necessary because Spring Security's `MvcRequestMatcher` expects a `HandlerMappingIntrospector` bean with the name of `mvcHandlerMappingIntrospector` to be registered by your Spring MVC configuration that is used to perform the matching.
32
-
33
-
For a `web.xml` file, this means that you should place your configuration in the `DispatcherServlet.xml`:
class SecurityInitializer : AbstractAnnotationConfigDispatcherServletInitializer() {
97
-
override fun getRootConfigClasses(): Array<Class<*>>? {
98
-
return null
99
-
}
100
-
101
-
override fun getServletConfigClasses(): Array<Class<*>> {
102
-
return arrayOf(
103
-
RootConfiguration::class.java,
104
-
WebMvcConfiguration::class.java
105
-
)
106
-
}
107
-
108
-
override fun getServletMappings(): Array<String> {
109
-
return arrayOf("/")
110
-
}
111
-
}
112
-
----
113
-
======
114
-
115
-
[NOTE]
116
-
====
117
-
We always recommend that you provide authorization rules by matching on the `HttpServletRequest` and method security.
118
-
119
-
Providing authorization rules by matching on `HttpServletRequest` is good, because it happens very early in the code path and helps reduce the https://en.wikipedia.org/wiki/Attack_surface[attack surface].
120
-
Method security ensures that, if someone has bypassed the web authorization rules, your application is still secured.
121
-
This is known as https://en.wikipedia.org/wiki/Defense_in_depth_(computing)[Defense in Depth]
return new PathPatternRequestMatcherBuilderFactoryBean();
135
42
}
136
43
----
137
44
138
45
Kotlin::
139
46
+
140
47
[source,kotlin,role="secondary"]
141
48
----
142
-
@RequestMapping("/admin")
143
-
fun admin(): String {
144
-
// ...
145
-
}
146
-
----
147
-
======
148
-
149
-
To restrict access to this controller method to admin users, you can provide authorization rules by matching on the `HttpServletRequest` with the following:
150
-
151
-
[tabs]
152
-
======
153
-
Java::
154
-
+
155
-
[source,java,role="primary"]
156
-
----
157
49
@Bean
158
-
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
159
-
http
160
-
.authorizeHttpRequests((authorize) -> authorize
161
-
.requestMatchers("/admin").hasRole("ADMIN")
162
-
);
163
-
return http.build();
50
+
fun usePathPattern(): PathPatternRequestMatcherBuilderFactoryBean {
With either configuration, the `/admin` URL requires the authenticated user to be an admin user.
193
-
However, depending on our Spring MVC configuration, the `/admin.html` URL also maps to our `admin()` method.
194
-
Additionally, depending on our Spring MVC configuration, the `/admin` URL also maps to our `admin()` method.
63
+
and Spring Security will find the appropriate Spring MVC configuration for you.
195
64
196
-
The problem is that our security rule protects only `/admin`.
197
-
We could add additional rules for all the permutations of Spring MVC, but this would be quite verbose and tedious.
65
+
If you *are* customizing Spring MVC's `PathPatternParser` instance, you will need to <<security-mvc-same-application-context, configure Spring Security and Spring MVC in the same `ApplicationContext`>>.
198
66
199
-
Fortunately, when using the `requestMatchers` DSL method, Spring Security automatically creates a `MvcRequestMatcher` if it detects that Spring MVC is available in the classpath.
200
-
Therefore, it will protect the same URLs that Spring MVC will match on by using Spring MVC to match on the URL.
201
-
202
-
One common requirement when using Spring MVC is to specify the servlet path property.
203
-
204
-
For Java-based Configuration, you can use the `MvcRequestMatcher.Builder` to create multiple `MvcRequestMatcher` instances that share the same servlet path:
205
-
206
-
[source,java,role="primary"]
207
-
----
208
-
@Bean
209
-
public SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {
210
-
MvcRequestMatcher.Builder mvcMatcherBuilder = new MvcRequestMatcher.Builder(introspector).servletPath("/path");
We always recommend that you provide authorization rules by matching on the `HttpServletRequest` and method security.
219
70
220
-
For Kotlin and XML, this happens when you specify the servlet path for each path like so:
71
+
Providing authorization rules by matching on `HttpServletRequest` is good, because it happens very early in the code path and helps reduce the https://en.wikipedia.org/wiki/Attack_surface[attack surface].
72
+
Method security ensures that, if someone has bypassed the web authorization rules, your application is still secured.
73
+
This is known as https://en.wikipedia.org/wiki/Defense_in_depth_(computing)[Defense in Depth]
74
+
====
221
75
222
-
[tabs]
223
-
======
224
-
Kotlin::
225
-
+
226
-
[source,kotlin,role="secondary"]
227
-
----
228
-
@Bean
229
-
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
Now that Spring MVC is integrated with Spring Security, you are ready to write some xref:servlet/authorization/authorize-http-requests.adoc[authorization rules] that will use `PathPatternRequestMatcher`.
250
77
251
78
[[mvc-authentication-principal]]
252
79
== @AuthenticationPrincipal
@@ -766,3 +593,95 @@ class CsrfController {
766
593
767
594
It is important to keep the `CsrfToken` a secret from other domains.
768
595
This means that, if you use https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS[Cross Origin Sharing (CORS)], you should *NOT* expose the `CsrfToken` to any external domains.
596
+
597
+
[[security-mvc-same-application-context]]
598
+
== Configuring Spring MVC and Spring Security in the Same Application Context
599
+
600
+
If you are using Boot, Spring MVC and Spring Security are in the same application context by default.
601
+
602
+
Otherwise, for Java Config, including both `@EnableWebMvc` and `@EnableWebSecurity` will construct Spring Security and Spring MVC components in the same context.
603
+
604
+
Of, if you are using ``ServletListener``s you can do:
0 commit comments