|
24 | 24 | import jakarta.servlet.http.HttpServletResponse;
|
25 | 25 |
|
26 | 26 | 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; |
27 | 31 | import org.springframework.security.web.util.matcher.NegatedRequestMatcher;
|
28 | 32 | import org.springframework.security.web.util.matcher.RequestMatcher;
|
29 | 33 | import org.springframework.web.filter.OncePerRequestFilter;
|
|
32 | 36 |
|
33 | 37 | public class ChangePasswordAdvisingFilter extends OncePerRequestFilter {
|
34 | 38 |
|
35 |
| - private RequestMatcher shouldHandleAdvice = new NegatedRequestMatcher(pathPattern("/change-password")); |
| 39 | + private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); |
36 | 40 |
|
37 |
| - private ChangePasswordAdviceHandler changePasswordAdviceHandler = new SimpleChangePasswordAdviceHandler( |
38 |
| - "/.well-known/change-password"); |
| 41 | + private final String changePasswordUrl; |
| 42 | + |
| 43 | + private RequestCache requestCache = new NullRequestCache(); |
39 | 44 |
|
40 | 45 | private ChangePasswordAdviceRepository changePasswordAdviceRepository = new HttpSessionChangePasswordAdviceRepository();
|
41 | 46 |
|
| 47 | + private RequestMatcher requestMatcher; |
| 48 | + |
| 49 | + public ChangePasswordAdvisingFilter(String changePasswordUrl) { |
| 50 | + this.changePasswordUrl = changePasswordUrl; |
| 51 | + this.requestMatcher = new NegatedRequestMatcher(pathPattern(changePasswordUrl)); |
| 52 | + } |
| 53 | + |
42 | 54 | @Override
|
43 | 55 | protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
44 | 56 | throws ServletException, IOException {
|
45 |
| - if (!this.shouldHandleAdvice.matches(request)) { |
| 57 | + if (!this.requestMatcher.matches(request)) { |
46 | 58 | chain.doFilter(request, response);
|
47 | 59 | return;
|
48 | 60 | }
|
49 | 61 | 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); |
55 | 68 | }
|
56 | 69 |
|
57 | 70 | public void setChangePasswordAdviceRepository(ChangePasswordAdviceRepository changePasswordAdviceRepository) {
|
58 | 71 | this.changePasswordAdviceRepository = changePasswordAdviceRepository;
|
59 | 72 | }
|
60 | 73 |
|
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; |
63 | 80 | }
|
64 | 81 |
|
65 | 82 | }
|
0 commit comments