Skip to content

Commit a04035d

Browse files
committed
Removed Handler
- This allow the request matcher to be in the filter, preventing eager access to the session
1 parent 23a1541 commit a04035d

File tree

4 files changed

+32
-133
lines changed

4 files changed

+32
-133
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
import org.springframework.security.web.authentication.password.ChangePasswordAdviceSessionAuthenticationStrategy;
3232
import org.springframework.security.web.authentication.password.ChangePasswordAdvisingFilter;
3333
import org.springframework.security.web.authentication.password.HttpSessionChangePasswordAdviceRepository;
34-
import org.springframework.security.web.authentication.password.SimpleChangePasswordAdviceHandler;
34+
import org.springframework.security.web.authentication.password.RedirectingChangePasswordAdviceHandler;
35+
import org.springframework.security.web.savedrequest.RequestCache;
3536
import org.springframework.security.web.savedrequest.RequestCacheAwareFilter;
3637
import org.springframework.util.Assert;
3738

@@ -147,13 +148,9 @@ public void configure(B http) throws Exception {
147148
return;
148149
}
149150

150-
ChangePasswordAdviceHandler changePasswordAdviceHandler = (this.changePasswordAdviceHandler != null)
151-
? this.changePasswordAdviceHandler : this.context.getBeanProvider(ChangePasswordAdviceHandler.class)
152-
.getIfUnique(() -> new SimpleChangePasswordAdviceHandler(this.changePasswordPage));
153-
154-
ChangePasswordAdvisingFilter advising = new ChangePasswordAdvisingFilter();
151+
ChangePasswordAdvisingFilter advising = new ChangePasswordAdvisingFilter(this.changePasswordPage);
155152
advising.setChangePasswordAdviceRepository(http.getSharedObject(ChangePasswordAdviceRepository.class));
156-
advising.setChangePasswordAdviceHandler(changePasswordAdviceHandler);
153+
advising.setRequestCache(http.getSharedObject(RequestCache.class));
157154
http.addFilterBefore(advising, RequestCacheAwareFilter.class);
158155
}
159156

web/src/main/java/org/springframework/security/web/authentication/password/ChangePasswordAdviceHandler.java

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

web/src/main/java/org/springframework/security/web/authentication/password/ChangePasswordAdvisingFilter.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
import jakarta.servlet.http.HttpServletResponse;
2525

2626
import org.springframework.security.authentication.password.ChangePasswordAdvice;
27+
import org.springframework.security.web.DefaultRedirectStrategy;
28+
import org.springframework.security.web.RedirectStrategy;
29+
import org.springframework.security.web.savedrequest.NullRequestCache;
30+
import org.springframework.security.web.savedrequest.RequestCache;
2731
import org.springframework.security.web.util.matcher.NegatedRequestMatcher;
2832
import org.springframework.security.web.util.matcher.RequestMatcher;
2933
import org.springframework.web.filter.OncePerRequestFilter;
@@ -32,34 +36,47 @@
3236

3337
public class ChangePasswordAdvisingFilter extends OncePerRequestFilter {
3438

35-
private RequestMatcher shouldHandleAdvice = new NegatedRequestMatcher(pathPattern("/change-password"));
39+
private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
3640

37-
private ChangePasswordAdviceHandler changePasswordAdviceHandler = new SimpleChangePasswordAdviceHandler(
38-
"/.well-known/change-password");
41+
private final String changePasswordUrl;
42+
43+
private RequestCache requestCache = new NullRequestCache();
3944

4045
private ChangePasswordAdviceRepository changePasswordAdviceRepository = new HttpSessionChangePasswordAdviceRepository();
4146

47+
private RequestMatcher requestMatcher;
48+
49+
public ChangePasswordAdvisingFilter(String changePasswordUrl) {
50+
this.changePasswordUrl = changePasswordUrl;
51+
this.requestMatcher = new NegatedRequestMatcher(pathPattern(changePasswordUrl));
52+
}
53+
4254
@Override
4355
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
4456
throws ServletException, IOException {
45-
if (!this.shouldHandleAdvice.matches(request)) {
57+
if (!this.requestMatcher.matches(request)) {
4658
chain.doFilter(request, response);
4759
return;
4860
}
4961
ChangePasswordAdvice advice = this.changePasswordAdviceRepository.loadPasswordAdvice(request);
50-
this.changePasswordAdviceHandler.handle(request, response, chain, advice);
51-
}
52-
53-
public void setShouldHandleAdviceRequestMatcher(RequestMatcher shouldHandleAdvice) {
54-
this.shouldHandleAdvice = shouldHandleAdvice;
62+
if (advice.getAction() != ChangePasswordAdvice.Action.MUST_CHANGE) {
63+
chain.doFilter(request, response);
64+
return;
65+
}
66+
this.requestCache.saveRequest(request, response);
67+
this.redirectStrategy.sendRedirect(request, response, this.changePasswordUrl);
5568
}
5669

5770
public void setChangePasswordAdviceRepository(ChangePasswordAdviceRepository changePasswordAdviceRepository) {
5871
this.changePasswordAdviceRepository = changePasswordAdviceRepository;
5972
}
6073

61-
public void setChangePasswordAdviceHandler(ChangePasswordAdviceHandler changePasswordAdviceHandler) {
62-
this.changePasswordAdviceHandler = changePasswordAdviceHandler;
74+
public void setRequestCache(RequestCache requestCache) {
75+
this.requestCache = requestCache;
76+
}
77+
78+
public void setRequestMatcher(RequestMatcher requestMatcher) {
79+
this.requestMatcher = requestMatcher;
6380
}
6481

6582
}

web/src/main/java/org/springframework/security/web/authentication/password/SimpleChangePasswordAdviceHandler.java

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

0 commit comments

Comments
 (0)