kotlin.adoc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. [[kotlin-config]]
  2. = Kotlin Configuration
  3. Spring Security Kotlin configuration has been available since Spring Security 5.3.
  4. It lets users configure Spring Security by using a native Kotlin DSL.
  5. [NOTE]
  6. ====
  7. Spring Security provides https://github.com/spring-projects/spring-security-samples/tree/main/servlet/spring-boot/kotlin/hello-security[a sample application] to demonstrate the use of Spring Security Kotlin Configuration.
  8. ====
  9. [[kotlin-config-httpsecurity]]
  10. == HttpSecurity
  11. How does Spring Security know that we want to require all users to be authenticated?
  12. How does Spring Security know we want to support form-based authentication?
  13. There is a configuration class (called `SecurityFilterChain`) that is being invoked behind the scenes.
  14. It is configured with the following default implementation:
  15. ====
  16. [source,kotlin]
  17. ----
  18. @Bean
  19. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  20. http {
  21. authorizeRequests {
  22. authorize(anyRequest, authenticated)
  23. }
  24. formLogin { }
  25. httpBasic { }
  26. }
  27. return http.build()
  28. }
  29. ----
  30. ====
  31. The default configuration (shown in the preceding listing):
  32. * Ensures that any request to our application requires the user to be authenticated
  33. * Lets users authenticate with form-based login
  34. * Lets users authenticate with HTTP Basic authentication
  35. Note that this configuration is parallels the XML namespace configuration:
  36. ====
  37. [source,xml]
  38. ----
  39. <http>
  40. <intercept-url pattern="/**" access="authenticated"/>
  41. <form-login />
  42. <http-basic />
  43. </http>
  44. ----
  45. ====
  46. == Multiple HttpSecurity Instances
  47. We can configure multiple `HttpSecurity` instances, just as we can have multiple `<http>` blocks.
  48. The key is to register multiple `SecurityFilterChain` ``@Bean``s.
  49. The following example has a different configuration for URL's that start with `/api/`:
  50. ====
  51. [source,kotlin]
  52. ----
  53. @Configuration
  54. @EnableWebSecurity
  55. class MultiHttpSecurityConfig {
  56. @Bean <1>
  57. public fun userDetailsService(): UserDetailsService {
  58. val users: User.UserBuilder = User.withDefaultPasswordEncoder()
  59. val manager = InMemoryUserDetailsManager()
  60. manager.createUser(users.username("user").password("password").roles("USER").build())
  61. manager.createUser(users.username("admin").password("password").roles("USER","ADMIN").build())
  62. return manager
  63. }
  64. @Order(1) <2>
  65. @Bean
  66. open fun apiFilterChain(http: HttpSecurity): SecurityFilterChain {
  67. http {
  68. securityMatcher("/api/**") <3>
  69. authorizeRequests {
  70. authorize(anyRequest, hasRole("ADMIN"))
  71. }
  72. httpBasic { }
  73. }
  74. return http.build()
  75. }
  76. @Bean <4>
  77. open fun formLoginFilterChain(http: HttpSecurity): SecurityFilterChain {
  78. http {
  79. authorizeRequests {
  80. authorize(anyRequest, authenticated)
  81. }
  82. formLogin { }
  83. }
  84. return http.build()
  85. }
  86. }
  87. ----
  88. <1> Configure Authentication as usual.
  89. <2> Create an instance of `SecurityFilterChain` that contains `@Order` to specify which `SecurityFilterChain` should be considered first.
  90. <3> The `http.antMatcher` states that this `HttpSecurity` is applicable only to URLs that start with `/api/`
  91. <4> Create another instance of `SecurityFilterChain`.
  92. If the URL does not start with `/api/`, this configuration is used.
  93. This configuration is considered after `apiFilterChain`, since it has an `@Order` value after `1` (no `@Order` defaults to last).
  94. ====