| 
									
										
										
										
											2019-11-25 15:49:51 -06:00
										 |  |  | [[servlet-http]] | 
					
						
							|  |  |  | = HTTP | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 15:21:42 -05:00
										 |  |  | All HTTP based communication should be protected xref:features/exploits/http.adoc#http[using TLS]. | 
					
						
							| 
									
										
										
										
											2019-11-25 15:49:51 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | Below you can find details around Servlet specific features that assist with HTTPS usage. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [[servlet-http-redirect]] | 
					
						
							|  |  |  | == Redirect to HTTPS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If a client makes a request using HTTP rather than HTTPS, Spring Security can be configured to redirect to HTTPS. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | For example, the following Java configuration will redirect any HTTP requests to HTTPS: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-18 14:44:33 +02:00
										 |  |  | .Redirect to HTTPS | 
					
						
							| 
									
										
										
										
											2019-11-25 15:49:51 -06:00
										 |  |  | ==== | 
					
						
							| 
									
										
										
										
											2020-09-18 14:44:33 +02:00
										 |  |  | .Java | 
					
						
							|  |  |  | [source,java,role="primary"] | 
					
						
							| 
									
										
										
										
											2019-11-25 15:49:51 -06:00
										 |  |  | ---- | 
					
						
							|  |  |  | @Configuration | 
					
						
							|  |  |  | @EnableWebSecurity | 
					
						
							| 
									
										
										
										
											2022-02-08 16:12:10 +01:00
										 |  |  | public class WebSecurityConfig { | 
					
						
							| 
									
										
										
										
											2019-11-25 15:49:51 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-08 16:12:10 +01:00
										 |  |  | 	@Bean | 
					
						
							|  |  |  | 	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | 
					
						
							| 
									
										
										
										
											2019-11-25 15:49:51 -06:00
										 |  |  | 		http | 
					
						
							|  |  |  | 			// ... | 
					
						
							| 
									
										
										
										
											2020-01-10 13:10:36 +01:00
										 |  |  | 			.requiresChannel(channel -> channel | 
					
						
							|  |  |  | 				.anyRequest().requiresSecure() | 
					
						
							| 
									
										
										
										
											2019-11-25 15:49:51 -06:00
										 |  |  | 			); | 
					
						
							| 
									
										
										
										
											2022-02-08 16:12:10 +01:00
										 |  |  | 		return http.build(); | 
					
						
							| 
									
										
										
										
											2019-11-25 15:49:51 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ---- | 
					
						
							| 
									
										
										
										
											2020-09-18 14:44:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | .Kotlin | 
					
						
							|  |  |  | [source,kotlin,role="secondary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | @Configuration | 
					
						
							|  |  |  | @EnableWebSecurity | 
					
						
							| 
									
										
										
										
											2022-02-08 16:12:10 +01:00
										 |  |  | class SecurityConfig { | 
					
						
							| 
									
										
										
										
											2020-09-18 14:44:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-08 16:12:10 +01:00
										 |  |  |     @Bean | 
					
						
							|  |  |  |     open fun filterChain(http: HttpSecurity): SecurityFilterChain { | 
					
						
							| 
									
										
										
										
											2020-09-18 14:44:33 +02:00
										 |  |  |         http { | 
					
						
							|  |  |  |             // ... | 
					
						
							|  |  |  |             requiresChannel { | 
					
						
							|  |  |  |                 secure(AnyRequestMatcher.INSTANCE, "REQUIRES_SECURE_CHANNEL") | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-02-08 16:12:10 +01:00
										 |  |  |         return http.build() | 
					
						
							| 
									
										
										
										
											2020-09-18 14:44:33 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ---- | 
					
						
							| 
									
										
										
										
											2019-11-25 15:49:51 -06:00
										 |  |  | ==== | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The following XML configuration will redirect all HTTP requests to HTTPS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .Redirect to HTTPS with XML Configuration | 
					
						
							|  |  |  | ==== | 
					
						
							|  |  |  | [source,xml] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | <http> | 
					
						
							|  |  |  | 	<intercept-url pattern="/**" access="ROLE_USER" requires-channel="https"/> | 
					
						
							|  |  |  | ... | 
					
						
							|  |  |  | </http> | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | ==== | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [[servlet-hsts]] | 
					
						
							|  |  |  | == Strict Transport Security | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-30 16:56:54 -05:00
										 |  |  | Spring Security provides support for xref:servlet/exploits/headers.adoc#servlet-headers-hsts[Strict Transport Security] and enables it by default. | 
					
						
							| 
									
										
										
										
											2019-11-25 15:49:51 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | [[servlet-http-proxy-server]] | 
					
						
							|  |  |  | == Proxy Server Configuration | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 15:21:42 -05:00
										 |  |  | Spring Security xref:features/exploits/http.adoc#http-proxy-server[integrates with proxy servers]. |