configuration.adoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. [source,java]
  8. .Configuration using lambdas
  9. ----
  10. @Configuration
  11. @EnableWebSecurity
  12. public class SecurityConfig {
  13. @Bean
  14. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  15. http
  16. .authorizeHttpRequests(authorize -> authorize
  17. .requestMatchers("/blog/**").permitAll()
  18. .anyRequest().authenticated()
  19. )
  20. .formLogin(formLogin -> formLogin
  21. .loginPage("/login")
  22. .permitAll()
  23. )
  24. .rememberMe(Customizer.withDefaults());
  25. return http.build();
  26. }
  27. }
  28. ----
  29. [source,java]
  30. .Equivalent configuration without using lambdas
  31. ----
  32. @Configuration
  33. @EnableWebSecurity
  34. public class SecurityConfig {
  35. @Bean
  36. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  37. http
  38. .authorizeHttpRequests()
  39. .requestMatchers("/blog/**").permitAll()
  40. .anyRequest().authenticated()
  41. .and()
  42. .formLogin()
  43. .loginPage("/login")
  44. .permitAll()
  45. .and()
  46. .rememberMe();
  47. return http.build();
  48. }
  49. }
  50. ----
  51. 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.
  52. This has been done mainly for a couple of reasons:
  53. - The previous way it was not clear what object was getting configured without knowing what the return type was.
  54. The deeper the nesting the more confusing it became.
  55. Even experienced users would think that their configuration was doing one thing when in fact, it was doing something else.
  56. - Consistency.
  57. Many code bases switched between the two styles which caused inconsistencies that made understanding the configuration difficult and often led to misconfigurations.
  58. === Lambda DSL Configuration Tips
  59. When comparing the two samples above, you will notice some key differences:
  60. - In the Lambda DSL there is no need to chain configuration options using the `.and()` method.
  61. The `HttpSecurity` instance is automatically returned for further configuration after the call to the lambda method.
  62. - `Customizer.withDefaults()` enables a security feature using the defaults provided by Spring Security.
  63. This is a shortcut for the lambda expression `it -> {}`.
  64. === WebFlux Security
  65. You may also configure WebFlux security using lambdas in a similar manner.
  66. Below is an example configuration using lambdas.
  67. [source,java]
  68. .WebFlux configuration using lambdas
  69. ----
  70. @Configuration
  71. @EnableWebFluxSecurity
  72. public class SecurityConfig {
  73. @Bean
  74. public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  75. http
  76. .authorizeExchange(exchanges -> exchanges
  77. .pathMatchers("/blog/**").permitAll()
  78. .anyExchange().authenticated()
  79. )
  80. .httpBasic(Customizer.withDefaults())
  81. .formLogin(formLogin -> formLogin
  82. .loginPage("/login")
  83. );
  84. return http.build();
  85. }
  86. }
  87. ----
  88. === Goals of the Lambda DSL
  89. The Lambda DSL was created to accomplish to following goals:
  90. - Automatic indentation makes the configuration more readable.
  91. - There is no need to chain configuration options using `.and()`
  92. - The Spring Security DSL has a similar configuration style to other Spring DSLs such as Spring Integration and Spring Cloud Gateway.
  93. == Use `.with()` instead of `.apply()` for Custom DSLs
  94. In versions prior to 6.2, if you had a xref:servlet/configuration/java.adoc#jc-custom-dsls[custom DSL], you would apply it to the `HttpSecurity` using the `HttpSecurity#apply(...)` method.
  95. However, starting from version 6.2, this method is deprecated and will be removed in 7.0 because it will no longer be possible to chain configurations using `.and()` once `.and()` is removed (see https://github.com/spring-projects/spring-security/issues/13067).
  96. Instead, it is recommended to use the new `.with(...)` method.
  97. For more information about how to use `.with(...)` please refer to the xref:servlet/configuration/java.adoc#jc-custom-dsls[Custom DSLs section].