web.adoc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. = Web Migrations
  2. [[use-path-pattern]]
  3. == Use PathPatternRequestMatcher by Default
  4. In Spring Security 7, `AntPathRequestMatcher` and `MvcRequestMatcher` are no longer supported and the Java DSL requires that all URIs be absolute (less any context root).
  5. At that time, Spring Security 7 will use `PathPatternRequestMatcher` by default.
  6. To check how prepared you are for this change, you can publish this bean:
  7. [tabs]
  8. ======
  9. Java::
  10. +
  11. [source,java,role="primary"]
  12. ----
  13. @Bean
  14. PathPatternRequestMatcherBuilderFactoryBean requestMatcherBuilder() {
  15. return new PathPatternRequestMatcherBuilderFactoryBean();
  16. }
  17. ----
  18. Kotlin::
  19. +
  20. [source,kotlin,role="secondary"]
  21. ----
  22. @Bean
  23. fun requestMatcherBuilder(): PathPatternRequestMatcherBuilderFactoryBean {
  24. return PathPatternRequestMatcherBuilderFactoryBean()
  25. }
  26. ----
  27. Xml::
  28. +
  29. [source,xml,role="secondary"]
  30. ----
  31. <b:bean class="org.springframework.security.config.web.PathPatternRequestMatcherBuilderFactoryBean"/>
  32. ----
  33. ======
  34. This will tell the Spring Security DSL to use `PathPatternRequestMatcher` for all request matchers that it constructs.
  35. In the event that you are directly constructing an object (as opposed to having the DSL construct it) that has a `setRequestMatcher` method. you should also proactively specify a `PathPatternRequestMatcher` there as well.
  36. === Migrate `exitUserUrl` and `switchUserUrl` Request Matchers in `SwitchUserFilter`
  37. `SwitchUserFilter`, constructs an `AntPathRequestMatcher` in its `setExitUserUrl` and `setSwitchUserUrl` methods.
  38. This will change to use `PathPatternRequestMatcher` in Spring Security 7.
  39. To prepare for this change, call `setExitUserMatcher` and `setSwithcUserMatcher` to provide this `PathPatternRequestMatcher` in advance.
  40. That is, change this:
  41. [tabs]
  42. ======
  43. Java::
  44. +
  45. [source,java,role="primary"]
  46. ----
  47. SwitchUserFilter switchUser = new SwitchUserFilter();
  48. // ... other configuration
  49. switchUser.setExitUserUrl("/exit/impersonate");
  50. ----
  51. Kotlin::
  52. +
  53. [source,kotlin,role="secondary"]
  54. ----
  55. val switchUser = SwitchUserFilter()
  56. // ... other configuration
  57. switchUser.setExitUserUrl("/exit/impersonate")
  58. ----
  59. ======
  60. to this:
  61. [tabs]
  62. ======
  63. Java::
  64. +
  65. [source,java,role="primary"]
  66. ----
  67. SwitchUserFilter switchUser = new SwitchUserFilter();
  68. // ... other configuration
  69. switchUser.setExitUserMatcher(PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.POST, "/exit/impersonate"));
  70. ----
  71. Kotlin::
  72. +
  73. [source,kotlin,role="secondary"]
  74. ----
  75. val switchUser = SwitchUserFilter()
  76. // ... other configuration
  77. switchUser.setExitUserMatcher(PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.POST, "/exit/impersonate"))
  78. ----
  79. ======
  80. === Migrate CAS Proxy Receptor Request Matcher
  81. Spring Security 6 converts any configured `proxyReceptorUrl` to a request matcher that matches the end of the request, that is `/**/proxy/receptor`.
  82. In Spring Security 7, this pattern is not allowed and will change to using `PathPatternRequestMatcher`.
  83. Also in Spring Security 7m the URL should by absolute, excluding any context path, like so: `/proxy/receptor`.
  84. So to prepare for these change, you can use `setProxyReceptorRequestMatcher` instead of `setProxyReceptorUrl`.
  85. That is, change this:
  86. [tabs]
  87. ======
  88. Java::
  89. +
  90. [source,java,role="primary"]
  91. ----
  92. casAuthentication.setProxyReceptorUrl("/proxy/receptor");
  93. ----
  94. Kotlin::
  95. +
  96. [source,kotlin,role="secondary"]
  97. ----
  98. casAuthentication.setProxyReceptorUrl("/proxy/receptor")
  99. ----
  100. ======
  101. to this:
  102. [tabs]
  103. ======
  104. Java::
  105. +
  106. [source,java,role="primary"]
  107. ----
  108. casAuthentication.setProxyReceptorUrl(PathPatternRequestMatcher.withDefaults().matcher("/proxy/receptor"));
  109. ----
  110. Kotlin::
  111. +
  112. [source,kotlin,role="secondary"]
  113. ----
  114. casAuthentication.setProxyReceptorUrl(PathPatternRequestMatcher.withDefaults().matcher("/proxy/receptor"))
  115. ----
  116. ======
  117. == Include the Servlet Path Prefix in Authorization Rules
  118. For many applications <<use-path-pattern, the above>> will make no difference since most commonly all URIs listed are matched by the default servlet.
  119. However, if you have other servlets with servlet path prefixes, xref:servlet/authorization/authorize-http-requests.adoc[then these paths now need to be supplied separately].
  120. For example, if I have a Spring MVC controller with `@RequestMapping("/orders")` and my MVC application is deployed to `/mvc` (instead of the default servlet), then the URI for this endpoint is `/mvc/orders`.
  121. Historically, the Java DSL hasn't had a simple way to specify the servlet path prefix and Spring Security attempted to infer it.
  122. Over time, we learned that these inference would surprise developers.
  123. Instead of taking this responsibility away from developers, now it is simpler to specify the servlet path prefix like so:
  124. [method,java]
  125. ----
  126. PathPatternRequestParser.Builder servlet = PathPatternRequestParser.servletPath("/mvc");
  127. http
  128. .authorizeHttpRequests((authorize) -> authorize
  129. .requestMatchers(servlet.pattern("/orders/**").matcher()).authenticated()
  130. )
  131. ----
  132. For paths that belong to the default servlet, use `PathPatternRequestParser.path()` instead:
  133. [method,java]
  134. ----
  135. PathPatternRequestParser.Builder request = PathPatternRequestParser.path();
  136. http
  137. .authorizeHttpRequests((authorize) -> authorize
  138. .requestMatchers(request.pattern("/js/**").matcher()).authenticated()
  139. )
  140. ----
  141. Note that this doesn't address every kind of servlet since not all servlets have a path prefix.
  142. For example, expressions that match the JSP Servlet might use an ant pattern `/**/*.jsp`.
  143. There is not yet a general-purpose replacement for these, and so you are encouraged to use `RegexRequestMatcher`, like so: `regexMatcher("\\.jsp$")`.
  144. For many applications this will make no difference since most commonly all URIs listed are matched by the default servlet.
  145. [[use-redirect-to-https]]
  146. == Use RedirectToHttps Instead of Channel Security
  147. Years ago, HTTPS at large was enough of a performance and configuration concern that applications wanted to be able to decide which segments of an application would require HTTPS.
  148. `requires-channel` in XML and `requiresChannel` in Java Config allowed configurating an application with that in mind:
  149. [tabs]
  150. ======
  151. Java::
  152. +
  153. [source,java,role="primary"]
  154. ----
  155. http
  156. .requiresChannel((channel) -> channel
  157. .requestMatchers("/secure/**").requiresSecureChannel()
  158. .requestMatchers("/insecure/**").requiresInsecureChannel()
  159. )
  160. ----
  161. Kotlin::
  162. +
  163. [source,kotlin,role="secondary"]
  164. ----
  165. http {
  166. requiresChannel {
  167. secure("/secure/**")
  168. seccure("/insecure/**", "REQUIRES_INSECURE_CHANNEL")
  169. }
  170. }
  171. ----
  172. Xml::
  173. +
  174. [source,xml,role="secondary"]
  175. ----
  176. <http>
  177. <intercept-url pattern="/secure/**" access="authenticated" requires-channel="REQUIRES_SECURE_CHANNEL"/>
  178. <intercept-url pattern="/insecure/**" access="authenticated" requires-channel="REQUIRES_INSECURE_CHANNEL"/>
  179. </http>
  180. ----
  181. ======
  182. Modern applications should either always require HTTPS.
  183. However, there are times, like when developing locally, when one would like the application to use HTTP.
  184. Or, you may have continuing circumstances that require part of your application to be HTTP.
  185. In any case, you can migrate to `redirect-to-https-request-matcher-ref` and `redirectToHttps` by first constructing a `RequestMatcher` that contains all circumstances where redirecting to HTTPS is needed.
  186. Then you can reference that request matcher like so:
  187. [tabs]
  188. ======
  189. Java::
  190. +
  191. [source,java,role="primary"]
  192. ----
  193. http
  194. .redirectToHttps((https) -> https.requestMatchers("/secure/**"))
  195. // ...
  196. ----
  197. Kotlin::
  198. +
  199. [source,kotlin,role="secondary"]
  200. ----
  201. var secure: RequestMatcher = PathPatternRequestMatcher.withDefaults().pattern("/secure/**")
  202. http {
  203. redirectToHttps {
  204. requestMatchers = secure
  205. }
  206. // ...
  207. }
  208. ----
  209. Xml::
  210. +
  211. [source,xml,role="secondary"]
  212. ----
  213. <b:bean id="builder" class="org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher$Builder"/>
  214. <b:bean id="secure" class="org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher" factory-bean="builder" factory-method="matcher">
  215. <b:constructor-arg value="/secure/**"/>
  216. </b:bean>
  217. <http redirect-to-https-request-matcher-ref="secure">
  218. <intercept-url pattern="/secure/**" access="authenticated"/>
  219. <intercept-url pattern="/insecure/**" access="authenticated"/>
  220. <!-- ... -->
  221. </http>
  222. ----
  223. ======
  224. [TIP]
  225. =====
  226. If you have several circumstances where HTTP is needed, consider using `OrRequestMatcher` to combine them into a single `RequestMatcher` instance.
  227. =====