Skip to content

Commit cbc61bf

Browse files
committed
Remove Reasons
- These aren't super helpful in the end. Applications can inspect the type and get a richer set of information.
1 parent a04035d commit cbc61bf

File tree

18 files changed

+188
-208
lines changed

18 files changed

+188
-208
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/PasswordManagementConfigurer.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,17 @@
1919
import org.springframework.context.ApplicationContext;
2020
import org.springframework.context.ApplicationContextAware;
2121
import org.springframework.security.authentication.password.ChangePasswordAdvisor;
22-
import org.springframework.security.authentication.password.ChangePasswordServiceAdvisor;
23-
import org.springframework.security.authentication.password.DelegatingChangePasswordAdvisor;
22+
import org.springframework.security.authentication.password.CompositeChangePasswordAdvisor;
23+
import org.springframework.security.authentication.password.UserDetailsChangePasswordAdvisor;
2424
import org.springframework.security.authentication.password.UserDetailsPasswordManager;
2525
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
2626
import org.springframework.security.web.RequestMatcherRedirectFilter;
2727
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
2828
import org.springframework.security.web.authentication.password.ChangeCompromisedPasswordAdvisor;
29-
import org.springframework.security.web.authentication.password.ChangePasswordAdviceHandler;
3029
import org.springframework.security.web.authentication.password.ChangePasswordAdviceRepository;
3130
import org.springframework.security.web.authentication.password.ChangePasswordAdviceSessionAuthenticationStrategy;
3231
import org.springframework.security.web.authentication.password.ChangePasswordAdvisingFilter;
3332
import org.springframework.security.web.authentication.password.HttpSessionChangePasswordAdviceRepository;
34-
import org.springframework.security.web.authentication.password.RedirectingChangePasswordAdviceHandler;
3533
import org.springframework.security.web.savedrequest.RequestCache;
3634
import org.springframework.security.web.savedrequest.RequestCacheAwareFilter;
3735
import org.springframework.util.Assert;
@@ -59,8 +57,6 @@ public final class PasswordManagementConfigurer<B extends HttpSecurityBuilder<B>
5957

6058
private ChangePasswordAdvisor changePasswordAdvisor;
6159

62-
private ChangePasswordAdviceHandler changePasswordAdviceHandler;
63-
6460
private UserDetailsPasswordManager userDetailsPasswordManager;
6561

