configuration.adoc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. = Configuration Migrations
  2. The following steps relate to changes around how to configure `HttpSecurity`, `WebSecurity` and related components.
  3. == Use the Lambda DSL
  4. The Lambda DSL is present in Spring Security since version 5.2, and it allows HTTP security to be configured using lambdas.
  5. You may have seen this style of configuration in the Spring Security documentation or samples.
  6. Let us take a look at how a lambda configuration of HTTP security compares to the previous configuration style.
  7. ====
  8. [source,java]
  9. .Configuration using lambdas
  10. ----
  11. @Configuration
  12. @EnableWebSecurity
  13. public class SecurityConfig {
  14. @Bean
  15. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  16. http
  17. .authorizeHttpRequests(authorize -> authorize
  18. .requestMatchers("/blog/**").permitAll()
  19. .anyRequest().authenticated()
  20. )
  21. .formLogin(formLogin -> formLogin
  22. .loginPage("/login")
  23. .permitAll()
  24. )
  25. .rememberMe(Customizer.withDefaults());
  26. return http.build();
  27. }
  28. }
  29. ----
  30. ====
  31. ====
  32. [source,java]
  33. .Equivalent configuration without using lambdas
  34. ----
  35. @Configuration
  36. @EnableWebSecurity
  37. public class SecurityConfig {
  38. @Bean
  39. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  40. http
  41. .authorizeHttpRequests()
  42. .requestMatchers("/blog/**").permitAll()
  43. .anyRequest().authenticated()
  44. .and()
  45. .formLogin()
  46. .loginPage("/login")
  47. .permitAll()
  48. .and()
  49. .rememberMe();
  50. return http.build();
  51. }
  52. }
  53. ----
  54. ====
  55. The Lambda DSL is the preferred way to configure Spring Security, the prior configuration style will not be valid in Spring Security 7 where the usage of the Lambda DSL will be required.
  56. This has been done mainly for a couple of reasons:
  57. - The previous way it was not clear what object was getting configured without knowing what the return type was.
  58. The deeper the nesting the more confusing it became.
  59. Even experienced users would think that their configuration was doing one thing when in fact, it was doing something else.
  60. - Consistency.
  61. Many code bases switched between the two styles which caused inconsistencies that made understanding the configuration difficult and often led to misconfigurations.
  62. === Lambda DSL Configuration Tips
  63. When comparing the two samples above, you will notice some key differences:
  64. - In the Lambda DSL there is no need to chain configuration options using the `.and()` method.
  65. The `HttpSecurity` instance is automatically returned for further configuration after the call to the lambda method.
  66. - `Customizer.withDefaults()` enables a security feature using the defaults provided by Spring Security.
  67. This is a shortcut for the lambda expression `it -> {}`.
  68. === WebFlux Security
  69. You may also configure WebFlux security using lambdas in a similar manner.
  70. Below is an example configuration using lambdas.
  71. ====
  72. [source,java]
  73. .WebFlux configuration using lambdas
  74. ----
  75. @Configuration
  76. @EnableWebFluxSecurity
  77. public class SecurityConfig {
  78. @Bean
  79. public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  80. http
  81. .authorizeExchange(exchanges -> exchanges
  82. .pathMatchers("/blog/**").permitAll()
  83. .anyExchange().authenticated()
  84. )
  85. .httpBasic(Customizer.withDefaults())
  86. .formLogin(formLogin -> formLogin
  87. .loginPage("/login")
  88. );
  89. return http.build();
  90. }
  91. }
  92. ----
  93. ====
  94. === Goals of the Lambda DSL
  95. The Lambda DSL was created to accomplish to following goals:
  96. - Automatic indentation makes the configuration more readable.
  97. - The is no need to chain configuration options using `.and()`
  98. - The Spring Security DSL has a similar configuration style to other Spring DSLs such as Spring Integration and Spring Cloud Gateway.