Skip to content

Commit a0e5af3

Browse files
Improve PasswordEncoder Error Messaging
Closes gh-14880
1 parent 6ca0162 commit a0e5af3

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

crypto/src/main/java/org/springframework/security/crypto/password/DelegatingPasswordEncoder.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121

22+
import org.springframework.util.StringUtils;
23+
2224
/**
2325
* A password encoder that delegates to another PasswordEncoder based upon a prefixed
2426
* identifier.
@@ -129,6 +131,10 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
129131

130132
private static final String DEFAULT_ID_SUFFIX = "}";
131133

134+
public static final String NO_PASSWORD_ENCODER_MAPPED = "There is no PasswordEncoder mapped for the id \"%s\"";
135+
136+
public static final String NO_PASSWORD_ENCODER_PREFIX = "You have entered a password with no PasswordEncoder. If that is your intent, it should be prefixed with `{noop}`.";
137+
132138
private final String idPrefix;
133139

134140
private final String idSuffix;
@@ -286,15 +292,10 @@ public String encode(CharSequence rawPassword) {
286292
@Override
287293
public boolean matches(CharSequence rawPassword, String prefixEncodedPassword) {
288294
String id = extractId(prefixEncodedPassword);
289-
checkIfStringIsEmptyOrNull(id);
290-
throw new IllegalArgumentException("There is no PasswordEncoder mapped for the id \"" + id + "\"");
291-
}
292-
293-
private void checkIfStringIsEmptyOrNull(String string) {
294-
if (string == null || string.isEmpty()) {
295-
throw new IllegalArgumentException(
296-
"You have entered a password with no PasswordEncoder. If that is your intent, it should be prefixed with `{noop}`.");
295+
if (StringUtils.hasText(id)) {
296+
throw new IllegalArgumentException(String.format(NO_PASSWORD_ENCODER_MAPPED, id));
297297
}
298+
throw new IllegalArgumentException(NO_PASSWORD_ENCODER_PREFIX);
298299
}
299300

300301
}

crypto/src/test/java/org/springframework/security/crypto/password/DelegatingPasswordEncoderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)