6662
/**
@@ -87,12 +83,6 @@ public PasswordManagementConfigurer<B> changePasswordAdvisor(ChangePasswordAdvis
8783
return this;
8884
}
8985

90-
public PasswordManagementConfigurer<B> changePasswordAdviceHandler(
91-
ChangePasswordAdviceHandler changePasswordAdviceHandler) {
92-
this.changePasswordAdviceHandler = changePasswordAdviceHandler;
93-
return this;
94-
}
95-
9686
public PasswordManagementConfigurer<B> userDetailsPasswordManager(
9787
UserDetailsPasswordManager userDetailsPasswordManager) {
9888
this.userDetailsPasswordManager = userDetailsPasswordManager;
@@ -116,8 +106,8 @@ public void init(B http) throws Exception {
116106

117107
ChangePasswordAdvisor changePasswordAdvisor = (this.changePasswordAdvisor != null) ? this.changePasswordAdvisor
118108
: this.context.getBeanProvider(ChangePasswordAdvisor.class)
119-
.getIfUnique(() -> DelegatingChangePasswordAdvisor
120-
.of(new ChangePasswordServiceAdvisor(passwordManager), new ChangeCompromisedPasswordAdvisor()));
109+
.getIfUnique(() -> CompositeChangePasswordAdvisor
110+
.of(new UserDetailsChangePasswordAdvisor(), new ChangeCompromisedPasswordAdvisor()));
121111

122112
http.setSharedObject(ChangePasswordAdviceRepository.class, changePasswordAdviceRepository);
123113
http.setSharedObject(UserDetailsPasswordManager.class, passwordManager);

config/src/test/java/org/springframework/security/config/annotation/web/configurers/PasswordManagementConfigurerTests.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
import org.springframework.mock.web.MockHttpSession;
3232
import org.springframework.security.authentication.password.ChangePasswordAdvice;
3333
import org.springframework.security.authentication.password.ChangePasswordAdvisor;
34-
import org.springframework.security.authentication.password.ChangePasswordReasons;
35-
import org.springframework.security.authentication.password.SimpleChangePasswordAdvice;
3634
import org.springframework.security.authentication.password.UserDetailsPasswordManager;
3735
import org.springframework.security.config.Customizer;
3836
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -250,8 +248,7 @@ ResponseEntity<ChangePasswordAdvice> requireChangePassword(@PathVariable("userna
250248
if (user == null) {
251249
return ResponseEntity.notFound().build();
252250
}
253-
ChangePasswordAdvice advice = this.passwords.loadPasswordAdvice(user);
254-
return ResponseEntity.ok(advice);
251+
return ResponseEntity.ok(user.getChangePasswordAdvice());
255252
}
256253

257254
@PostMapping("/expire/{username}")
@@ -260,8 +257,7 @@ ResponseEntity<ChangePasswordAdvice> expirePassword(@PathVariable("username") St
260257
if (user == null) {
261258
return ResponseEntity.notFound().build();
262259
}
263-
ChangePasswordAdvice advice = new SimpleChangePasswordAdvice(ChangePasswordAdvice.Action.MUST_CHANGE,
264-
ChangePasswordReasons.EXPIRED);
260+
ChangePasswordAdvice advice = () -> ChangePasswordAdvice.Action.MUST_CHANGE;
265261
this.passwords.savePasswordAdvice(user, advice);
266262
URI uri = URI.create("/admin/passwords/advice/" + username);
267263
return ResponseEntity.created(uri).body(advice);
@@ -299,7 +295,6 @@ ResponseEntity<?> changePassword(@AuthenticationPrincipal UserDetails user,
299295
return ResponseEntity.badRequest().body(advice);
300296
}
301297
this.passwords.updatePassword(user, this.encoder.encode(password));
302-
this.passwords.removePasswordAdvice(user);
303298
this.changePasswordAdviceRepository.removePasswordAdvice(request, response);
304299
return ResponseEntity.ok().build();
305300
}

core/src/main/java/org/springframework/security/authentication/password/ChangeLengthPasswordAdvisor.java

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public ChangeLengthPasswordAdvisor(int minLength, int maxLength) {
4141
@Override
4242
public ChangePasswordAdvice advise(UserDetails user, String password) {
4343
if (password.length() < this.minLength) {
44-
return new SimpleChangePasswordAdvice(this.tooShortAction, ChangePasswordReasons.TOO_SHORT);
44+
return new TooShortAdvice(this.tooShortAction, this.minLength, password.length());
4545
}
4646
if (password.length() > this.maxLength) {
47-
return new SimpleChangePasswordAdvice(this.tooLongAction, ChangePasswordReasons.TOO_LONG);
47+
return new TooLongAdvice(this.tooLongAction, this.maxLength, password.length());
4848
}
49-
return ChangePasswordAdvice.abstain();
49+
return ChangePasswordAdvice.ABSTAIN;
5050
}
5151

5252
public void setTooShortAction(Action tooShortAction) {
@@ -57,4 +57,76 @@ public void setTooLongAction(Action tooLongAction) {
5757
this.tooLongAction = tooLongAction;
5858
}
5959

60+
public static final class TooShortAdvice implements ChangePasswordAdvice {
61+
private final Action action;
62+
63+
private final int minLength;
64+
65+
private final int actualLength;
66+
67+
private TooShortAdvice(Action action, int minLength, int actualLength) {
68+
this.action = action;
69+
this.minLength = minLength;
70+
this.actualLength = actualLength;
71+
}
72+
73+
@Override
74+
public Action getAction() {
75+
return this.action;
76+
}
77+
78+
public int getMinLength() {
79+
return this.minLength;
80+
}
81+
82+
public int getActualLength() {
83+
return this.actualLength;
84+
}
85+
86+
@Override
87+
public String toString() {
88+
return "TooShort [" +
89+
"action=" + this.action +
90+
", minLength=" + this.minLength +
91+
", actualLength=" + this.actualLength +
92+
"]";
93+
}
94+
95+
}
96+
97+
public static final class TooLongAdvice implements ChangePasswordAdvice {
98+
private final Action action;
99+
100+
private final int maxLength;
101+
102+
private final int actualLength;
103+
104+
private TooLongAdvice(Action action, int maxLength, int actualLength) {
105+
this.action = action;
106+
this.maxLength = maxLength;
107+
this.actualLength = actualLength;
108+
}
109+
110+
@Override
111+
public Action getAction() {
112+
return this.action;
113+
}
114+
115+
public int getMaxLength() {
116+
return this.maxLength;
117+
}
118+
119+
public int getActualLength() {
120+
return this.actualLength;
121+
}
122+
123+
@Override
124+
public String toString() {
125+
return "TooLong [" +
126+
"action=" + this.action +
127+
", maxLength=" + this.maxLength +
128+
", actualLength=" + this.actualLength +
129+
"]";
130+
}
131+
}
60132
}

core/src/main/java/org/springframework/security/authentication/password/ChangePasswordAdvice.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,11 @@
1616

1717
package org.springframework.security.authentication.password;
1818

19-
import java.util.Collection;
20-
2119
public interface ChangePasswordAdvice {
2220

23-
Action getAction();
24-
25-
Collection<String> getReasons();
21+
ChangePasswordAdvice ABSTAIN = () -> Action.ABSTAIN;
2622

27-
static ChangePasswordAdvice abstain() {
28-
return SimpleChangePasswordAdvice.ABSTAIN;
29-
}
23+
Action getAction();
3024

3125
enum Action {
3226

core/src/main/java/org/springframework/security/authentication/password/ChangePasswordReasons.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

core/src/main/java/org/springframework/security/authentication/password/ChangePasswordServiceAdvisor.java

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,63 +16,69 @@
1616

1717
package org.springframework.security.authentication.password;
1818

19-
import java.util.ArrayList;
2019
import java.util.Collection;
2120
import java.util.Collections;
2221
import java.util.List;
2322

2423
import org.springframework.security.core.userdetails.UserDetails;
2524

26-
public final class DelegatingChangePasswordAdvisor implements ChangePasswordAdvisor {
25+
public final class CompositeChangePasswordAdvisor implements ChangePasswordAdvisor {
2726

2827
private final List<ChangePasswordAdvisor> advisors;
2928

30-
private DelegatingChangePasswordAdvisor(List<ChangePasswordAdvisor> advisors) {
29+
private CompositeChangePasswordAdvisor(List<ChangePasswordAdvisor> advisors) {
3130
this.advisors = Collections.unmodifiableList(advisors);
3231
}
3332

3433
public static ChangePasswordAdvisor of(ChangePasswordAdvisor... advisors) {
35-
return new DelegatingChangePasswordAdvisor(List.of(advisors));
34+
return new CompositeChangePasswordAdvisor(List.of(advisors));
3635
}
3736

3837
@Override
3938
public ChangePasswordAdvice advise(UserDetails user, String password) {
4039
Collection<ChangePasswordAdvice> advice = this.advisors.stream()
4140
.map((advisor) -> advisor.advise(user, password))
42-
.filter((a) -> a.getAction() != ChangePasswordAdvice.Action.ABSTAIN)
4341
.toList();
44-
return new CompositeChangePasswordAdvice(advice);
42+
return new Advice(advice);
4543
}
4644

47-
private static final class CompositeChangePasswordAdvice implements ChangePasswordAdvice {
45+
public static final class Advice implements ChangePasswordAdvice {
4846

4947
private final Action action;
5048

51-
private final Collection<String> reasons;
49+
private final Collection<ChangePasswordAdvice> advice;
5250

53-
private CompositeChangePasswordAdvice(Collection<ChangePasswordAdvice> advice) {
51+
private Advice(Collection<ChangePasswordAdvice> advice) {
52+
this.action = findMostUrgentAction(advice);
53+
this.advice = advice;
54+
}
55+
56+
private Action findMostUrgentAction(Collection<ChangePasswordAdvice> advice) {
5457
Action mostUrgentAction = Action.ABSTAIN;
55-
Collection<String> reasons = new ArrayList<>();
5658
for (ChangePasswordAdvice a : advice) {
5759
if (mostUrgentAction.ordinal() < a.getAction().ordinal()) {
5860
mostUrgentAction = a.getAction();
5961
}
60-
reasons.addAll(a.getReasons());
6162
}
62-
this.action = mostUrgentAction;
63-
this.reasons = reasons;
63+
return mostUrgentAction;
6464
}
6565

6666
@Override
6767
public Action getAction() {
6868
return this.action;
6969
}
7070

71-
@Override
72-
public Collection<String> getReasons() {
73-
return this.reasons;
71+
public Collection<ChangePasswordAdvice> getAdvice() {
72+
return this.advice;
7473
}
7574

75+
@Override
76+
public String toString() {
77+
return "Composite [" +
78+
"action=" + this.action +
79+
", advice=" + this.advice +
80+
"]";
81+
}
7682
}
7783

7884
}

core/src/main/java/org/springframework/security/authentication/password/SimpleChangePasswordAdvice.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)