|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 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.PortMapper; |
| 21 | +import org.springframework.security.web.transport.HttpsRedirectFilter; |
| 22 | +import org.springframework.security.web.util.matcher.OrRequestMatcher; |
| 23 | +import org.springframework.security.web.util.matcher.RequestMatcher; |
| 24 | + |
| 25 | +/** |
| 26 | + * Specifies for what requests the application should redirect to HTTPS. When this |
| 27 | + * configurer is added, it redirects all HTTP requests by default to HTTPS. |
| 28 | + * |
| 29 | + * <h2>Security Filters</h2> |
| 30 | + * |
| 31 | + * The following Filters are populated |
| 32 | + * |
| 33 | + * <ul> |
| 34 | + * <li>{@link HttpsRedirectFilter}</li> |
| 35 | + * </ul> |
| 36 | + * |
| 37 | + * <h2>Shared Objects Created</h2> |
| 38 | + * |
| 39 | + * No shared objects are created. |
| 40 | + * |
| 41 | + * <h2>Shared Objects Used</h2> |
| 42 | + * |
| 43 | + * The following shared objects are used: |
| 44 | + * |
| 45 | + * <ul> |
| 46 | + * <li>{@link PortMapper} is used to configure {@link HttpsRedirectFilter}</li> |
| 47 | + * </ul> |
| 48 | + * |
| 49 | + * @param <H> the type of {@link HttpSecurityBuilder} that is being configured |
| 50 | + * @author Josh Cummings |
| 51 | + * @since 6.5 |
| 52 | + */ |
| 53 | +public final class HttpsRedirectConfigurer<H extends HttpSecurityBuilder<H>> |
| 54 | + extends AbstractHttpConfigurer<HeadersConfigurer<H>, H> { |
| 55 | + |
| 56 | + private RequestMatcher requestMatcher; |
| 57 | + |
| 58 | + public HttpsRedirectConfigurer<H> requestMatchers(RequestMatcher... matchers) { |
| 59 | + this.requestMatcher = new OrRequestMatcher(matchers); |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void configure(H http) throws Exception { |
| 65 | + HttpsRedirectFilter filter = new HttpsRedirectFilter(); |
| 66 | + if (this.requestMatcher != null) { |
| 67 | + filter.setRequestMatcher(this.requestMatcher); |
| 68 | + } |
| 69 | + PortMapper mapper = http.getSharedObject(PortMapper.class); |
| 70 | + if (mapper != null) { |
| 71 | + filter.setPortMapper(mapper); |
| 72 | + } |
| 73 | + http.addFilter(filter); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments