2
0

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