Skip to content

Commit b9a5bdc

Browse files
committed
Merge branch '3.1.x'
Closes gh-38249
2 parents ec83f8b + e549380 commit b9a5bdc

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
@@ -36,6 +36,7 @@
3636
* Tests for {@link EncodePasswordCommand}.
3737
*
3838
* @author Phillip Webb
39+
* @author Moritz Halbritter
3940
*/
4041
@ExtendWith(MockitoExtension.class)
4142
class EncodePasswordCommandTests {
@@ -63,6 +64,17 @@ void encodeWithNoAlgorithmShouldUseBcrypt() throws Exception {
6364
assertThat(status).isEqualTo(ExitStatus.OK);
6465
}
6566

67+
@Test
68+
void encodeWithDefaultShouldUseBcrypt() throws Exception {
69+
EncodePasswordCommand command = new EncodePasswordCommand();
70+
ExitStatus status = command.run("-a", "default", "boot");
71+
then(this.log).should().info(assertArg((message) -> {
72+
assertThat(message).startsWith("{bcrypt}");
73+
assertThat(PasswordEncoderFactories.createDelegatingPasswordEncoder().matches("boot", message)).isTrue();
74+
}));
75+
assertThat(status).isEqualTo(ExitStatus.OK);
76+
}
77+
6678
@Test
6779
void encodeWithBCryptShouldUseBCrypt() throws Exception {
6880
EncodePasswordCommand command = new EncodePasswordCommand();

0 commit comments

Comments
 (0)