migration.adoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. [[requestcache-query-optimization]]
  25. === Optimize Querying of `RequestCache`
  26. In Spring Security 5, the default behavior is to query the xref:servlet/architecture.adoc#savedrequests[saved request] on every request.
  27. 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.
  28. In Spring Security 6, the default is that `RequestCache` will only be queried for a cached request if the HTTP parameter `continue` is defined.
  29. This allows Spring Security to avoid unnecessarily reading the `HttpSession` with the `RequestCache`.
  30. In Spring Security 5 the default is to use `HttpSessionRequestCache` which will be queried for a cached request on every request.
  31. 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:
  32. include::partial$servlet/architecture/request-cache-continue.adoc[]
  33. === Use `AuthorizationManager` for Method Security
  34. There are no further migration steps for this feature.
  35. === Use `AuthorizationManager` for Message Security
  36. In 6.0, `<websocket-message-broker>` defaults `use-authorization-manager` to `true`.
  37. So, to complete migration, remove any `websocket-message-broker@use-authorization-manager=true` attribute.
  38. For example:
  39. ====
  40. .Xml
  41. [source,xml,role="primary"]
  42. ----
  43. <websocket-message-broker use-authorization-manager="true"/>
  44. ----
  45. ====
  46. changes to:
  47. ====
  48. .Xml
  49. [source,xml,role="primary"]
  50. ----
  51. <websocket-message-broker/>
  52. ----
  53. ====
  54. There are no further migrations steps for Java or Kotlin for this feature.
  55. === Use `AuthorizationManager` for Request Security
  56. In 6.0, `<http>` defaults `once-per-request` to `false`, `filter-all-dispatcher-types` to `true`, and `use-authorization-manager` to `true`.
  57. 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`.
  58. So, to complete migration, any defaults values can be removed.
  59. For example, if you opted in to the 6.0 default for `filter-all-dispatcher-types` or `authorizeHttpRequests#filterAllDispatcherTypes` like so:
  60. ====
  61. .Java
  62. [source,java,role="primary"]
  63. ----
  64. http
  65. .authorizeHttpRequests((authorize) -> authorize
  66. .filterAllDispatcherTypes(true)
  67. // ...
  68. )
  69. ----
  70. .Kotlin
  71. [source,java,role="secondary"]
  72. ----
  73. http {
  74. authorizeHttpRequests {
  75. filterAllDispatcherTypes = true
  76. // ...
  77. }
  78. }
  79. ----
  80. .Xml
  81. [source,xml,role="secondary"]
  82. ----
  83. <http use-authorization-manager="true" filter-all-dispatcher-types="true"/>
  84. ----
  85. ====
  86. then the defaults may be removed:
  87. ====
  88. .Java
  89. [source,java,role="primary"]
  90. ----
  91. http
  92. .authorizeHttpRequests((authorize) -> authorize
  93. // ...
  94. )
  95. ----
  96. .Kotlin
  97. [source,java,role="secondary"]
  98. ----
  99. http {
  100. authorizeHttpRequests {
  101. // ...
  102. }
  103. }
  104. ----
  105. .Xml
  106. [source,xml,role="secondary"]
  107. ----
  108. <http/>
  109. ----
  110. ====
  111. [NOTE]
  112. ====
  113. `once-per-request` applies only when `use-authorization-manager="false"` and `filter-all-dispatcher-types` only applies when `use-authorization-manager="true"`
  114. ====
  115. === Propagate ``AuthenticationServiceException``s
  116. {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`].
  117. 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.
  118. So, if you opted into this behavior by setting `rethrowAuthenticationServiceException` too `true`, you can now remove it like so:
  119. ====
  120. .Java
  121. [source,java,role="primary"]
  122. ----
  123. AuthenticationFilter authenticationFilter = new AuthenticationFilter(...);
  124. AuthenticationEntryPointFailureHandler handler = new AuthenticationEntryPointFailureHandler(...);
  125. handler.setRethrowAuthenticationServiceException(true);
  126. authenticationFilter.setAuthenticationFailureHandler(handler);
  127. ----
  128. .Kotlin
  129. [source,kotlin,role="secondary"]
  130. ----
  131. val authenticationFilter: AuthenticationFilter = new AuthenticationFilter(...)
  132. val handler: AuthenticationEntryPointFailureHandler = new AuthenticationEntryPointFailureHandler(...)
  133. handler.setRethrowAuthenticationServiceException(true)
  134. authenticationFilter.setAuthenticationFailureHandler(handler)
  135. ----
  136. .Xml
  137. [source,xml,role="secondary"]
  138. ----
  139. <bean id="authenticationFilter" class="org.springframework.security.web.authentication.AuthenticationFilter">
  140. <!-- ... -->
  141. <property ref="authenticationFailureHandler"/>
  142. </bean>
  143. <bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.AuthenticationEntryPointFailureHandler">
  144. <property name="rethrowAuthenticationServiceException" value="true"/>
  145. </bean>
  146. ----
  147. ====
  148. changes to:
  149. ====
  150. .Java
  151. [source,java,role="primary"]
  152. ----
  153. AuthenticationFilter authenticationFilter = new AuthenticationFilter(...);
  154. AuthenticationEntryPointFailureHandler handler = new AuthenticationEntryPointFailureHandler(...);
  155. authenticationFilter.setAuthenticationFailureHandler(handler);
  156. ----
  157. .Kotlin
  158. [source,kotlin,role="secondary"]
  159. ----
  160. val authenticationFilter: AuthenticationFilter = new AuthenticationFilter(...)
  161. val handler: AuthenticationEntryPointFailureHandler = new AuthenticationEntryPointFailureHandler(...)
  162. authenticationFilter.setAuthenticationFailureHandler(handler)
  163. ----
  164. .Xml
  165. [source,xml,role="secondary"]
  166. ----
  167. <bean id="authenticationFilter" class="org.springframework.security.web.authentication.AuthenticationFilter">
  168. <!-- ... -->
  169. <property ref="authenticationFailureHandler"/>
  170. </bean>
  171. <bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.AuthenticationEntryPointFailureHandler">
  172. <!-- ... -->
  173. </bean>
  174. ----
  175. ====
  176. [[servlet-opt-in-sha256-rememberme]]
  177. === Use SHA-256 in Remember Me
  178. The `TokenBasedRememberMeServices` implementation now supports SHA-256 for the Remember Me token and this is the default in Spring Security 6.
  179. This change makes the implementation more secure by default since MD5 is already proven to be a weak hashing algorithm and vulnerable against collision attacks and modular differential attacks.
  180. The new generated tokens now have the information of which algorithm was used to generate the token and that information is used in order to match it.
  181. If the algorithm name is not present, then the `matchingAlgorithm` property is used to check the token.
  182. This allows for a smooth transition from MD5 to SHA-256.
  183. To opt into the new Spring Security 6 default to encode the tokens while still being able to decode tokens encoded with MD5, you can set the `encodingAlgorithm` property to SHA-256 and the `matchingAlgorithm` property to MD5.
  184. See the xref:servlet/authentication/rememberme.adoc#_tokenbasedremembermeservices[reference documentation] and the {security-api-url}org/springframework/security/web/authentication/rememberme/TokenBasedRememberMeServices.html[API docs] for more information.
  185. [[servlet-opt-in-sha256-sha256-encoding]]
  186. .Use Spring Security 6 defaults for encoding, SHA-256 for encoding and MD5 for matching
  187. ====
  188. .Java
  189. [source,java,role="primary"]
  190. ----
  191. @Configuration
  192. @EnableWebSecurity
  193. public class SecurityConfig {
  194. @Bean
  195. SecurityFilterChain securityFilterChain(HttpSecurity http, RememberMeServices rememberMeServices) throws Exception {
  196. http
  197. // ...
  198. .rememberMe((remember) -> remember
  199. .rememberMeServices(rememberMeServices)
  200. );
  201. return http.build();
  202. }
  203. @Bean
  204. RememberMeServices rememberMeServices(UserDetailsService userDetailsService) {
  205. RememberMeTokenAlgorithm encodingAlgorithm = RememberMeTokenAlgorithm.SHA256;
  206. TokenBasedRememberMeServices rememberMe = new TokenBasedRememberMeServices(myKey, userDetailsService, encodingAlgorithm);
  207. rememberMe.setMatchingAlgorithm(RememberMeTokenAlgorithm.MD5);
  208. return rememberMe;
  209. }
  210. }
  211. ----
  212. .XML
  213. [source,xml,role="secondary"]
  214. ----
  215. <http>
  216. <remember-me services-ref="rememberMeServices"/>
  217. </http>
  218. <bean id="rememberMeServices" class=
  219. "org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
  220. <property name="userDetailsService" ref="myUserDetailsService"/>
  221. <property name="key" value="springRocks"/>
  222. <property name="matchingAlgorithm" value="MD5"/>
  223. <property name="encodingAlgorithm" value="SHA256"/>
  224. </bean>
  225. ----
  226. ====
  227. At some point, you will want to fully migrate to Spring Security 6 defaults. But how do you know when it is safe to do so?
  228. Let's suppose that you deployed your application using SHA-256 as the encoding algorithm (as you have done <<servlet-opt-in-sha256-sha256-encoding,here>>) on November 1st, if you have the value for the `tokenValiditySeconds` property set to N days (14 is the default), you can migrate to SHA-256 N days after November 1st (which is November 15th in this example).
  229. By that time, all the tokens generated with MD5 will have expired.
  230. .Use Spring Security 6 defaults, SHA-256 for both encoding and matching
  231. ====
  232. .Java
  233. [source,java,role="primary"]
  234. ----
  235. @Configuration
  236. @EnableWebSecurity
  237. public class SecurityConfig {
  238. @Bean
  239. SecurityFilterChain securityFilterChain(HttpSecurity http, RememberMeServices rememberMeServices) throws Exception {
  240. http
  241. // ...
  242. .rememberMe((remember) -> remember
  243. .rememberMeServices(rememberMeServices)
  244. );
  245. return http.build();
  246. }
  247. @Bean
  248. RememberMeServices rememberMeServices(UserDetailsService userDetailsService) {
  249. RememberMeTokenAlgorithm encodingAlgorithm = RememberMeTokenAlgorithm.SHA256;
  250. TokenBasedRememberMeServices rememberMe = new TokenBasedRememberMeServices(myKey, userDetailsService, encodingAlgorithm);
  251. rememberMe.setMatchingAlgorithm(RememberMeTokenAlgorithm.SHA256);
  252. return rememberMe;
  253. }
  254. }
  255. ----
  256. .XML
  257. [source,xml,role="secondary"]
  258. ----
  259. <http>
  260. <remember-me services-ref="rememberMeServices"/>
  261. </http>
  262. <bean id="rememberMeServices" class=
  263. "org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
  264. <property name="userDetailsService" ref="myUserDetailsService"/>
  265. <property name="key" value="springRocks"/>
  266. <property name="matchingAlgorithm" value="SHA256"/>
  267. <property name="encodingAlgorithm" value="SHA256"/>
  268. </bean>
  269. ----
  270. ====
  271. If you are having problems with the Spring Security 6 defaults, you can explicitly opt into 5.8 defaults using the following configuration:
  272. .Use MD5 for both encoding and matching algorithms
  273. ====
  274. .Java
  275. [source,java,role="primary"]
  276. ----
  277. @Configuration
  278. @EnableWebSecurity
  279. public class SecurityConfig {
  280. @Bean
  281. SecurityFilterChain securityFilterChain(HttpSecurity http, RememberMeServices rememberMeServices) throws Exception {
  282. http
  283. // ...
  284. .rememberMe((remember) -> remember
  285. .rememberMeServices(rememberMeServices)
  286. );
  287. return http.build();
  288. }
  289. @Bean
  290. RememberMeServices rememberMeServices(UserDetailsService userDetailsService) {
  291. RememberMeTokenAlgorithm encodingAlgorithm = RememberMeTokenAlgorithm.MD5;
  292. TokenBasedRememberMeServices rememberMe = new TokenBasedRememberMeServices(myKey, userDetailsService, encodingAlgorithm);
  293. rememberMe.setMatchingAlgorithm(RememberMeTokenAlgorithm.MD5);
  294. return rememberMe;
  295. }
  296. }
  297. ----
  298. .XML
  299. [source,xml,role="secondary"]
  300. ----
  301. <http>
  302. <remember-me services-ref="rememberMeServices"/>
  303. </http>
  304. <bean id="rememberMeServices" class=
  305. "org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
  306. <property name="userDetailsService" ref="myUserDetailsService"/>
  307. <property name="key" value="springRocks"/>
  308. <property name="matchingAlgorithm" value="MD5"/>
  309. <property name="encodingAlgorithm" value="MD5"/>
  310. </bean>
  311. ----
  312. ====
  313. == Reactive
  314. === Use `AuthorizationManager` for Method Security
  315. In 6.0, `@EnableReactiveMethodSecurity` defaults `useAuthorizationManager` to `true`.
  316. So, to complete migration, {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableReactiveMethodSecurity.html[`@EnableReactiveMethodSecurity`] remove the `useAuthorizationManager` attribute:
  317. ====
  318. .Java
  319. [source,java,role="primary"]
  320. ----
  321. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  322. ----
  323. .Kotlin
  324. [source,kotlin,role="secondary"]
  325. ----
  326. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  327. ----
  328. ====
  329. changes to:
  330. ====
  331. .Java
  332. [source,java,role="primary"]
  333. ----
  334. @EnableReactiveMethodSecurity
  335. ----
  336. .Kotlin
  337. [source,kotlin,role="secondary"]
  338. ----
  339. @EnableReactiveMethodSecurity
  340. ----
  341. ====
  342. '''
  343. [[revert]]
  344. 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.
  345. It's more important to stay on 6.0 and get the security improvements.
  346. == Revert Servlet
  347. == Revert Reactive