|
| 1 | +/* |
| 2 | + * Copyright 2002-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.config.annotation.web.configurers; |
| 18 | + |
| 19 | +import org.springframework.security.config.annotation.web.HttpSecurityBuilder; |
| 20 | +import org.springframework.security.web.RequestMatcherRedirectFilter; |
| 21 | +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
| 22 | +import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
| 23 | +import org.springframework.util.Assert; |
| 24 | + |
| 25 | +/** |
| 26 | + * Adds password management support. |
| 27 | + * |
| 28 | + * @author Evgeniy Cheban |
| 29 | + * @since 5.6 |
| 30 | + */ |
| 31 | +public final class PasswordManagementConfigurer<B extends HttpSecurityBuilder<B>> |
| 32 | + extends AbstractHttpConfigurer<PasswordManagementConfigurer<B>, B> { |
| 33 | + |
| 34 | + private static final String WELL_KNOWN_CHANGE_PASSWORD_PATTERN = "/.well-known/change-password"; |
| 35 | + |
| 36 | + private static final String DEFAULT_CHANGE_PASSWORD_PAGE = "/change-password"; |
| 37 | + |
| 38 | + private String changePasswordPage = DEFAULT_CHANGE_PASSWORD_PAGE; |
| 39 | + |
| 40 | + /** |
| 41 | + * Sets the change password page. Defaults to |
| 42 | + * {@link PasswordManagementConfigurer#DEFAULT_CHANGE_PASSWORD_PAGE}. |
| 43 | + * @param changePasswordPage the change password page |
| 44 | + * @return the {@link PasswordManagementConfigurer} for further customizations |
| 45 | + */ |
| 46 | + public PasswordManagementConfigurer<B> changePasswordPage(String changePasswordPage) { |
| 47 | + Assert.hasText(changePasswordPage, "changePasswordPage cannot be empty"); |
| 48 | + this.changePasswordPage = changePasswordPage; |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritDoc} |
| 54 | + */ |
| 55 | + @Override |
| 56 | + public void configure(B http) throws Exception { |
| 57 | + RequestMatcherRedirectFilter changePasswordFilter = new RequestMatcherRedirectFilter( |
| 58 | + new AntPathRequestMatcher(WELL_KNOWN_CHANGE_PASSWORD_PATTERN), this.changePasswordPage); |
| 59 | + http.addFilterBefore(postProcess(changePasswordFilter), UsernamePasswordAuthenticationFilter.class); |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments