Skip to content

Commit 40929a5

Browse files
committed
Merge branch '6.1.x'
Closes gh-13727
2 parents fe5a55f + bcfa4ad commit 40929a5

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

docs/modules/ROOT/pages/servlet/authorization/authorize-http-requests.adoc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ In many cases, your authorization rules will be more sophisticated than that, so
5252
* I have an app that uses `authorizeRequests` and I want to <<migrate-authorize-requests,migrate it to `authorizeHttpRequests`>>
5353
* I want to <<request-authorization-architecture,understand how the `AuthorizationFilter` components work>>
5454
* I want to <<match-requests, match requests>> based on a pattern; specifically <<match-by-regex,regex>>
55+
* I want to match request, and I map Spring MVC to <<mvc-not-default-servlet, something other than the default servlet>>
5556
* I want to <<authorize-requests, authorize requests>>
5657
* I want to <<match-by-custom, match a request programmatically>>
5758
* I want to <<authorize-requests, authorize a request programmatically>>
@@ -570,6 +571,71 @@ http {
570571
----
571572
====
572573

574+
[[match-by-mvc]]
575+
=== Using an MvcRequestMatcher
576+
577+
Generally speaking, you can use `requestMatchers(String)` as demonstrated above.
578+
579+
However, if you map Spring MVC to a different servlet path, then you need to account for that in your security configuration.
580+
581+
For example, if Spring MVC is mapped to `/spring-mvc` instead of `/` (the default), then you may have an endpoint like `/spring-mvc/my/controller` that you want to authorize.
582+
583+
You need to use `MvcRequestMatcher` to split the servlet path and the controller path in your configuration like so:
584+
585+
.Match by MvcRequestMatcher
586+
====
587+
.Java
588+
[source,java,role="primary"]
589+
----
590+
@Bean
591+
MvcRequestMatcher.Builder mvc(HandlerMappingIntrospector introspector) {
592+
return new MvcRequestMatcher.Builder(introspector).servletPath("/spring-mvc");
593+
}
594+
595+
@Bean
596+
SecurityFilterChain appEndpoints(HttpSecurity http, MvcRequestMatcher.Builder mvc) {
597+
http
598+
.authorizeHttpRequests((authorize) -> authorize
599+
.requestMatchers(mvc.pattern("/my/controller/**")).hasAuthority("controller")
600+
.anyRequest().authenticated()
601+
);
602+
603+
return http.build();
604+
}
605+
----
606+
607+
.Kotlin
608+
[source,kotlin,role="secondary"]
609+
----
610+
@Bean
611+
fun mvc(introspector: HandlerMappingIntrospector): MvcRequestMatcher.Builder =
612+
MvcRequestMatcher.Builder(introspector).servletPath("/spring-mvc");
613+
614+
@Bean
615+
fun appEndpoints(http: HttpSecurity, mvc: MvcRequestMatcher.Builder): SecurityFilterChain =
616+
http {
617+
authorizeHttpRequests {
618+
authorize(mvc.pattern("/my/controller/**"), hasAuthority("controller"))
619+
authorize(anyRequest, authenticated)
620+
}
621+
}
622+
----
623+
624+
.Xml
625+
[source,xml,role="secondary"]
626+
----
627+
<http>
628+
<intercept-url servlet-path="/spring-mvc" pattern="/my/controller/**" access="hasAuthority('controller')"/>
629+
<intercept-url pattern="/**" access="authenticated"/>
630+
</http>
631+
----
632+
====
633+
634+
This need can arise in at least two different ways:
635+
636+
* If you use the `spring.mvc.servlet.path` Boot property to change the default path (`/`) to something else
637+
* If you register more than one Spring MVC `DispatcherServlet` (thus requiring that one of them not be the default path)
638+
573639
[[match-by-custom]]
574640
=== Using a Custom Matcher
575641

0 commit comments

Comments
 (0)