authentication.adoc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. = Authentication Migrations
  2. The following steps relate to changes around how authentication is performed.
  3. [[servlet-opt-in-sha256-rememberme]]
  4. == Use SHA-256 in Remember Me
  5. The `TokenBasedRememberMeServices` implementation now supports SHA-256 for the Remember Me token and this is the default in Spring Security 6.
  6. 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.
  7. 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.
  8. If the algorithm name is not present, then the `matchingAlgorithm` property is used to check the token.
  9. This allows for a smooth transition from MD5 to SHA-256.
  10. 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.
  11. 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.
  12. [[servlet-opt-in-sha256-sha256-encoding]]
  13. .Use Spring Security 6 defaults for encoding, SHA-256 for encoding and MD5 for matching
  14. [tabs]
  15. ======
  16. Java::
  17. +
  18. [source,java,role="primary"]
  19. ----
  20. @Configuration
  21. @EnableWebSecurity
  22. public class SecurityConfig {
  23. @Bean
  24. SecurityFilterChain securityFilterChain(HttpSecurity http, RememberMeServices rememberMeServices) throws Exception {
  25. http
  26. // ...
  27. .rememberMe((remember) -> remember
  28. .rememberMeServices(rememberMeServices)
  29. );
  30. return http.build();
  31. }
  32. @Bean
  33. RememberMeServices rememberMeServices(UserDetailsService userDetailsService) {
  34. RememberMeTokenAlgorithm encodingAlgorithm = RememberMeTokenAlgorithm.SHA256;
  35. TokenBasedRememberMeServices rememberMe = new TokenBasedRememberMeServices(myKey, userDetailsService, encodingAlgorithm);
  36. rememberMe.setMatchingAlgorithm(RememberMeTokenAlgorithm.MD5);
  37. return rememberMe;
  38. }
  39. }
  40. ----
  41. XML::
  42. +
  43. [source,xml,role="secondary"]
  44. ----
  45. <http>
  46. <remember-me services-ref="rememberMeServices"/>
  47. </http>
  48. <bean id="rememberMeServices" class=
  49. "org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
  50. <property name="userDetailsService" ref="myUserDetailsService"/>
  51. <property name="key" value="springRocks"/>
  52. <property name="matchingAlgorithm" value="MD5"/>
  53. <property name="encodingAlgorithm" value="SHA256"/>
  54. </bean>
  55. ----
  56. ======
  57. 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?
  58. 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).
  59. By that time, all the tokens generated with MD5 will have expired.
  60. .Use Spring Security 6 defaults, SHA-256 for both encoding and matching
  61. [tabs]
  62. ======
  63. Java::
  64. +
  65. [source,java,role="primary"]
  66. ----
  67. @Configuration
  68. @EnableWebSecurity
  69. public class SecurityConfig {
  70. @Bean
  71. SecurityFilterChain securityFilterChain(HttpSecurity http, RememberMeServices rememberMeServices) throws Exception {
  72. http
  73. // ...
  74. .rememberMe((remember) -> remember
  75. .rememberMeServices(rememberMeServices)
  76. );
  77. return http.build();
  78. }
  79. @Bean
  80. RememberMeServices rememberMeServices(UserDetailsService userDetailsService) {
  81. RememberMeTokenAlgorithm encodingAlgorithm = RememberMeTokenAlgorithm.SHA256;
  82. TokenBasedRememberMeServices rememberMe = new TokenBasedRememberMeServices(myKey, userDetailsService, encodingAlgorithm);
  83. rememberMe.setMatchingAlgorithm(RememberMeTokenAlgorithm.SHA256);
  84. return rememberMe;
  85. }
  86. }
  87. ----
  88. XML::
  89. +
  90. [source,xml,role="secondary"]
  91. ----
  92. <http>
  93. <remember-me services-ref="rememberMeServices"/>
  94. </http>
  95. <bean id="rememberMeServices" class=
  96. "org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
  97. <property name="userDetailsService" ref="myUserDetailsService"/>
  98. <property name="key" value="springRocks"/>
  99. <property name="matchingAlgorithm" value="SHA256"/>
  100. <property name="encodingAlgorithm" value="SHA256"/>
  101. </bean>
  102. ----
  103. ======
  104. If you are having problems with the Spring Security 6 defaults, you can explicitly opt into 5.8 defaults using the following configuration:
  105. .Use MD5 for both encoding and matching algorithms
  106. [tabs]
  107. ======
  108. Java::
  109. +
  110. [source,java,role="primary"]
  111. ----
  112. @Configuration
  113. @EnableWebSecurity
  114. public class SecurityConfig {
  115. @Bean
  116. SecurityFilterChain securityFilterChain(HttpSecurity http, RememberMeServices rememberMeServices) throws Exception {
  117. http
  118. // ...
  119. .rememberMe((remember) -> remember
  120. .rememberMeServices(rememberMeServices)
  121. );
  122. return http.build();
  123. }
  124. @Bean
  125. RememberMeServices rememberMeServices(UserDetailsService userDetailsService) {
  126. RememberMeTokenAlgorithm encodingAlgorithm = RememberMeTokenAlgorithm.MD5;
  127. TokenBasedRememberMeServices rememberMe = new TokenBasedRememberMeServices(myKey, userDetailsService, encodingAlgorithm);
  128. rememberMe.setMatchingAlgorithm(RememberMeTokenAlgorithm.MD5);
  129. return rememberMe;
  130. }
  131. }
  132. ----
  133. XML::
  134. +
  135. [source,xml,role="secondary"]
  136. ----
  137. <http>
  138. <remember-me services-ref="rememberMeServices"/>
  139. </http>
  140. <bean id="rememberMeServices" class=
  141. "org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
  142. <property name="userDetailsService" ref="myUserDetailsService"/>
  143. <property name="key" value="springRocks"/>
  144. <property name="matchingAlgorithm" value="MD5"/>
  145. <property name="encodingAlgorithm" value="MD5"/>
  146. </bean>
  147. ----
  148. ======
  149. == Propagate ``AuthenticationServiceException``s
  150. {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`].
  151. 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.
  152. === Configure `AuthenticationFailureHandler` to rethrow ``AuthenticationServiceException``s
  153. To prepare for the 6.0 default, wire `AuthenticationFilter` instances with a `AuthenticationFailureHandler` that rethrows ``AuthenticationServiceException``s, like so:
  154. [tabs]
  155. ======
  156. Java::
  157. +
  158. [source,java,role="primary"]
  159. ----
  160. AuthenticationFilter authenticationFilter = new AuthenticationFilter(...);
  161. AuthenticationEntryPointFailureHandler handler = new AuthenticationEntryPointFailureHandler(...);
  162. handler.setRethrowAuthenticationServiceException(true);
  163. authenticationFilter.setAuthenticationFailureHandler(handler);
  164. ----
  165. Kotlin::
  166. +
  167. [source,kotlin,role="secondary"]
  168. ----
  169. val authenticationFilter: AuthenticationFilter = new AuthenticationFilter(...)
  170. val handler: AuthenticationEntryPointFailureHandler = new AuthenticationEntryPointFailureHandler(...)
  171. handler.setRethrowAuthenticationServiceException(true)
  172. authenticationFilter.setAuthenticationFailureHandler(handler)
  173. ----
  174. Xml::
  175. +
  176. [source,xml,role="secondary"]
  177. ----
  178. <bean id="authenticationFilter" class="org.springframework.security.web.authentication.AuthenticationFilter">
  179. <!-- ... -->
  180. <property ref="authenticationFailureHandler"/>
  181. </bean>
  182. <bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.AuthenticationEntryPointFailureHandler">
  183. <property name="rethrowAuthenticationServiceException" value="true"/>
  184. </bean>
  185. ----
  186. ======
  187. [[servlet-authenticationfailurehandler-opt-out]]
  188. === Opt-out Steps
  189. If rethrowing ``AuthenticationServiceException``s gives you trouble, you can set the value to false instead of taking the 6.0 default, like so:
  190. [tabs]
  191. ======
  192. Java::
  193. +
  194. [source,java,role="primary"]
  195. ----
  196. AuthenticationFilter authenticationFilter = new AuthenticationFilter(...);
  197. AuthenticationEntryPointFailureHandler handler = new AuthenticationEntryPointFailureHandler(...);
  198. handler.setRethrowAuthenticationServiceException(false);
  199. authenticationFilter.setAuthenticationFailureHandler(handler);
  200. ----
  201. Kotlin::
  202. +
  203. [source,kotlin,role="secondary"]
  204. ----
  205. val authenticationFilter: AuthenticationFilter = new AuthenticationFilter(...)
  206. val handler: AuthenticationEntryPointFailureHandler = new AuthenticationEntryPointFailureHandler(...)
  207. handler.setRethrowAuthenticationServiceException(false)
  208. authenticationFilter.setAuthenticationFailureHandler(handler)
  209. ----
  210. Xml::
  211. +
  212. [source,xml,role="secondary"]
  213. ----
  214. <bean id="authenticationFilter" class="org.springframework.security.web.authentication.AuthenticationFilter">
  215. <!-- ... -->
  216. <property ref="authenticationFailureHandler"/>
  217. </bean>
  218. <bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.AuthenticationEntryPointFailureHandler">
  219. <property name="rethrowAuthenticationServiceException" value="false"/>
  220. </bean>
  221. ----
  222. ======