Skip to content

Commit 71be24c

Browse files
committed
Add new factory method for creating DelegatingPasswordEncoder
See spring-projectsgh-4666
1 parent c7f4160 commit 71be24c

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

crypto/src/main/java/org/springframework/security/crypto/factory/PasswordEncoderFactories.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,35 @@ public class PasswordEncoderFactories {
5151
* @return the {@link PasswordEncoder} to use
5252
*/
5353
public static PasswordEncoder createDelegatingPasswordEncoder() {
54-
String encodingId = "bcrypt";
54+
return createDelegatingPasswordEncoder("bcrypt");
55+
}
56+
57+
/**
58+
* Creates a {@link DelegatingPasswordEncoder} with default mappings. Additional
59+
* mappings may be added and the encoding will be updated to conform with best
60+
* practices. However, due to the nature of {@link DelegatingPasswordEncoder} the
61+
* updates should not impact users. The mappings current are:
62+
*
63+
* <ul>
64+
* <li>bcrypt - {@link BCryptPasswordEncoder}</li>
65+
* <li>noop - {@link NoOpPasswordEncoder}</li>
66+
* <li>pbkdf2 - {@link Pbkdf2PasswordEncoder}</li>
67+
* <li>scrypt - {@link SCryptPasswordEncoder}</li>
68+
* <li>sha256 - {@link StandardPasswordEncoder}</li>
69+
* </ul>
70+
*
71+
* @param idForEncode the id used to lookup which {@link PasswordEncoder} should be
72+
* used for {@link PasswordEncoder#encode(CharSequence)}
73+
* @return the {@link PasswordEncoder} to use
74+
*/
75+
public static PasswordEncoder createDelegatingPasswordEncoder(String idForEncode) {
5576
Map<String,PasswordEncoder> encoders = new HashMap<>();
56-
encoders.put(encodingId, new BCryptPasswordEncoder());
77+
encoders.put("bcrypt", new BCryptPasswordEncoder());
5778
encoders.put("noop", NoOpPasswordEncoder.getInstance());
5879
encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());
5980
encoders.put("scrypt", new SCryptPasswordEncoder());
6081
encoders.put("sha256", new StandardPasswordEncoder());
61-
62-
return new DelegatingPasswordEncoder(encodingId, encoders);
82+
return new DelegatingPasswordEncoder(idForEncode, encoders);
6383
}
6484

6585
private PasswordEncoderFactories() {}

crypto/src/test/java/org/springframework/security/crypto/factory/PasswordEncoderFactoriesTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,13 @@ public void matchesWhenSha256ThenWorks() {
6868
assertThat(this.encoder.matches(this.rawPassword, encodedPassword)).isTrue();
6969
}
7070

71+
@Test
72+
public void encodeUsingSpecifiedEncoder() {
73+
PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder("pbkdf2");
74+
String encodedPassword = passwordEncoder.encode(this.rawPassword);
75+
76+
assertThat(encodedPassword).startsWith("{pbkdf2}");
77+
assertThat(passwordEncoder.matches(this.rawPassword, encodedPassword)).isTrue();
78+
}
79+
7180
}

0 commit comments

Comments
 (0)