Skip to content

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

Closed
Tracked by #1149
joshlong opened this issue Jul 22, 2018 · 9 comments
Closed
Tracked by #1149

Please add support for nested builders in the DSL #5557

joshlong opened this issue Jul 22, 2018 · 9 comments
Assignees
Labels
in: config An issue in spring-security-config type: enhancement A general enhancement
Milestone

Comments

@joshlong
Copy link
Member

This could take advantage of lambdas in Java 8.

This would mean we could configure things in Spring Security without worrying about indents.

foo 
  .a ( aSpec  ->aSpec.foo() .bar()) 
  .b ( bSpec -> bSpec x().y())

Spring Integration and Spring Cloud Gateway both support similar nested builder style DSLs.

kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this issue Aug 26, 2019
@eleftherias
Copy link
Contributor

This is an example of how to configure HTTP security using lambdas.

@Override
protected void configure(HttpSecurity http) throws Exception {
	http
		.authorizeRequests(authorizeRequests ->
			authorizeRequests
				.antMatchers("/css/**", "/index").permitAll()
				.antMatchers("/user/**").hasRole("USER")
		)
		.formLogin(formLogin ->
			formLogin
				.loginPage("/login")
				.failureUrl("/login-error")
		);
}

@mraible
Copy link
Contributor

mraible commented Nov 22, 2019

@eleftherias Here's a Kotlin-based SecurityConfiguration class. How do I change it to use lambdas?

@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!

@eleftherias
Copy link
Contributor

eleftherias commented Nov 25, 2019

@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 -> and use the it keyword, for example:

http
        .authorizeRequests { 
            it
                .anyRequest().authenticated()
        }

Also, check out the native Kotlin DSL that we've been working on.
It's currently experimental, but we're planning on integrating it into core Spring Security in the near future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: config An issue in spring-security-config type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

7 participants