http.adoc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. .requiresChannel(channel -> channel
  24. .anyRequest().requiresSecure()
  25. );
  26. return http.build();
  27. }
  28. }
  29. ----
  30. Kotlin::
  31. +
  32. [source,kotlin,role="secondary"]
  33. ----
  34. @Configuration
  35. @EnableWebSecurity
  36. class SecurityConfig {
  37. @Bean
  38. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  39. http {
  40. // ...
  41. requiresChannel {
  42. secure(AnyRequestMatcher.INSTANCE, "REQUIRES_SECURE_CHANNEL")
  43. }
  44. }
  45. return http.build()
  46. }
  47. }
  48. ----
  49. ======
  50. The following XML configuration redirects all HTTP requests to HTTPS
  51. .Redirect to HTTPS with XML Configuration
  52. [source,xml]
  53. ----
  54. <http>
  55. <intercept-url pattern="/**" access="ROLE_USER" requires-channel="https"/>
  56. ...
  57. </http>
  58. ----
  59. [[servlet-hsts]]
  60. == Strict Transport Security
  61. Spring Security provides support for xref:servlet/exploits/headers.adoc#servlet-headers-hsts[Strict Transport Security] and enables it by default.
  62. [[servlet-http-proxy-server]]
  63. == Proxy Server Configuration
  64. Spring Security xref:features/exploits/http.adoc#http-proxy-server[integrates with proxy servers].