spring-projects / spring-projects/spring-security

AccessDecisionManager of GlobalMethodSecurityConfiguration is not MessageSourceAware

Open
#5,031 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

https://github.com/spring-projects/spring-security/blob/c24490cbb875d6155ecd4ca5fcff6a1e35d47ae1/config/src/main/java/org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityConfiguration.java#L259

AffirmativeBased access decision manager which is used in GlobalMethodSecurityConfiguration is created by accessDecisionManager() method and it is not a Spring bean. As a result, setMessageSource() method gets never executed which makes this access decision manager not MessageSourceAware although AffirmativeBased implements MessageSourceAware interface. This avoids customizing spring security access denied messages generated by @PreAuthorize annotation.

The solution for that is to set the messageSource manually like below;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {

    @Autowired
    private MessageSource messageSource;

    @Override
    protected AccessDecisionManager accessDecisionManager() {
        AffirmativeBased manager = (AffirmativeBased) super.accessDecisionManager();
        manager.setMessageSource(messageSource);
        return manager;
    }

}

Just for reference, this issue doesn't apply to the access decision manager of AbstractInterceptUrlConfigurer where messageSource is set during postProcess.

This problem is also mentioned in this stackoverflow post.

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 in config/src/main/java/org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityConfiguration.java at accessDecisionManager(), then compare the message-source handling described for AbstractInterceptUrlConfigurer. Done means the AffirmativeBased manager used by global method security receives the MessageSource so customized access-denied messages from @PreAuthorize are effective.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.