password-storage.adoc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. [[authentication-password-storage]]
  2. = Password Storage
  3. Spring Security's `PasswordEncoder` interface is used to perform a one-way transformation of a password to let the password be stored securely.
  4. Given `PasswordEncoder` is a one-way transformation, it is not useful when the password transformation needs to be two-way (such as storing credentials used to authenticate to a database).
  5. Typically, `PasswordEncoder` is used for storing a password that needs to be compared to a user-provided password at the time of authentication.
  6. [[authentication-password-storage-history]]
  7. == Password Storage History
  8. Throughout the years, the standard mechanism for storing passwords has evolved.
  9. In the beginning, passwords were stored in plaintext.
  10. The passwords were assumed to be safe because the data store the passwords were saved in required credentials to access it.
  11. However, malicious users were able to find ways to get large "`data dumps`" of usernames and passwords by using attacks such as SQL Injection.
  12. As more and more user credentials became public, security experts realized that we needed to do more to protect users' passwords.
  13. Developers were then encouraged to store passwords after running them through a one way hash, such as SHA-256.
  14. When a user tried to authenticate, the hashed password would be compared to the hash of the password that they typed.
  15. This meant that the system only needed to store the one-way hash of the password.
  16. If a breach occurred, only the one-way hashes of the passwords were exposed.
  17. Since the hashes were one-way and it was computationally difficult to guess the passwords given the hash, it would not be worth the effort to figure out each password in the system.
  18. To defeat this new system, malicious users decided to create lookup tables known as https://en.wikipedia.org/wiki/Rainbow_table[Rainbow Tables].
  19. Rather than doing the work of guessing each password every time, they computed the password once and stored it in a lookup table.
  20. To mitigate the effectiveness of Rainbow Tables, developers were encouraged to use salted passwords.
  21. Instead of using just the password as input to the hash function, random bytes (known as salt) would be generated for every user's password.
  22. The salt and the user's password would be run through the hash function to produce a unique hash.
  23. The salt would be stored alongside the user's password in clear text.
  24. Then when a user tried to authenticate, the hashed password would be compared to the hash of the stored salt and the password that they typed.
  25. The unique salt meant that Rainbow Tables were no longer effective because the hash was different for every salt and password combination.
  26. In modern times, we realize that cryptographic hashes (like SHA-256) are no longer secure.
  27. The reason is that with modern hardware we can perform billions of hash calculations a second.
  28. This means that we can crack each password individually with ease.
  29. Developers are now encouraged to leverage adaptive one-way functions to store a password.
  30. Validation of passwords with adaptive one-way functions are intentionally resource-intensive (they intentionally use a lot of CPU, memory, or other resources).
  31. An adaptive one-way function allows configuring a "`work factor`" that can grow as hardware gets better.
  32. We recommend that the "`work factor`" be tuned to take about one second to verify a password on your system.
  33. This trade off is to make it difficult for attackers to crack the password, but not so costly that it puts excessive burden on your own system or irritates users.
  34. Spring Security has attempted to provide a good starting point for the "`work factor`", but we encourage users to customize the "`work factor`" for their own system, since the performance varies drastically from system to system.
  35. Examples of adaptive one-way functions that should be used include <<authentication-password-storage-bcrypt,bcrypt>>, <<authentication-password-storage-pbkdf2,PBKDF2>>, <<authentication-password-storage-scrypt,scrypt>>, and <<authentication-password-storage-argon2,argon2>>.
  36. Because adaptive one-way functions are intentionally resource intensive, validating a username and password for every request can significantly degrade the performance of an application.
  37. There is nothing Spring Security (or any other library) can do to speed up the validation of the password, since security is gained by making the validation resource intensive.
  38. Users are encouraged to exchange the long term credentials (that is, username and password) for a short term credential (such as a session, and OAuth Token, and so on).
  39. The short term credential can be validated quickly without any loss in security.
  40. [[authentication-password-storage-dpe]]
  41. == DelegatingPasswordEncoder
  42. Prior to Spring Security 5.0, the default `PasswordEncoder` was `NoOpPasswordEncoder`, which required plain-text passwords.
  43. Based on the <<authentication-password-storage-history,Password History>> section, you might expect that the default `PasswordEncoder` would now be something like `BCryptPasswordEncoder`.
  44. However, this ignores three real world problems:
  45. - Many applications use old password encodings that cannot easily migrate.
  46. - The best practice for password storage will change again.
  47. - As a framework, Spring Security cannot make breaking changes frequently.
  48. Instead Spring Security introduces `DelegatingPasswordEncoder`, which solves all of the problems by:
  49. - Ensuring that passwords are encoded by using the current password storage recommendations
  50. - Allowing for validating passwords in modern and legacy formats
  51. - Allowing for upgrading the encoding in the future
  52. You can easily construct an instance of `DelegatingPasswordEncoder` by using `PasswordEncoderFactories`:
  53. .Create Default DelegatingPasswordEncoder
  54. ====
  55. .Java
  56. [source,java,role="primary"]
  57. ----
  58. PasswordEncoder passwordEncoder =
  59. PasswordEncoderFactories.createDelegatingPasswordEncoder();
  60. ----
  61. .Kotlin
  62. [source,kotlin,role="secondary"]
  63. ----
  64. val passwordEncoder: PasswordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder()
  65. ----
  66. ====
  67. Alternatively, you can create your own custom instance:
  68. .Create Custom DelegatingPasswordEncoder
  69. ====
  70. .Java
  71. [source,java,role="primary"]
  72. ----
  73. String idForEncode = "bcrypt";
  74. Map encoders = new HashMap<>();
  75. encoders.put(idForEncode, new BCryptPasswordEncoder());
  76. encoders.put("noop", NoOpPasswordEncoder.getInstance());
  77. encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());
  78. encoders.put("scrypt", new SCryptPasswordEncoder());
  79. encoders.put("sha256", new StandardPasswordEncoder());
  80. PasswordEncoder passwordEncoder =
  81. new DelegatingPasswordEncoder(idForEncode, encoders);
  82. ----
  83. .Kotlin
  84. [source,kotlin,role="secondary"]
  85. ----
  86. val idForEncode = "bcrypt"
  87. val encoders: MutableMap<String, PasswordEncoder> = mutableMapOf()
  88. encoders[idForEncode] = BCryptPasswordEncoder()
  89. encoders["noop"] = NoOpPasswordEncoder.getInstance()
  90. encoders["pbkdf2"] = Pbkdf2PasswordEncoder()
  91. encoders["scrypt"] = SCryptPasswordEncoder()
  92. encoders["sha256"] = StandardPasswordEncoder()
  93. val passwordEncoder: PasswordEncoder = DelegatingPasswordEncoder(idForEncode, encoders)
  94. ----
  95. ====
  96. [[authentication-password-storage-dpe-format]]
  97. === Password Storage Format
  98. The general format for a password is:
  99. .DelegatingPasswordEncoder Storage Format
  100. ====
  101. [source,text,attrs="-attributes"]
  102. ----
  103. {id}encodedPassword
  104. ----
  105. ====
  106. `id` is an identifier that is used to look up which `PasswordEncoder` should be used and `encodedPassword` is the original encoded password for the selected `PasswordEncoder`.
  107. The `id` must be at the beginning of the password, start with `{`, and end with `}`.
  108. If the `id` cannot be found, the `id` is set to null.
  109. For example, the following might be a list of passwords encoded using different `id` values.
  110. All of the original passwords are `password`.
  111. .DelegatingPasswordEncoder Encoded Passwords Example
  112. ====
  113. [source,text,attrs="-attributes"]
  114. ----
  115. {bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG // <1>
  116. {noop}password // <2>
  117. {pbkdf2}5d923b44a6d129f3ddf3e3c8d29412723dcbde72445e8ef6bf3b508fbf17fa4ed4d6b99ca763d8dc // <3>
  118. {scrypt}$e0801$8bWJaSu2IKSn9Z9kM+TPXfOc/9bdYSrN1oD9qfVThWEwdRTnO7re7Ei+fUZRJ68k9lTyuTeUp4of4g24hHnazw==$OAOec05+bXxvuu/1qZ6NUR+xQYvYv7BeL1QxwRpY5Pc= // <4>
  119. {sha256}97cde38028ad898ebc02e690819fa220e88c62e0699403e94fff291cfffaf8410849f27605abcbc0 // <5>
  120. ----
  121. ====
  122. <1> The first password has a `PasswordEncoder` id of `bcrypt` and an `encodedPassword` value of `$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG`.
  123. When matching, it would delegate to `BCryptPasswordEncoder`
  124. <2> The second password has a `PasswordEncoder` id of `noop` and `encodedPassword` value of `password`.
  125. When matching, it would delegate to `NoOpPasswordEncoder`
  126. <3> The third password has a `PasswordEncoder` id of `pbkdf2` and `encodedPassword` value of `5d923b44a6d129f3ddf3e3c8d29412723dcbde72445e8ef6bf3b508fbf17fa4ed4d6b99ca763d8dc`.
  127. When matching, it would delegate to `Pbkdf2PasswordEncoder`
  128. <4> The fourth password has a `PasswordEncoder` id of `scrypt` and `encodedPassword` value of `$e0801$8bWJaSu2IKSn9Z9kM+TPXfOc/9bdYSrN1oD9qfVThWEwdRTnO7re7Ei+fUZRJ68k9lTyuTeUp4of4g24hHnazw==$OAOec05+bXxvuu/1qZ6NUR+xQYvYv7BeL1QxwRpY5Pc=`
  129. When matching, it would delegate to `SCryptPasswordEncoder`
  130. <5> The final password has a `PasswordEncoder` id of `sha256` and `encodedPassword` value of `97cde38028ad898ebc02e690819fa220e88c62e0699403e94fff291cfffaf8410849f27605abcbc0`.
  131. When matching, it would delegate to `StandardPasswordEncoder`
  132. [NOTE]
  133. ====
  134. Some users might be concerned that the storage format is provided for a potential hacker.
  135. This is not a concern because the storage of the password does not rely on the algorithm being a secret.
  136. Additionally, most formats are easy for an attacker to figure out without the prefix.
  137. For example, BCrypt passwords often start with `$2a$`.
  138. ====
  139. [[authentication-password-storage-dpe-encoding]]
  140. === Password Encoding
  141. The `idForEncode` passed into the constructor determines which `PasswordEncoder` is used for encoding passwords.
  142. In the `DelegatingPasswordEncoder` we constructed earlier, that means that the result of encoding `password` is delegated to `BCryptPasswordEncoder` and be prefixed with `+{bcrypt}+`.
  143. The end result looks like the following example:
  144. .DelegatingPasswordEncoder Encode Example
  145. ====
  146. [source,text,attrs="-attributes"]
  147. ----
  148. {bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG
  149. ----
  150. ====
  151. [[authentication-password-storage-dpe-matching]]
  152. === Password Matching
  153. Matching is based upon the `+{id}+` and the mapping of the `id` to the `PasswordEncoder` provided in the constructor.
  154. Our example in <<authentication-password-storage-dpe-format,Password Storage Format>> provides a working example of how this is done.
  155. By default, the result of invoking `matches(CharSequence, String)` with a password and an `id` that is not mapped (including a null id) results in an `IllegalArgumentException`.
  156. This behavior can be customized by using `DelegatingPasswordEncoder.setDefaultPasswordEncoderForMatches(PasswordEncoder)`.
  157. By using the `id`, we can match on any password encoding but encode passwords by using the most modern password encoding.
  158. This is important, because unlike encryption, password hashes are designed so that there is no simple way to recover the plaintext.
  159. Since there is no way to recover the plaintext, it is difficult to migrate the passwords.
  160. While it is simple for users to migrate `NoOpPasswordEncoder`, we chose to include it by default to make it simple for the getting-started experience.
  161. [[authentication-password-storage-dep-getting-started]]
  162. === Getting Started Experience
  163. If you are putting together a demo or a sample, it is a bit cumbersome to take time to hash the passwords of your users.
  164. There are convenience mechanisms to make this easier, but this is still not intended for production.
  165. .withDefaultPasswordEncoder Example
  166. ====
  167. .Java
  168. [source,java,role="primary",attrs="-attributes"]
  169. ----
  170. User user = User.withDefaultPasswordEncoder()
  171. .username("user")
  172. .password("password")
  173. .roles("user")
  174. .build();
  175. System.out.println(user.getPassword());
  176. // {bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG
  177. ----
  178. .Kotlin
  179. [source,kotlin,role="secondary",attrs="-attributes"]
  180. ----
  181. val user = User.withDefaultPasswordEncoder()
  182. .username("user")
  183. .password("password")
  184. .roles("user")
  185. .build()
  186. println(user.password)
  187. // {bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG
  188. ----
  189. ====
  190. If you are creating multiple users, you can also reuse the builder:
  191. .withDefaultPasswordEncoder Reusing the Builder
  192. ====
  193. .Java
  194. [source,java,role="primary"]
  195. ----
  196. UserBuilder users = User.withDefaultPasswordEncoder();
  197. User user = users
  198. .username("user")
  199. .password("password")
  200. .roles("USER")
  201. .build();
  202. User admin = users
  203. .username("admin")
  204. .password("password")
  205. .roles("USER","ADMIN")
  206. .build();
  207. ----
  208. .Kotlin
  209. [source,kotlin,role="secondary"]
  210. ----
  211. val users = User.withDefaultPasswordEncoder()
  212. val user = users
  213. .username("user")
  214. .password("password")
  215. .roles("USER")
  216. .build()
  217. val admin = users
  218. .username("admin")
  219. .password("password")
  220. .roles("USER", "ADMIN")
  221. .build()
  222. ----
  223. ====
  224. This does hash the password that is stored, but the passwords are still exposed in memory and in the compiled source code.
  225. Therefore, it is still not considered secure for a production environment.
  226. For production, you should <<authentication-password-storage-boot-cli,hash your passwords externally>>.
  227. [[authentication-password-storage-boot-cli]]
  228. === Encode with Spring Boot CLI
  229. The easiest way to properly encode your password is to use the https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-cli.html[Spring Boot CLI].
  230. For example, the following example encodes the password of `password` for use with <<authentication-password-storage-dpe>>:
  231. .Spring Boot CLI encodepassword Example
  232. ====
  233. [source,attrs="-attributes"]
  234. ----
  235. spring encodepassword password
  236. {bcrypt}$2a$10$X5wFBtLrL/kHcmrOGGTrGufsBX8CJ0WpQpF3pgeuxBB/H73BK1DW6
  237. ----
  238. ====
  239. [[authentication-password-storage-dpe-troubleshoot]]
  240. === Troubleshooting
  241. The following error occurs when one of the passwords that are stored has no `id`, as described in <<authentication-password-storage-dpe-format>>.
  242. ====
  243. ----
  244. java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
  245. at org.springframework.security.crypto.password.DelegatingPasswordEncoder$UnmappedIdPasswordEncoder.matches(DelegatingPasswordEncoder.java:233)
  246. at org.springframework.security.crypto.password.DelegatingPasswordEncoder.matches(DelegatingPasswordEncoder.java:196)
  247. ----
  248. ====
  249. The easiest way to resolve it is to figure out how your passwords are currently being stored and explicitly provide the correct `PasswordEncoder`.
  250. If you are migrating from Spring Security 4.2.x, you can revert to the previous behavior by <<authentication-password-storage-configuration,exposing a `NoOpPasswordEncoder` bean>>.
  251. Alternatively, you can prefix all of your passwords with the correct `id` and continue to use `DelegatingPasswordEncoder`.
  252. For example, if you are using BCrypt, you would migrate your password from something like:
  253. ====
  254. ----
  255. $2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG
  256. ----
  257. ====
  258. to
  259. ====
  260. [source,attrs="-attributes"]
  261. ----
  262. {bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG
  263. ----
  264. ====
  265. For a complete listing of the mappings, see the Javadoc for
  266. https://docs.spring.io/spring-security/site/docs/5.0.x/api/org/springframework/security/crypto/factory/PasswordEncoderFactories.html[`PasswordEncoderFactories`].
  267. [[authentication-password-storage-bcrypt]]
  268. == BCryptPasswordEncoder
  269. The `BCryptPasswordEncoder` implementation uses the widely supported https://en.wikipedia.org/wiki/Bcrypt[bcrypt] algorithm to hash the passwords.
  270. To make it more resistant to password cracking, bcrypt is deliberately slow.
  271. Like other adaptive one-way functions, it should be tuned to take about 1 second to verify a password on your system.
  272. The default implementationThe default implementation of `BCryptPasswordEncoder` uses strength 10 as mentioned in the Javadoc of https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/crypto/bcrypt/BCryptPasswordEncoder.html[`BCryptPasswordEncoder`]. You are encouraged to
  273. tune and test the strength parameter on your own system so that it takes roughly 1 second to verify a password.
  274. .BCryptPasswordEncoder
  275. ====
  276. .Java
  277. [source,java,role="primary"]
  278. ----
  279. // Create an encoder with strength 16
  280. BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(16);
  281. String result = encoder.encode("myPassword");
  282. assertTrue(encoder.matches("myPassword", result));
  283. ----
  284. .Kotlin
  285. [source,kotlin,role="secondary"]
  286. ----
  287. // Create an encoder with strength 16
  288. val encoder = BCryptPasswordEncoder(16)
  289. val result: String = encoder.encode("myPassword")
  290. assertTrue(encoder.matches("myPassword", result))
  291. ----
  292. ====
  293. [[authentication-password-storage-argon2]]
  294. == Argon2PasswordEncoder
  295. The `Argon2PasswordEncoder` implementation uses the https://en.wikipedia.org/wiki/Argon2[Argon2] algorithm to hash the passwords.
  296. Argon2 is the winner of the https://en.wikipedia.org/wiki/Password_Hashing_Competition[Password Hashing Competition].
  297. To defeat password cracking on custom hardware, Argon2 is a deliberately slow algorithm that requires large amounts of memory.
  298. Like other adaptive one-way functions, it should be tuned to take about 1 second to verify a password on your system.
  299. The current implementation of the `Argon2PasswordEncoder` requires BouncyCastle.
  300. .Argon2PasswordEncoder
  301. ====
  302. .Java
  303. [source,java,role="primary"]
  304. ----
  305. // Create an encoder with all the defaults
  306. Argon2PasswordEncoder encoder = new Argon2PasswordEncoder();
  307. String result = encoder.encode("myPassword");
  308. assertTrue(encoder.matches("myPassword", result));
  309. ----
  310. .Kotlin
  311. [source,kotlin,role="secondary"]
  312. ----
  313. // Create an encoder with all the defaults
  314. val encoder = Argon2PasswordEncoder()
  315. val result: String = encoder.encode("myPassword")
  316. assertTrue(encoder.matches("myPassword", result))
  317. ----
  318. ====
  319. [[authentication-password-storage-pbkdf2]]
  320. == Pbkdf2PasswordEncoder
  321. The `Pbkdf2PasswordEncoder` implementation uses the https://en.wikipedia.org/wiki/PBKDF2[PBKDF2] algorithm to hash the passwords.
  322. To defeat password cracking PBKDF2 is a deliberately slow algorithm.
  323. Like other adaptive one-way functions, it should be tuned to take about 1 second to verify a password on your system.
  324. This algorithm is a good choice when FIPS certification is required.
  325. .Pbkdf2PasswordEncoder
  326. ====
  327. .Java
  328. [source,java,role="primary"]
  329. ----
  330. // Create an encoder with all the defaults
  331. Pbkdf2PasswordEncoder encoder = new Pbkdf2PasswordEncoder();
  332. String result = encoder.encode("myPassword");
  333. assertTrue(encoder.matches("myPassword", result));
  334. ----
  335. .Kotlin
  336. [source,kotlin,role="secondary"]
  337. ----
  338. // Create an encoder with all the defaults
  339. val encoder = Pbkdf2PasswordEncoder()
  340. val result: String = encoder.encode("myPassword")
  341. assertTrue(encoder.matches("myPassword", result))
  342. ----
  343. ====
  344. [[authentication-password-storage-scrypt]]
  345. == SCryptPasswordEncoder
  346. The `SCryptPasswordEncoder` implementation uses the https://en.wikipedia.org/wiki/Scrypt[scrypt] algorithm to hash the passwords.
  347. To defeat password cracking on custom hardware, scrypt is a deliberately slow algorithm that requires large amounts of memory.
  348. Like other adaptive one-way functions, it should be tuned to take about 1 second to verify a password on your system.
  349. .SCryptPasswordEncoder
  350. ====
  351. .Java
  352. [source,java,role="primary"]
  353. ----
  354. // Create an encoder with all the defaults
  355. SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
  356. String result = encoder.encode("myPassword");
  357. assertTrue(encoder.matches("myPassword", result));
  358. ----
  359. .Kotlin
  360. [source,kotlin,role="secondary"]
  361. ----
  362. // Create an encoder with all the defaults
  363. val encoder = SCryptPasswordEncoder()
  364. val result: String = encoder.encode("myPassword")
  365. assertTrue(encoder.matches("myPassword", result))
  366. ----
  367. ====
  368. [[authentication-password-storage-other]]
  369. == Other ``PasswordEncoder``s
  370. There are a significant number of other `PasswordEncoder` implementations that exist entirely for backward compatibility.
  371. They are all deprecated to indicate that they are no longer considered secure.
  372. However, there are no plans to remove them, since it is difficult to migrate existing legacy systems.
  373. [[authentication-password-storage-configuration]]
  374. == Password Storage Configuration
  375. Spring Security uses <<authentication-password-storage-dpe>> by default.
  376. However, you can customize this by exposing a `PasswordEncoder` as a Spring bean.
  377. If you are migrating from Spring Security 4.2.x, you can revert to the previous behavior by exposing a `NoOpPasswordEncoder` bean.
  378. [WARNING]
  379. ====
  380. Reverting to `NoOpPasswordEncoder` is not considered to be secure.
  381. You should instead migrate to using `DelegatingPasswordEncoder` to support secure password encoding.
  382. ====
  383. .NoOpPasswordEncoder
  384. ====
  385. .Java
  386. [source,java,role="primary"]
  387. ----
  388. @Bean
  389. public static NoOpPasswordEncoder passwordEncoder() {
  390. return NoOpPasswordEncoder.getInstance();
  391. }
  392. ----
  393. .XML
  394. [source,xml,role="secondary"]
  395. ----
  396. <b:bean id="passwordEncoder"
  397. class="org.springframework.security.crypto.password.NoOpPasswordEncoder" factory-method="getInstance"/>
  398. ----
  399. .Kotlin
  400. [source,kotlin,role="secondary"]
  401. ----
  402. @Bean
  403. fun passwordEncoder(): PasswordEncoder {
  404. return NoOpPasswordEncoder.getInstance();
  405. }
  406. ----
  407. ====
  408. [NOTE]
  409. ====
  410. XML Configuration requires the `NoOpPasswordEncoder` bean name to be `passwordEncoder`.
  411. ====
  412. [[authentication-change-password-configuration]]
  413. == Change Password Configuration
  414. Most applications that allow a user to specify a password also require a feature for updating that password.
  415. https://w3c.github.io/webappsec-change-password-url/[A Well-Know URL for Changing Passwords] indicates a mechanism by which password managers can discover the password update endpoint for a given application.
  416. You can configure Spring Security to provide this discovery endpoint.
  417. For example, if the change password endpoint in your application is `/change-password`, then you can configure Spring Security like so:
  418. .Default Change Password Endpoint
  419. ====
  420. .Java
  421. [source,java,role="primary"]
  422. ----
  423. http
  424. .passwordManagement(Customizer.withDefaults())
  425. ----
  426. .XML
  427. [source,xml,role="secondary"]
  428. ----
  429. <sec:password-management/>
  430. ----
  431. .Kotlin
  432. [source,kotlin,role="secondary"]
  433. ----
  434. http {
  435. passwordManagement { }
  436. }
  437. ----
  438. ====
  439. Then, when a password manager navigates to `/.well-known/change-password` then Spring Security will redirect your endpoint, `/change-password`.
  440. Or, if your endpoint is something other than `/change-password`, you can also specify that like so:
  441. .Change Password Endpoint
  442. ====
  443. .Java
  444. [source,java,role="primary"]
  445. ----
  446. http
  447. .passwordManagement((management) -> management
  448. .changePasswordPage("/update-password")
  449. )
  450. ----
  451. .XML
  452. [source,xml,role="secondary"]
  453. ----
  454. <sec:password-management change-password-page="/update-password"/>
  455. ----
  456. .Kotlin
  457. [source,kotlin,role="secondary"]
  458. ----
  459. http {
  460. passwordManagement {
  461. changePasswordPage = "/update-password"
  462. }
  463. }
  464. ----
  465. ====
  466. With the above configuration, when a password manager navigates to `/.well-known/change-password`, then Spring Security will redirect to `/update-password`.