http.adoc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. [[servlet-http]]
  2. = HTTP
  3. All HTTP-based communication should be protected xref:features/exploits/http.adoc#http[using TLS].
  4. This section discusses the details of servlet-specific features that assist with HTTPS usage.
  5. [[servlet-http-redirect]]
  6. == Redirect to HTTPS
  7. If a client makes a request using HTTP rather than HTTPS, you can configure Spring Security to redirect to HTTPS.
  8. For example, the following Java or Kotlin configuration redirects any HTTP requests to HTTPS:
  9. .Redirect to HTTPS
  10. [tabs]
  11. ======
  12. Java::
  13. +
  14. [source,java,role="primary"]
  15. ----
  16. @Configuration
  17. @EnableWebSecurity
  18. public class WebSecurityConfig {
  19. @Bean
  20. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  21. http
  22. // ...
  23. .redirectToHttps(withDefaults());
  24. return http.build();
  25. }
  26. }
  27. ----
  28. Kotlin::
  29. +
  30. [source,kotlin,role="secondary"]
  31. ----
  32. @Configuration
  33. @EnableWebSecurity
  34. class SecurityConfig {
  35. @Bean
  36. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  37. http {
  38. // ...
  39. redirectToHttps { }
  40. }
  41. return http.build()
  42. }
  43. }
  44. ----
  45. ======
  46. The following XML configuration redirects all HTTP requests to HTTPS
  47. .Redirect to HTTPS with XML Configuration
  48. [source,xml]
  49. ----
  50. <http>
  51. <intercept-url pattern="/**" access="ROLE_USER" requires-channel="https"/>
  52. ...
  53. </http>
  54. ----
  55. [[servlet-hsts]]
  56. == Strict Transport Security
  57. Spring Security provides support for xref:servlet/exploits/headers.adoc#servlet-headers-hsts[Strict Transport Security] and enables it by default.
  58. [[servlet-http-proxy-server]]
  59. == Proxy Server Configuration
  60. Spring Security xref:features/exploits/http.adoc#http-proxy-server[integrates with proxy servers].