http.adoc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. ====
  11. .Java
  12. [source,java,role="primary"]
  13. ----
  14. @Bean
  15. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  16. http
  17. // ...
  18. .redirectToHttps(withDefaults());
  19. return http.build();
  20. }
  21. ----
  22. .Kotlin
  23. [source,kotlin,role="secondary"]
  24. ----
  25. @Bean
  26. fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  27. return http {
  28. // ...
  29. redirectToHttps { }
  30. }
  31. }
  32. ----
  33. ====
  34. The configuration can easily be wrapped around an if statement to only be turned on in production.
  35. Alternatively, it can be enabled by looking for a property about the request that only happens in production.
  36. For example, if the production environment adds a header named `X-Forwarded-Proto` the following Java Configuration could be used:
  37. .Redirect to HTTPS when X-Forwarded
  38. ====
  39. .Java
  40. [source,java,role="primary"]
  41. ----
  42. @Bean
  43. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  44. http
  45. // ...
  46. .redirectToHttps(redirect -> redirect
  47. .httpsRedirectWhen(e -> e.getRequest().getHeaders().containsKey("X-Forwarded-Proto"))
  48. );
  49. return http.build();
  50. }
  51. ----
  52. .Kotlin
  53. [source,kotlin,role="secondary"]
  54. ----
  55. @Bean
  56. fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  57. return http {
  58. // ...
  59. redirectToHttps {
  60. httpsRedirectWhen {
  61. it.request.headers.containsKey("X-Forwarded-Proto")
  62. }
  63. }
  64. }
  65. }
  66. ----
  67. ====
  68. [[webflux-hsts]]
  69. == Strict Transport Security
  70. Spring Security provides support for xref:servlet/exploits/headers.adoc#servlet-headers-hsts[Strict Transport Security] and enables it by default.
  71. [[webflux-http-proxy-server]]
  72. == Proxy Server Configuration
  73. Spring Security xref:features/exploits/http.adoc#http-proxy-server[integrates with proxy servers].