migration.adoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. [[migration]]
  2. = Migrating to 6.0
  3. The Spring Security team has prepared the 5.8 release to simplify upgrading to Spring Security 6.0.
  4. Use 5.8 and
  5. ifdef::spring-security-version[]
  6. xref:5.8.0@migration.adoc[its preparation steps]
  7. endif::[]
  8. ifndef::spring-security-version[]
  9. its preparation steps
  10. endif::[]
  11. to simplify updating to 6.0
  12. After updating to 5.8, follow this guide to perform any needed migration steps.
  13. Also, this guide includes ways to <<revert,revert to 5.x>> behaviors and its defaults, should you run into trouble.
  14. == Servlet
  15. In Spring Security 5, the default behavior is for the xref:servlet/authentication/architecture.adoc#servlet-authentication-securitycontext[`SecurityContext`] to automatically be saved to the xref:servlet/authentication/persistence.adoc#securitycontextrepository[`SecurityContextRepository`] using the xref:servlet/authentication/persistence.adoc#securitycontextpersistencefilter[`SecurityContextPersistenceFilter`].
  16. Saving must be done just prior to the `HttpServletResponse` being committed and just before `SecurityContextPersistenceFilter`.
  17. Unfortunately, automatic persistence of the `SecurityContext` can surprise users when it is done prior to the request completing (i.e. just prior to committing the `HttpServletResponse`).
  18. It also is complex to keep track of the state to determine if a save is necessary causing unnecessary writes to the `SecurityContextRepository` (i.e. `HttpSession`) at times.
  19. In Spring Security 6, the default behavior is that the xref:servlet/authentication/persistence.adoc#securitycontextholderfilter[`SecurityContextHolderFilter`] will only read the `SecurityContext` from `SecurityContextRepository` and populate it in the `SecurityContextHolder`.
  20. Users now must explicitly save the `SecurityContext` with the `SecurityContextRepository` if they want the `SecurityContext` to persist between requests.
  21. This removes ambiguity and improves performance by only requiring writing to the `SecurityContextRepository` (i.e. `HttpSession`) when it is necessary.
  22. If you are explicitly opting into Spring Security 6's new defaults, the following configuration can be removed to accept the Spring Security 6 defaults.
  23. include::partial$servlet/architecture/security-context-explicit.adoc[]
  24. === Multiple SecurityContextRepository
  25. In Spring Security 5, the default xref:servlet/authentication/persistence.adoc#securitycontextrepository[`SecurityContextRepository`] was `HttpSessionSecurityContextRepository`.
  26. In Spring Security 6, the default `SecurityContextRepository` is `DelegatingSecurityContextRepository`.
  27. If you configured the `SecurityContextRepository` only for the purpose of updating to 6.0, you can remove it completely.
  28. [[requestcache-query-optimization]]
  29. === Optimize Querying of `RequestCache`
  30. In Spring Security 5, the default behavior is to query the xref:servlet/architecture.adoc#savedrequests[saved request] on every request.
  31. This means that in a typical setup, that in order to use the xref:servlet/architecture.adoc#requestcache[`RequestCache`] the `HttpSession` is queried on every request.
  32. In Spring Security 6, the default is that `RequestCache` will only be queried for a cached request if the HTTP parameter `continue` is defined.
  33. This allows Spring Security to avoid unnecessarily reading the `HttpSession` with the `RequestCache`.
  34. In Spring Security 5 the default is to use `HttpSessionRequestCache` which will be queried for a cached request on every request.
  35. If you are not overriding the defaults (i.e. using `NullRequestCache`), then the following configuration can be used to explicitly opt into the Spring Security 6 behavior in Spring Security 5.8:
  36. include::partial$servlet/architecture/request-cache-continue.adoc[]
  37. === Use `AuthorizationManager` for Method Security
  38. There are no further migration steps for this feature.
  39. === Use `AuthorizationManager` for Message Security
  40. In 6.0, `<websocket-message-broker>` defaults `use-authorization-manager` to `true`.
  41. So, to complete migration, remove any `websocket-message-broker@use-authorization-manager=true` attribute.
  42. For example:
  43. ====
  44. .Xml
  45. [source,xml,role="primary"]
  46. ----
  47. <websocket-message-broker use-authorization-manager="true"/>
  48. ----
  49. ====
  50. changes to:
  51. ====
  52. .Xml
  53. [source,xml,role="primary"]
  54. ----
  55. <websocket-message-broker/>
  56. ----
  57. ====
  58. There are no further migrations steps for Java or Kotlin for this feature.
  59. === Use `AuthorizationManager` for Request Security
  60. In 6.0, `<http>` defaults `once-per-request` to `false`, `filter-all-dispatcher-types` to `true`, and `use-authorization-manager` to `true`.
  61. Also, xref:servlet/authorization/authorize-requests.adoc#filtersecurityinterceptor-every-request[`authorizeRequests#filterSecurityInterceptorOncePerRequest`] defaults to `false` and xref:servlet/authorization/authorize-http-requests.adoc[`authorizeHttpRequests#filterAllDispatcherTypes`] defaults to `true`.
  62. So, to complete migration, any defaults values can be removed.
  63. For example, if you opted in to the 6.0 default for `filter-all-dispatcher-types` or `authorizeHttpRequests#filterAllDispatcherTypes` like so:
  64. ====
  65. .Java
  66. [source,java,role="primary"]
  67. ----
  68. http
  69. .authorizeHttpRequests((authorize) -> authorize
  70. .filterAllDispatcherTypes(true)
  71. // ...
  72. )
  73. ----
  74. .Kotlin
  75. [source,java,role="secondary"]
  76. ----
  77. http {
  78. authorizeHttpRequests {
  79. filterAllDispatcherTypes = true
  80. // ...
  81. }
  82. }
  83. ----
  84. .Xml
  85. [source,xml,role="secondary"]
  86. ----
  87. <http use-authorization-manager="true" filter-all-dispatcher-types="true"/>
  88. ----
  89. ====
  90. then the defaults may be removed:
  91. ====
  92. .Java
  93. [source,java,role="primary"]
  94. ----
  95. http
  96. .authorizeHttpRequests((authorize) -> authorize
  97. // ...
  98. )
  99. ----
  100. .Kotlin
  101. [source,java,role="secondary"]
  102. ----
  103. http {
  104. authorizeHttpRequests {
  105. // ...
  106. }
  107. }
  108. ----
  109. .Xml
  110. [source,xml,role="secondary"]
  111. ----
  112. <http/>
  113. ----
  114. ====
  115. [NOTE]
  116. ====
  117. `once-per-request` applies only when `use-authorization-manager="false"` and `filter-all-dispatcher-types` only applies when `use-authorization-manager="true"`
  118. ====
  119. === Propagate ``AuthenticationServiceException``s
  120. {security-api-url}org/springframework/security/web/authentication/AuthenticationFilter.html[`AuthenticationFilter`] propagates {security-api-url}org/springframework/security/authentication/AuthenticationServiceException.html[``AuthenticationServiceException``]s to the {security-api-url}org/springframework/security/authentication/AuthenticationEntryPoint.html[`AuthenticationEntryPoint`].
  121. Because ``AuthenticationServiceException``s represent a server-side error instead of a client-side error, in 6.0, this changes to propagate them to the container.
  122. So, if you opted into this behavior by setting `rethrowAuthenticationServiceException` too `true`, you can now remove it like so:
  123. ====
  124. .Java
  125. [source,java,role="primary"]
  126. ----
  127. AuthenticationFilter authenticationFilter = new AuthenticationFilter(...);
  128. AuthenticationEntryPointFailureHandler handler = new AuthenticationEntryPointFailureHandler(...);
  129. handler.setRethrowAuthenticationServiceException(true);
  130. authenticationFilter.setAuthenticationFailureHandler(handler);
  131. ----
  132. .Kotlin
  133. [source,kotlin,role="secondary"]
  134. ----
  135. val authenticationFilter: AuthenticationFilter = new AuthenticationFilter(...)
  136. val handler: AuthenticationEntryPointFailureHandler = new AuthenticationEntryPointFailureHandler(...)
  137. handler.setRethrowAuthenticationServiceException(true)
  138. authenticationFilter.setAuthenticationFailureHandler(handler)
  139. ----
  140. .Xml
  141. [source,xml,role="secondary"]
  142. ----
  143. <bean id="authenticationFilter" class="org.springframework.security.web.authentication.AuthenticationFilter">
  144. <!-- ... -->
  145. <property ref="authenticationFailureHandler"/>
  146. </bean>
  147. <bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.AuthenticationEntryPointFailureHandler">
  148. <property name="rethrowAuthenticationServiceException" value="true"/>
  149. </bean>
  150. ----
  151. ====
  152. changes to:
  153. ====
  154. .Java
  155. [source,java,role="primary"]
  156. ----
  157. AuthenticationFilter authenticationFilter = new AuthenticationFilter(...);
  158. AuthenticationEntryPointFailureHandler handler = new AuthenticationEntryPointFailureHandler(...);
  159. authenticationFilter.setAuthenticationFailureHandler(handler);
  160. ----
  161. .Kotlin
  162. [source,kotlin,role="secondary"]
  163. ----
  164. val authenticationFilter: AuthenticationFilter = new AuthenticationFilter(...)
  165. val handler: AuthenticationEntryPointFailureHandler = new AuthenticationEntryPointFailureHandler(...)
  166. authenticationFilter.setAuthenticationFailureHandler(handler)
  167. ----
  168. .Xml
  169. [source,xml,role="secondary"]
  170. ----
  171. <bean id="authenticationFilter" class="org.springframework.security.web.authentication.AuthenticationFilter">
  172. <!-- ... -->
  173. <property ref="authenticationFailureHandler"/>
  174. </bean>
  175. <bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.AuthenticationEntryPointFailureHandler">
  176. <!-- ... -->
  177. </bean>
  178. ----
  179. ====
  180. [[servlet-opt-in-sha256-rememberme]]
  181. === Use SHA-256 in Remember Me
  182. In 6.0, the `TokenBasedRememberMeServices` uses SHA-256 to encode and match the token.
  183. To complete the migration, any default values can be removed.
  184. For example, if you opted in to the 6.0 default for `encodingAlgorithm` and `matchingAlgorithm` like so:
  185. ====
  186. .Java
  187. [source,java,role="primary"]
  188. ----
  189. @Configuration
  190. @EnableWebSecurity
  191. public class SecurityConfig {
  192. @Bean
  193. SecurityFilterChain securityFilterChain(HttpSecurity http, RememberMeServices rememberMeServices) throws Exception {
  194. http
  195. // ...
  196. .rememberMe((remember) -> remember
  197. .rememberMeServices(rememberMeServices)
  198. );
  199. return http.build();
  200. }
  201. @Bean
  202. RememberMeServices rememberMeServices(UserDetailsService userDetailsService) {
  203. RememberMeTokenAlgorithm encodingAlgorithm = RememberMeTokenAlgorithm.SHA256;
  204. TokenBasedRememberMeServices rememberMe = new TokenBasedRememberMeServices(myKey, userDetailsService, encodingAlgorithm);
  205. rememberMe.setMatchingAlgorithm(RememberMeTokenAlgorithm.SHA256);
  206. return rememberMe;
  207. }
  208. }
  209. ----
  210. .XML
  211. [source,xml,role="secondary"]
  212. ----
  213. <http>
  214. <remember-me services-ref="rememberMeServices"/>
  215. </http>
  216. <bean id="rememberMeServices" class=
  217. "org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
  218. <property name="userDetailsService" ref="myUserDetailsService"/>
  219. <property name="key" value="springRocks"/>
  220. <property name="matchingAlgorithm" value="SHA256"/>
  221. <property name="encodingAlgorithm" value="SHA256"/>
  222. </bean>
  223. ----
  224. ====
  225. then the defaults can be removed:
  226. ====
  227. .Java
  228. [source,java,role="primary"]
  229. ----
  230. @Configuration
  231. @EnableWebSecurity
  232. public class SecurityConfig {
  233. @Bean
  234. SecurityFilterChain securityFilterChain(HttpSecurity http, RememberMeServices rememberMeServices) throws Exception {
  235. http
  236. // ...
  237. .rememberMe((remember) -> remember
  238. .rememberMeServices(rememberMeServices)
  239. );
  240. return http.build();
  241. }
  242. @Bean
  243. RememberMeServices rememberMeServices(UserDetailsService userDetailsService) {
  244. return new TokenBasedRememberMeServices(myKey, userDetailsService);
  245. }
  246. }
  247. ----
  248. .XML
  249. [source,xml,role="secondary"]
  250. ----
  251. <http>
  252. <remember-me services-ref="rememberMeServices"/>
  253. </http>
  254. <bean id="rememberMeServices" class=
  255. "org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
  256. <property name="userDetailsService" ref="myUserDetailsService"/>
  257. <property name="key" value="springRocks"/>
  258. </bean>
  259. ----
  260. ====
  261. [[use-new-requestmatchers]]
  262. === Use the new `requestMatchers` methods
  263. There are no further migration steps for this feature.
  264. [[use-new-security-matchers]]
  265. === Use the new `securityMatchers` methods
  266. There are no further migration steps for this feature.
  267. == Reactive
  268. === Use `AuthorizationManager` for Method Security
  269. In 6.0, `@EnableReactiveMethodSecurity` defaults `useAuthorizationManager` to `true`.
  270. So, to complete migration, {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableReactiveMethodSecurity.html[`@EnableReactiveMethodSecurity`] remove the `useAuthorizationManager` attribute:
  271. ====
  272. .Java
  273. [source,java,role="primary"]
  274. ----
  275. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  276. ----
  277. .Kotlin
  278. [source,kotlin,role="secondary"]
  279. ----
  280. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  281. ----
  282. ====
  283. changes to:
  284. ====
  285. .Java
  286. [source,java,role="primary"]
  287. ----
  288. @EnableReactiveMethodSecurity
  289. ----
  290. .Kotlin
  291. [source,kotlin,role="secondary"]
  292. ----
  293. @EnableReactiveMethodSecurity
  294. ----
  295. ====
  296. '''
  297. === Propagate ``AuthenticationServiceException``s
  298. {security-api-url}org/springframework/security/web/server/authentication/AuthenticationWebFilter.html[`AuthenticationWebFilter`] propagates {security-api-url}org/springframework/security/authentication/AuthenticationServiceException.html[``AuthenticationServiceException``]s to the {security-api-url}org/springframework/security/web/server/ServerAuthenticationEntryPoint.html[`ServerAuthenticationEntryPoint`].
  299. Because ``AuthenticationServiceException``s represent a server-side error instead of a client-side error, in 6.0, this changes to propagate them to the container.
  300. So, if you opted into this behavior by setting `rethrowAuthenticationServiceException` too `true`, you can now remove it like so:
  301. ====
  302. .Java
  303. [source,java,role="primary"]
  304. ----
  305. AuthenticationFailureHandler bearerFailureHandler = new ServerAuthenticationEntryPointFailureHandler(bearerEntryPoint);
  306. bearerFailureHandler.setRethrowAuthenticationServiceException(true);
  307. AuthenticationFailureHandler basicFailureHandler = new ServerAuthenticationEntryPointFailureHandler(basicEntryPoint);
  308. basicFailureHandler.setRethrowAuthenticationServiceException(true);
  309. ----
  310. .Kotlin
  311. [source,kotlin,role="secondary"]
  312. ----
  313. val bearerFailureHandler: AuthenticationFailureHandler = ServerAuthenticationEntryPointFailureHandler(bearerEntryPoint)
  314. bearerFailureHandler.setRethrowAuthenticationServiceException(true)
  315. val basicFailureHandler: AuthenticationFailureHandler = ServerAuthenticationEntryPointFailureHandler(basicEntryPoint)
  316. basicFailureHandler.setRethrowAuthenticationServiceException(true)
  317. ----
  318. ====
  319. changes to:
  320. ====
  321. .Java
  322. [source,java,role="primary"]
  323. ----
  324. AuthenticationFailureHandler bearerFailureHandler = new ServerAuthenticationEntryPointFailureHandler(bearerEntryPoint);
  325. AuthenticationFailureHandler basicFailureHandler = new ServerAuthenticationEntryPointFailureHandler(basicEntryPoint);
  326. ----
  327. .Kotlin
  328. [source,kotlin,role="secondary"]
  329. ----
  330. val bearerFailureHandler: AuthenticationFailureHandler = ServerAuthenticationEntryPointFailureHandler(bearerEntryPoint)
  331. val basicFailureHandler: AuthenticationFailureHandler = ServerAuthenticationEntryPointFailureHandler(basicEntryPoint)
  332. ----
  333. ====
  334. [NOTE]
  335. ====
  336. If you configured the `ServerAuthenticationFailureHandler` only for the purpose of updating to 6.0, you can remove it completely.
  337. ====
  338. [[revert]]
  339. If you are running into trouble with any of the 6.0 changes, please first try to apply the following changes to get you up and running.
  340. It's more important to stay on 6.0 and get the security improvements.
  341. == Revert Servlet
  342. == Revert Reactive