11
22[[kotlin-config]]
33= Kotlin Configuration
4+
45Spring Security Kotlin configuration has been available since Spring Security 5.3.
56It lets users configure Spring Security by using a native Kotlin DSL.
67
@@ -23,27 +24,27 @@ import org.springframework.security.config.annotation.web.invoke
2324
2425@Bean
2526open fun filterChain(http: HttpSecurity): SecurityFilterChain {
26- http {
27+ http {
2728 authorizeHttpRequests {
2829 authorize(anyRequest, authenticated)
2930 }
30- formLogin { }
31- httpBasic { }
31+ formLogin { }
32+ httpBasic { }
3233 }
3334 return http.build()
3435}
3536----
3637
3738[NOTE]
38- Make sure that import the `invoke` function in your class, sometimes the IDE will not auto-import it causing compilation issues.
39+ Make sure to import the `invoke` function in your class, as the IDE will not always auto-import the method, causing compilation issues.
3940
4041The default configuration (shown in the preceding listing):
4142
4243* Ensures that any request to our application requires the user to be authenticated
4344* Lets users authenticate with form-based login
4445* Lets users authenticate with HTTP Basic authentication
4546
46- Note that this configuration is parallels the XML namespace configuration:
47+ Note that this configuration parallels the XML namespace configuration:
4748
4849[source,xml]
4950----
@@ -58,13 +59,13 @@ Note that this configuration is parallels the XML namespace configuration:
5859
5960We can configure multiple `HttpSecurity` instances, just as we can have multiple `<http>` blocks.
6061The key is to register multiple `SecurityFilterChain` ``@Bean``s.
61- The following example has a different configuration for URL's that start with `/api/`:
62+ The following example has a different configuration for URLs that start with `/api/`:
6263
6364[source,kotlin]
6465----
65- @Configuration
6666import org.springframework.security.config.annotation.web.invoke
6767
68+ @Configuration
6869@EnableWebSecurity
6970class MultiHttpSecurityConfig {
7071 @Bean <1>
@@ -104,7 +105,7 @@ class MultiHttpSecurityConfig {
104105
105106<1> Configure Authentication as usual.
106107<2> Create an instance of `SecurityFilterChain` that contains `@Order` to specify which `SecurityFilterChain` should be considered first.
107- <3> The `http.antMatcher ` states that this `HttpSecurity` is applicable only to URLs that start with `/api/`
108+ <3> The `http.securityMatcher ` states that this `HttpSecurity` is applicable only to URLs that start with `/api/`
108109<4> Create another instance of `SecurityFilterChain`.
109110If the URL does not start with `/api/`, this configuration is used.
110111This configuration is considered after `apiFilterChain`, since it has an `@Order` value after `1` (no `@Order` defaults to last).
0 commit comments