http.adoc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. [[webflux-http]]
  2. = HTTP
  3. All HTTP based communication should be protected xref:features/exploits/http.adoc#http[using TLS].
  4. Below you can find details around WebFlux specific features that assist with HTTPS usage.
  5. [[webflux-http-redirect]]
  6. == Redirect to HTTPS
  7. If a client makes a request using HTTP rather than HTTPS, Spring Security can be configured to redirect to HTTPS.
  8. For example, the following Java configuration will redirect any HTTP requests to HTTPS:
  9. .Redirect to HTTPS
  10. [tabs]
  11. ======
  12. Java::
  13. +
  14. [source,java,role="primary"]
  15. ----
  16. @Bean
  17. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  18. http
  19. // ...
  20. .redirectToHttps(withDefaults());
  21. return http.build();
  22. }
  23. ----
  24. Kotlin::
  25. +
  26. [source,kotlin,role="secondary"]
  27. ----
  28. @Bean
  29. fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  30. return http {
  31. // ...
  32. redirectToHttps { }
  33. }
  34. }
  35. ----
  36. ======
  37. The configuration can easily be wrapped around an if statement to only be turned on in production.
  38. Alternatively, it can be enabled by looking for a property about the request that only happens in production.
  39. For example, if the production environment adds a header named `X-Forwarded-Proto` the following Java Configuration could be used:
  40. .Redirect to HTTPS when X-Forwarded
  41. [tabs]
  42. ======
  43. Java::
  44. +
  45. [source,java,role="primary"]
  46. ----
  47. @Bean
  48. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  49. http
  50. // ...
  51. .redirectToHttps(redirect -> redirect
  52. .httpsRedirectWhen(e -> e.getRequest().getHeaders().containsKey("X-Forwarded-Proto"))
  53. );
  54. return http.build();
  55. }
  56. ----
  57. Kotlin::
  58. +
  59. [source,kotlin,role="secondary"]
  60. ----
  61. @Bean
  62. fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  63. return http {
  64. // ...
  65. redirectToHttps {
  66. httpsRedirectWhen {
  67. it.request.headers.containsKey("X-Forwarded-Proto")
  68. }
  69. }
  70. }
  71. }
  72. ----
  73. ======
  74. [[webflux-hsts]]
  75. == Strict Transport Security
  76. Spring Security provides support for xref:servlet/exploits/headers.adoc#servlet-headers-hsts[Strict Transport Security] and enables it by default.
  77. [[webflux-http-proxy-server]]
  78. == Proxy Server Configuration
  79. Spring Security xref:features/exploits/http.adoc#http-proxy-server[integrates with proxy servers].