kotlin.adoc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 `WebSecurityConfigurerAdapter`) that is being invoked behind the scenes.
  14. It has a method called `configure` with the following default implementation:
  15. ====
  16. [source,kotlin]
  17. ----
  18. fun configure(http: HttpSecurity) {
  19. http {
  20. authorizeRequests {
  21. authorize(anyRequest, authenticated)
  22. }
  23. formLogin { }
  24. httpBasic { }
  25. }
  26. }
  27. ----
  28. ====
  29. The default configuration (shown in the preceding listing):
  30. * Ensures that any request to our application requires the user to be authenticated
  31. * Lets users authenticate with form-based login
  32. * Lets users authenticate with HTTP Basic authentication
  33. Note that this configuration is parallels the XML namespace configuration:
  34. ====
  35. [source,xml]
  36. ----
  37. <http>
  38. <intercept-url pattern="/**" access="authenticated"/>
  39. <form-login />
  40. <http-basic />
  41. </http>
  42. ----
  43. ====
  44. == Multiple HttpSecurity Instances
  45. We can configure multiple HttpSecurity instances, just as we can have multiple `<http>` blocks.
  46. The key is to extend the `WebSecurityConfigurerAdapter` multiple times.
  47. The following example has a different configuration for URL's that start with `/api/`:
  48. ====
  49. [source,kotlin]
  50. ----
  51. @EnableWebSecurity
  52. class MultiHttpSecurityConfig {
  53. @Bean <1>
  54. public fun userDetailsService(): UserDetailsService {
  55. val users: User.UserBuilder = User.withDefaultPasswordEncoder()
  56. val manager = InMemoryUserDetailsManager()
  57. manager.createUser(users.username("user").password("password").roles("USER").build())
  58. manager.createUser(users.username("admin").password("password").roles("USER","ADMIN").build())
  59. return manager
  60. }
  61. @Configuration
  62. @Order(1) <2>
  63. class ApiWebSecurityConfigurationAdapter: WebSecurityConfigurerAdapter() {
  64. override fun configure(http: HttpSecurity) {
  65. http {
  66. securityMatcher("/api/**") <3>
  67. authorizeRequests {
  68. authorize(anyRequest, hasRole("ADMIN"))
  69. }
  70. httpBasic { }
  71. }
  72. }
  73. }
  74. @Configuration <4>
  75. class FormLoginWebSecurityConfigurerAdapter: WebSecurityConfigurerAdapter() {
  76. override fun configure(http: HttpSecurity) {
  77. http {
  78. authorizeRequests {
  79. authorize(anyRequest, authenticated)
  80. }
  81. formLogin { }
  82. }
  83. }
  84. }
  85. }
  86. ----
  87. <1> Configure Authentication as usual.
  88. <2> Create an instance of `WebSecurityConfigurerAdapter` that contains `@Order` to specify which `WebSecurityConfigurerAdapter` 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 `WebSecurityConfigurerAdapter`.
  91. If the URL does not start with `/api/`, this configuration is used.
  92. This configuration is considered after `ApiWebSecurityConfigurationAdapter`, since it has an `@Order` value after `1` (no `@Order` defaults to last).
  93. ====