Skip to content

Add Programmatic Proxy Support for Method Security #14716

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

Merged
merged 1 commit into from
Mar 13, 2024

Conversation

jzheaux
Copy link
Contributor

@jzheaux jzheaux commented Mar 11, 2024

Proxy arbitrary objects like so:

public class Flight {
    @PreAuthorize("hasAuthority('airplane:read')")
    Double getAltitude() {
        return 35000d;
    }
}

// ...

var preAuthorize = AuthorizationManagerBeforeMethodInterceptor.preAuthorize();
var factory = new AuthorizationAdvisorProxyFactory(preAuthorize);
var flight = new Flight();
assertThatNoException().isThrownBy(flight::getAltitude);
Flight proxied = (Flight) factory.proxy(flight);
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(proxied::getAltitude);

For values of type Iterator, Collection, Stream, Array, Map, and Optional, the values are proxied instead of the collection. For Class values, ProxyFactory#getProxyClass is used.

Also when adding @EnableMethodSecurity, AuthorizationProxyFactory is available as a bean.

Closes gh-14596

@jzheaux jzheaux self-assigned this Mar 11, 2024
@jzheaux jzheaux added in: core An issue in spring-security-core type: enhancement A general enhancement labels Mar 11, 2024
Copy link
Member

@rwinch rwinch left a comment

Choose a reason for hiding this comment

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

Overall it looks really good! I've provided some feedback inline

@jzheaux jzheaux merged commit 52dfbfb into spring-projects:main Mar 13, 2024
@jzheaux jzheaux deleted the gh-14596 branch March 13, 2024 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core An issue in spring-security-core type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Programmatic Proxy Support for Method Security
2 participants