|
153 | 153 | import org.springframework.security.web.server.header.StrictTransportSecurityServerHttpHeadersWriter;
|
154 | 154 | import org.springframework.security.web.server.header.XFrameOptionsServerHttpHeadersWriter;
|
155 | 155 | import org.springframework.security.web.server.header.XXssProtectionServerHttpHeadersWriter;
|
| 156 | +import org.springframework.security.web.server.ExchangeMatcherRedirectWebFilter; |
156 | 157 | import org.springframework.security.web.server.savedrequest.NoOpServerRequestCache;
|
157 | 158 | import org.springframework.security.web.server.savedrequest.ServerRequestCache;
|
158 | 159 | import org.springframework.security.web.server.savedrequest.ServerRequestCacheWebFilter;
|
@@ -258,6 +259,8 @@ public class ServerHttpSecurity {
|
258 | 259 |
|
259 | 260 | private HttpBasicSpec httpBasic;
|
260 | 261 |
|
| 262 | + private PasswordManagementSpec passwordManagement; |
| 263 | + |
261 | 264 | private X509Spec x509;
|
262 | 265 |
|
263 | 266 | private final RequestCacheSpec requestCache = new RequestCacheSpec();
|
@@ -734,6 +737,58 @@ public ServerHttpSecurity httpBasic(Customizer<HttpBasicSpec> httpBasicCustomize
|
734 | 737 | return this;
|
735 | 738 | }
|
736 | 739 |
|
| 740 | + /** |
| 741 | + * Configures password management. An example configuration is provided below: |
| 742 | + * |
| 743 | + * <pre class="code"> |
| 744 | + * @Bean |
| 745 | + * public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { |
| 746 | + * http |
| 747 | + * // ... |
| 748 | + * .passwordManagement(); |
| 749 | + * return http.build(); |
| 750 | + * } |
| 751 | + * </pre> |
| 752 | + * |
| 753 | + * @return the {@link PasswordManagementSpec} to customize |
| 754 | + * @since 5.4 |
| 755 | + */ |
| 756 | + public PasswordManagementSpec passwordManagement() { |
| 757 | + if (this.passwordManagement == null) { |
| 758 | + this.passwordManagement = new PasswordManagementSpec(); |
| 759 | + } |
| 760 | + return this.passwordManagement; |
| 761 | + } |
| 762 | + |
| 763 | + /** |
| 764 | + * Configures password management. An example configuration is provided below: |
| 765 | + * |
| 766 | + * <pre class="code"> |
| 767 | + * @Bean |
| 768 | + * public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { |
| 769 | + * http |
| 770 | + * // ... |
| 771 | + * .passwordManagement(passwordManagement -> |
| 772 | + * // Custom change password page. |
| 773 | + * passwordManagement.changePasswordPage("/custom-change-password-page") |
| 774 | + * ); |
| 775 | + * return http.build(); |
| 776 | + * } |
| 777 | + * </pre> |
| 778 | + * |
| 779 | + * @param passwordManagementCustomizer the {@link Customizer} to provide more options for |
| 780 | + * the {@link PasswordManagementSpec} |
| 781 | + * @return the {@link ServerHttpSecurity} to customize |
| 782 | + * @since 5.4 |
| 783 | + */ |
| 784 | + public ServerHttpSecurity passwordManagement(Customizer<PasswordManagementSpec> passwordManagementCustomizer) { |
| 785 | + if (this.passwordManagement == null) { |
| 786 | + this.passwordManagement = new PasswordManagementSpec(); |
| 787 | + } |
| 788 | + passwordManagementCustomizer.customize(this.passwordManagement); |
| 789 | + return this; |
| 790 | + } |
| 791 | + |
737 | 792 | /**
|
738 | 793 | * Configures form based authentication. An example configuration is provided below:
|
739 | 794 | *
|
@@ -2448,6 +2503,9 @@ else if (this.securityContextRepository != null) {
|
2448 | 2503 | }
|
2449 | 2504 | this.httpBasic.configure(this);
|
2450 | 2505 | }
|
| 2506 | + if (this.passwordManagement != null) { |
| 2507 | + this.passwordManagement.configure(this); |
| 2508 | + } |
2451 | 2509 | if (this.formLogin != null) {
|
2452 | 2510 | if (this.formLogin.authenticationManager == null) {
|
2453 | 2511 | this.formLogin.authenticationManager(this.authenticationManager);
|
@@ -3054,6 +3112,51 @@ protected void configure(ServerHttpSecurity http) {
|
3054 | 3112 | private HttpBasicSpec() {}
|
3055 | 3113 | }
|
3056 | 3114 |
|
| 3115 | + /** |
| 3116 | + * Configures password management. |
| 3117 | + * |
| 3118 | + * @author Evgeniy Cheban |
| 3119 | + * @see #passwordManagement() |
| 3120 | + * @since 5.4 |
| 3121 | + */ |
| 3122 | + public class PasswordManagementSpec { |
| 3123 | + private static final String WELL_KNOWN_CHANGE_PASSWORD_PATTERN = "/.well-known/change-password"; |
| 3124 | + private static final String DEFAULT_CHANGE_PASSWORD_PAGE = "/change-password"; |
| 3125 | + |
| 3126 | + private String changePasswordPage = DEFAULT_CHANGE_PASSWORD_PAGE; |
| 3127 | + |
| 3128 | + /** |
| 3129 | + * Sets the change password page. |
| 3130 | + * Defaults to {@link PasswordManagementSpec#DEFAULT_CHANGE_PASSWORD_PAGE}. |
| 3131 | + * |
| 3132 | + * @param changePasswordPage the change password page |
| 3133 | + * @return the {@link PasswordManagementSpec} to continue configuring |
| 3134 | + */ |
| 3135 | + public PasswordManagementSpec changePasswordPage(String changePasswordPage) { |
| 3136 | + Assert.hasText(changePasswordPage, "changePasswordPage cannot be empty"); |
| 3137 | + this.changePasswordPage = changePasswordPage; |
| 3138 | + return this; |
| 3139 | + } |
| 3140 | + |
| 3141 | + /** |
| 3142 | + * Allows method chaining to continue configuring the {@link ServerHttpSecurity}. |
| 3143 | + * |
| 3144 | + * @return the {@link ServerHttpSecurity} to continue configuring |
| 3145 | + */ |
| 3146 | + public ServerHttpSecurity and() { |
| 3147 | + return ServerHttpSecurity.this; |
| 3148 | + } |
| 3149 | + |
| 3150 | + protected void configure(ServerHttpSecurity http) { |
| 3151 | + ExchangeMatcherRedirectWebFilter changePasswordWebFilter = new ExchangeMatcherRedirectWebFilter( |
| 3152 | + new PathPatternParserServerWebExchangeMatcher(WELL_KNOWN_CHANGE_PASSWORD_PATTERN), this.changePasswordPage); |
| 3153 | + http.addFilterBefore(changePasswordWebFilter, SecurityWebFiltersOrder.AUTHENTICATION); |
| 3154 | + } |
| 3155 | + |
| 3156 | + private PasswordManagementSpec() { |
| 3157 | + } |
| 3158 | + } |
| 3159 | + |
3057 | 3160 | /**
|
3058 | 3161 | * Configures Form Based authentication
|
3059 | 3162 | *
|
|
0 commit comments