spring-projects / spring-projects/spring-security

`ExceptionHandlingConfigurer#createDefaultAccessDeniedHandler` should always fallback to `AccessDeniedHandlerImpl`

Open
#18,871 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage type: enhancement
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

org.springframework.security.config.annotation.web.configurers.ExceptionHandlingConfigurer#createDefaultAccessDeniedHandler has this implementation:

LinkedHashMap<RequestMatcher, AccessDeniedHandler> defaultDeniedHandlerMappings

private AccessDeniedHandler createDefaultAccessDeniedHandler(H http) {
    if (this.defaultDeniedHandlerMappings.isEmpty()) {
        return new AccessDeniedHandlerImpl();
    }
    if (this.defaultDeniedHandlerMappings.size() == 1) {
        return this.defaultDeniedHandlerMappings.values().iterator().next();
    }
    return new RequestMatcherDelegatingAccessDeniedHandler(this.defaultDeniedHandlerMappings,
				new AccessDeniedHandlerImpl());
}

I'd propose that special case for this.defaultDeniedHandlerMappings.size() == 1 is not intuitive, because defaultDeniedHandlerMappings is always added for a specific RequestMatcher.

  • If I don't add a handler for a request matcher, then AccessDeniedHandlerImpl is always used.
  • If I add two handlers for request matchers, then AccessDeniedHandlerImpl is used when those matchers don't match
  • If I add exactly one handler for a request matcher, then that handler is also used when its request matcher does not match

I assume the reason is backwards compatibility rather than design choice?
Sorry if it has been reported before.

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.

Research direction

Start with org.springframework.security.config.annotation.web.configurers.ExceptionHandlingConfigurer#createDefaultAccessDeniedHandler and inspect how defaultDeniedHandlerMappings is used with request matchers. Verify the single-mapping behavior against the stated fallback expectations; done means an unmatched request uses AccessDeniedHandlerImpl consistently, with the relevant behavior covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
authorization, security
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.