spring-projects / spring-projects/spring-security

Declare authorization rules one at a time

Open
#16,509 0 comments 0 reactions 1 assignee View on GitHub

@jzheaux is already working on this.

Since Feb 4, 2025.

in: config type: enhancement
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

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())

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.