-
Notifications
You must be signed in to change notification settings - Fork 6k
Please add support for nested builders in the DSL #5557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is an example of how to configure HTTP security using lambdas.
|
@eleftherias Here's a Kotlin-based @Configuration
class SecurityConfiguration : WebSecurityConfigurerAdapter() {
override fun configure(http: HttpSecurity) {
//@formatter:off
http
.authorizeRequests().anyRequest().authenticated()
.and()
.oauth2Login()
.and()
.oauth2ResourceServer().jwt()
http.requiresChannel().anyRequest().requiresSecure(); // <1>
http.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()); // <2>
http.headers()
.contentSecurityPolicy("script-src 'self'; report-uri /csp-report-endpoint/"); // <3>
//@formatter:on
}
} P.S. I enjoyed learning about this feature on the Bootiful Podcast with @joshlong today! |
@mraible here is the same configuration using lambdas @Configuration
class SecurityConfiguration : WebSecurityConfigurerAdapter() {
override fun configure(http: HttpSecurity) {
http
.authorizeRequests { requests ->
requests
.anyRequest().authenticated()
}
.oauth2Login { }
.oauth2ResourceServer { oauth2ResourceServer ->
oauth2ResourceServer
.jwt { }
}
.requiresChannel { requiresChannel ->
requiresChannel
.anyRequest().requiresSecure() // <1>
}
.csrf { csrf ->
csrf
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) // <2>
}
.headers { headers ->
headers
.contentSecurityPolicy("script-src 'self'; report-uri /csp-report-endpoint/")// <3>
}
}
} In this example, I have chained all the configuration options, but you can separate them like you have done in the non-lambda configuration if you prefer. Additionally, you could emit the
Also, check out the native Kotlin DSL that we've been working on. |
This could take advantage of lambdas in Java 8.
This would mean we could configure things in Spring Security without worrying about indents.
Spring Integration and Spring Cloud Gateway both support similar nested builder style DSLs.
The text was updated successfully, but these errors were encountered: