Skip to content

Commit 3664877

Browse files
committed
Merge branch '2.7.x' into 3.0.x
Closes gh-38247
2 parents 26ff7a6 + fc6d4ef commit 3664877

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

spring-boot-project/spring-boot-tools/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/encodepassword/EncodePasswordCommand.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* {@link Command} to encode passwords for use with Spring Security.
4545
*
4646
* @author Phillip Webb
47+
* @author Moritz Halbritter
4748
* @since 2.0.0
4849
*/
4950
public class EncodePasswordCommand extends OptionParsingCommand {
@@ -70,8 +71,8 @@ public String getUsageHelp() {
7071
@Override
7172
public Collection<HelpExample> getExamples() {
7273
List<HelpExample> examples = new ArrayList<>();
73-
examples
74-
.add(new HelpExample("To encode a password with the default encoder", "spring encodepassword mypassword"));
74+
examples.add(new HelpExample("To encode a password with the default (bcrypt) encoder",
75+
"spring encodepassword mypassword"));
7576
examples.add(new HelpExample("To encode a password with pbkdf2", "spring encodepassword -a pbkdf2 mypassword"));
7677
return examples;
7778
}
@@ -82,12 +83,16 @@ private static final class EncodePasswordOptionHandler extends OptionHandler {
8283

8384
@Override
8485
protected void options() {
85-
this.algorithm = option(Arrays.asList("algorithm", "a"), "The algorithm to use").withRequiredArg()
86+
this.algorithm = option(Arrays.asList("algorithm", "a"),
87+
"The algorithm to use. Supported algorithms: "
88+
+ StringUtils.collectionToDelimitedString(ENCODERS.keySet(), ", ")
89+
+ ". The default algorithm uses bcrypt")
90+
.withRequiredArg()
8691
.defaultsTo("default");
8792
}
8893

8994
@Override
90-
protected ExitStatus run(OptionSet options) throws Exception {
95+
protected ExitStatus run(OptionSet options) {
9196
if (options.nonOptionArguments().size() != 1) {
9297
Log.error("A single password option must be provided");
9398
return ExitStatus.ERROR;

spring-boot-project/spring-boot-tools/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/encodepassword/EncodePasswordCommandTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* Tests for {@link EncodePasswordCommand}.
3838
*
3939
* @author Phillip Webb
40+
* @author Moritz Halbritter
4041
*/
4142
@ExtendWith(MockitoExtension.class)
4243
class EncodePasswordCommandTests {
@@ -67,6 +68,17 @@ void encodeWithNoAlgorithmShouldUseBcrypt() throws Exception {
6768
assertThat(status).isEqualTo(ExitStatus.OK);
6869
}
6970

71+
@Test
72+
void encodeWithDefaultShouldUseBcrypt() throws Exception {
73+
EncodePasswordCommand command = new EncodePasswordCommand();
74+
ExitStatus status = command.run("-a", "default", "boot");
75+
then(this.log).should().info(this.message.capture());
76+
assertThat(this.message.getValue()).startsWith("{bcrypt}");
77+
assertThat(PasswordEncoderFactories.createDelegatingPasswordEncoder().matches("boot", this.message.getValue()))
78+
.isTrue();
79+
assertThat(status).isEqualTo(ExitStatus.OK);
80+
}
81+
7082
@Test
7183
void encodeWithBCryptShouldUseBCrypt() throws Exception {
7284
EncodePasswordCommand command = new EncodePasswordCommand();

0 commit comments

Comments
 (0)