configuration.adoc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. The prior configuration style will not be valid in Spring Security 7 where the usage of the Lambda DSL will be required.
  6. You may have seen this style of configuration in the Spring Security documentation or samples.
  7. Let us take a look at how a lambda configuration of HTTP security compares to the previous configuration style.
  8. ====
  9. [source,java]
  10. .Configuration using lambdas
  11. ----
  12. @Configuration
  13. @EnableWebSecurity
  14. public class SecurityConfig {
  15. @Bean
  16. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  17. http
  18. .authorizeHttpRequests(authorize -> authorize
  19. .requestMatchers("/blog/**").permitAll()
  20. .anyRequest().authenticated()
  21. )
  22. .formLogin(formLogin -> formLogin
  23. .loginPage("/login")
  24. .permitAll()
  25. )
  26. .rememberMe(Customizer.withDefaults());
  27. return http.build();
  28. }
  29. }
  30. ----
  31. ====
  32. ====
  33. [source,java]
  34. .Equivalent configuration without using lambdas
  35. ----
  36. @Configuration
  37. @EnableWebSecurity
  38. public class SecurityConfig {
  39. @Bean
  40. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  41. http
  42. .authorizeHttpRequests()
  43. .requestMatchers("/blog/**").permitAll()
  44. .anyRequest().authenticated()
  45. .and()
  46. .formLogin()
  47. .loginPage("/login")
  48. .permitAll()
  49. .and()
  50. .rememberMe();
  51. return http.build();
  52. }
  53. }
  54. ----
  55. ====
  56. === Lambda DSL Configuration Tips
  57. When comparing the two samples above, you will notice some key differences:
  58. - In the Lambda DSL there is no need to chain configuration options using the `.and()` method.
  59. The `HttpSecurity` instance is automatically returned for further configuration after the call to the lambda method.
  60. - `Customizer.withDefaults()` enables a security feature using the defaults provided by Spring Security.
  61. This is a shortcut for the lambda expression `it -> {}`.
  62. === WebFlux Security
  63. You may also configure WebFlux security using lambdas in a similar manner.
  64. Below is an example configuration using lambdas.
  65. ====
  66. [source,java]
  67. .WebFlux configuration using lambdas
  68. ----
  69. @Configuration
  70. @EnableWebFluxSecurity
  71. public class SecurityConfig {
  72. @Bean
  73. public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  74. http
  75. .authorizeExchange(exchanges -> exchanges
  76. .pathMatchers("/blog/**").permitAll()
  77. .anyExchange().authenticated()
  78. )
  79. .httpBasic(Customizer.withDefaults())
  80. .formLogin(formLogin -> formLogin
  81. .loginPage("/login")
  82. );
  83. return http.build();
  84. }
  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. - The 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.