mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Merge branch '3.0.x' into 3.1.x
Closes gh-38248
This commit is contained in:
commit
e549380a59
@ -44,6 +44,7 @@ import org.springframework.util.StringUtils;
|
|||||||
* {@link Command} to encode passwords for use with Spring Security.
|
* {@link Command} to encode passwords for use with Spring Security.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Moritz Halbritter
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
public class EncodePasswordCommand extends OptionParsingCommand {
|
public class EncodePasswordCommand extends OptionParsingCommand {
|
||||||
@ -70,8 +71,8 @@ public class EncodePasswordCommand extends OptionParsingCommand {
|
|||||||
@Override
|
@Override
|
||||||
public Collection<HelpExample> getExamples() {
|
public Collection<HelpExample> getExamples() {
|
||||||
List<HelpExample> examples = new ArrayList<>();
|
List<HelpExample> examples = new ArrayList<>();
|
||||||
examples
|
examples.add(new HelpExample("To encode a password with the default (bcrypt) encoder",
|
||||||
.add(new HelpExample("To encode a password with the default encoder", "spring encodepassword mypassword"));
|
"spring encodepassword mypassword"));
|
||||||
examples.add(new HelpExample("To encode a password with pbkdf2", "spring encodepassword -a pbkdf2 mypassword"));
|
examples.add(new HelpExample("To encode a password with pbkdf2", "spring encodepassword -a pbkdf2 mypassword"));
|
||||||
return examples;
|
return examples;
|
||||||
}
|
}
|
||||||
@ -82,12 +83,16 @@ public class EncodePasswordCommand extends OptionParsingCommand {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void options() {
|
protected void options() {
|
||||||
this.algorithm = option(Arrays.asList("algorithm", "a"), "The algorithm to use").withRequiredArg()
|
this.algorithm = option(Arrays.asList("algorithm", "a"),
|
||||||
|
"The algorithm to use. Supported algorithms: "
|
||||||
|
+ StringUtils.collectionToDelimitedString(ENCODERS.keySet(), ", ")
|
||||||
|
+ ". The default algorithm uses bcrypt")
|
||||||
|
.withRequiredArg()
|
||||||
.defaultsTo("default");
|
.defaultsTo("default");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ExitStatus run(OptionSet options) throws Exception {
|
protected ExitStatus run(OptionSet options) {
|
||||||
if (options.nonOptionArguments().size() != 1) {
|
if (options.nonOptionArguments().size() != 1) {
|
||||||
Log.error("A single password option must be provided");
|
Log.error("A single password option must be provided");
|
||||||
return ExitStatus.ERROR;
|
return ExitStatus.ERROR;
|
||||||
|
@ -36,6 +36,7 @@ import static org.mockito.BDDMockito.then;
|
|||||||
* Tests for {@link EncodePasswordCommand}.
|
* Tests for {@link EncodePasswordCommand}.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Moritz Halbritter
|
||||||
*/
|
*/
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
class EncodePasswordCommandTests {
|
class EncodePasswordCommandTests {
|
||||||
@ -63,6 +64,17 @@ class EncodePasswordCommandTests {
|
|||||||
assertThat(status).isEqualTo(ExitStatus.OK);
|
assertThat(status).isEqualTo(ExitStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeWithDefaultShouldUseBcrypt() throws Exception {
|
||||||
|
EncodePasswordCommand command = new EncodePasswordCommand();
|
||||||
|
ExitStatus status = command.run("-a", "default", "boot");
|
||||||
|
then(this.log).should().info(assertArg((message) -> {
|
||||||
|
assertThat(message).startsWith("{bcrypt}");
|
||||||
|
assertThat(PasswordEncoderFactories.createDelegatingPasswordEncoder().matches("boot", message)).isTrue();
|
||||||
|
}));
|
||||||
|
assertThat(status).isEqualTo(ExitStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void encodeWithBCryptShouldUseBCrypt() throws Exception {
|
void encodeWithBCryptShouldUseBCrypt() throws Exception {
|
||||||
EncodePasswordCommand command = new EncodePasswordCommand();
|
EncodePasswordCommand command = new EncodePasswordCommand();
|
||||||
|
Loading…
Reference in New Issue
Block a user