kotlin.adoc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. import org.springframework.security.config.annotation.web.invoke
  19. @Bean
  20. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  21. http {
  22. authorizeRequests {
  23. authorize(anyRequest, authenticated)
  24. }
  25. formLogin { }
  26. httpBasic { }
  27. }
  28. return http.build()
  29. }
  30. ----
  31. ====
  32. [NOTE]
  33. Make sure that import the `invoke` function in your class, sometimes the IDE will not auto-import it causing compilation issues.
  34. The default configuration (shown in the preceding listing):
  35. * Ensures that any request to our application requires the user to be authenticated
  36. * Lets users authenticate with form-based login
  37. * Lets users authenticate with HTTP Basic authentication
  38. Note that this configuration is parallels the XML namespace configuration:
  39. ====
  40. [source,xml]
  41. ----
  42. <http>
  43. <intercept-url pattern="/**" access="authenticated"/>
  44. <form-login />
  45. <http-basic />
  46. </http>
  47. ----
  48. ====
  49. == Multiple HttpSecurity Instances
  50. We can configure multiple `HttpSecurity` instances, just as we can have multiple `<http>` blocks.
  51. The key is to register multiple `SecurityFilterChain` ``@Bean``s.
  52. The following example has a different configuration for URL's that start with `/api/`:
  53. ====
  54. [source,kotlin]
  55. ----
  56. @Configuration
  57. import org.springframework.security.config.annotation.web.invoke
  58. @EnableWebSecurity
  59. class MultiHttpSecurityConfig {
  60. @Bean <1>
  61. public fun userDetailsService(): UserDetailsService {
  62. val users: User.UserBuilder = User.withDefaultPasswordEncoder()
  63. val manager = InMemoryUserDetailsManager()
  64. manager.createUser(users.username("user").password("password").roles("USER").build())
  65. manager.createUser(users.username("admin").password("password").roles("USER","ADMIN").build())
  66. return manager
  67. }
  68. @Order(1) <2>
  69. @Bean
  70. open fun apiFilterChain(http: HttpSecurity): SecurityFilterChain {
  71. http {
  72. securityMatcher("/api/**") <3>
  73. authorizeRequests {
  74. authorize(anyRequest, hasRole("ADMIN"))
  75. }
  76. httpBasic { }
  77. }
  78. return http.build()
  79. }
  80. @Bean <4>
  81. open fun formLoginFilterChain(http: HttpSecurity): SecurityFilterChain {
  82. http {
  83. authorizeRequests {
  84. authorize(anyRequest, authenticated)
  85. }
  86. formLogin { }
  87. }
  88. return http.build()
  89. }
  90. }
  91. ----
  92. <1> Configure Authentication as usual.
  93. <2> Create an instance of `SecurityFilterChain` that contains `@Order` to specify which `SecurityFilterChain` should be considered first.
  94. <3> The `http.antMatcher` states that this `HttpSecurity` is applicable only to URLs that start with `/api/`
  95. <4> Create another instance of `SecurityFilterChain`.
  96. If the URL does not start with `/api/`, this configuration is used.
  97. This configuration is considered after `apiFilterChain`, since it has an `@Order` value after `1` (no `@Order` defaults to last).
  98. ====