Skip to content

Support hasScope in Method Security#18151

Merged
jzheaux merged 2 commits intospring-projects:mainfrom
ngocnhan-tran1996:gh-18013
Feb 10, 2026
Merged

Support hasScope in Method Security#18151
jzheaux merged 2 commits intospring-projects:mainfrom
ngocnhan-tran1996:gh-18013

Conversation

@ngocnhan-tran1996
Copy link
Copy Markdown
Contributor

@ngocnhan-tran1996 ngocnhan-tran1996 commented Nov 8, 2025

Closes: gh-18013

@jzheaux
Copy link
Copy Markdown
Contributor

jzheaux commented Nov 21, 2025

Hi, @ngocnhan-tran1996. We want to be careful about adding to the expression root, especially now that it has implications for AuthorizationManagerFactory. Alternatively, we could consider an interface OAuth2AuthorizationManagerFactory like this:

public interface OAuth2AuthorizationManagerFactory<T> {
    default AuthorizationManager<T> hasScope(String scope) {
        return OAuth2AuthorizationManagers.hasScope(scope);
    }

    // ...
}

And a default implementation:

@Bean 
OAuth2AuthorizationManagerFactory<Object> oauth2() {
    return new DefaultOAuth2AuthorizationManagerFactory();
}

That takes an AuthorizationManagerFactory as a parameter in support of MFA:

@Bean 
OAuth2AuthorizationManagerFactory<Object> oauth2(AuthorizationManagerFactory<Object> mfa) {
    return new OAuth2AuthorizationManagerFactory(mfa);
}

And then do:

@PreAuthorize("@oauth2.hasScope('message:read')")

I like this pattern since it allows for other modules to add their own expressions as well, without needing to change or extend SecurityExpressionRoot.

@ngocnhan-tran1996 ngocnhan-tran1996 force-pushed the gh-18013 branch 2 times, most recently from e6b1d19 to ffc9adf Compare November 22, 2025 05:06
@ngocnhan-tran1996
Copy link
Copy Markdown
Contributor Author

@jzheaux

I’ve made the requested changes. Let me know if anything else is needed.

Copy link
Copy Markdown
Contributor

@jzheaux jzheaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @ngocnhan-tran1996! I've left some feedback inline.

@jzheaux jzheaux added this to the 7.1.0-M1 milestone Dec 15, 2025
@jgrandja jgrandja modified the milestones: 7.1.0-M1, 7.1.x Jan 19, 2026
@jzheaux jzheaux modified the milestones: 7.1.x, 7.1.0-M2 Feb 6, 2026
ngocnhan-tran1996 and others added 2 commits February 10, 2026 14:42
Closes spring-projectsgh-18013

Signed-off-by: Tran Ngoc Nhan <ngocnhan.tran1996@gmail.com>
Issue spring-projectsgh-18013

Signed-off-by: Josh Cummings <3627351+jzheaux@users.noreply.github.com>
@jzheaux jzheaux changed the title Add hasScope as a valid SpEL expression to PreAuthorize Support hasScope in Method Security Feb 10, 2026
@jzheaux jzheaux added type: enhancement A general enhancement in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 10, 2026
@jzheaux jzheaux merged commit 705fa60 into spring-projects:main Feb 10, 2026
7 checks passed
@jzheaux
Copy link
Copy Markdown
Contributor

jzheaux commented Feb 10, 2026

Thanks for the PR, @ngocnhan-tran1996! This is now merged into main

@ngocnhan-tran1996 ngocnhan-tran1996 deleted the gh-18013 branch February 11, 2026 02:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add hasScope as a valid SpEL expression to PreAuthorize, etc.

4 participants