Skip to content
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

Declare authorization rules one at a time #16509

Open
jzheaux opened this issue Jan 30, 2025 · 0 comments
Open

Declare authorization rules one at a time #16509

jzheaux opened this issue Jan 30, 2025 · 0 comments
Assignees
Labels
in: config An issue in spring-security-config type: enhancement A general enhancement
Milestone

Comments

@jzheaux
Copy link
Contributor

jzheaux commented Jan 30, 2025

The set of authorization rules declared in the Java DSL can get messy if not formatted properly. For example, a declaration like this:

http
    .authorizeHttpRequests((authorize) -> authorize
        .requestMatchers("/js/**", "/css/**", "/error").permitAll()
        .requestMatchers(HttpMethod.GET, "/api/**").hasAuthority("api")
        .anyRequest().denyAll()
    )

can quickly become hard to reason about when written like this:

http
    .authorizeHttpRequests((authorize) -> authorize
        .requestMatchers("/js/**", "/css/**", "/error").permitAll().requestMatchers(HttpMethod.GET, "/api/**").hasAuthority("api").anyRequest().denyAll()()
    )

The DSL could help users write authorization rules in a way that's easier to comprehend over time by requiring that rules be declared one at a time:

http
    .authorizeHttpRequests((request) -> {
        request.uris("/js/**", "/css/**", "/error").authorize().everyone();
        request.methods(HttpMethod.GET).uris("/api/**").authorize().authorities("api:read");
        request.unmatched().authorize().none();
    })

This would be achieved by having the authorization methods (permitAll, authorities, etc.) return void.

When there is only one rule, this simplifies to:

http
    .authorizeHttpRequests((requests) -> requests.authorize().authenticated())
@jzheaux jzheaux added in: config An issue in spring-security-config type: enhancement A general enhancement labels Jan 30, 2025
@jzheaux jzheaux added this to the 6.5.x milestone Jan 30, 2025
@jzheaux jzheaux self-assigned this Feb 4, 2025
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
Status: No status
Development

No branches or pull requests

1 participant