kotlin.adoc 3.9 KB

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