kotlin.adoc 3.6 KB

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