servlet-exploits-http.md 1.6 KB
Newer Older
茶陵後's avatar
茶陵後 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
# HTTP

All HTTP based communication should be protected [using TLS](../../features/exploits/http.html#http).

Below you can find details around Servlet specific features that assist with HTTPS usage.

## 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:

Example 1. Redirect to HTTPS

Java

```
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends
		WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) {
		http
			// ...
			.requiresChannel(channel -> channel
				.anyRequest().requiresSecure()
			);
	}
}
```

Kotlin

```
@Configuration
@EnableWebSecurity
class SecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
        http {
            // ...
            requiresChannel {
                secure(AnyRequestMatcher.INSTANCE, "REQUIRES_SECURE_CHANNEL")
            }
        }
    }
}
```

The following XML configuration will redirect all HTTP requests to HTTPS

Example 2. Redirect to HTTPS with XML Configuration

```
<http>
	<intercept-url pattern="/**" access="ROLE_USER" requires-channel="https"/>
...
</http>
```

## Strict Transport Security

Spring Security provides support for [Strict Transport Security](headers.html#servlet-headers-hsts) and enables it by default.

## Proxy Server Configuration

Spring Security [integrates with proxy servers](../../features/exploits/http.html#http-proxy-server).

[Security HTTP Response Headers](headers.html)[HttpFirewall](firewall.html)