Skip to content
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

Create Authorized Proxy of Return Values #14669

Merged
merged 2 commits into from
Mar 19, 2024
Merged

Conversation

jzheaux
Copy link
Contributor

@jzheaux jzheaux commented Feb 29, 2024

Proxy arbitrary objects like so:

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

// ...

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

For return values of type Iterator, Collection, Stream, Array, and Map, the values are proxied instead of the collection.

Also when adding @EnableMethodSecurity, you can do:

@Component
@AuthorizeReturnObject
public class FlightRepository {
    Iterator<Flight> findAll() {
        return List.of(new Flight()).iterator();
    }
}

// ...

And then each Flight returned will be proxied.

Also works for @EnableReactiveMethodSecurity:

@Component
@AuthorizeReturnObject
public class FlightRepository {
    Flux<Flight> findAll() {
        return Flux.just(new Flight());
    }
}

// ...

Additionally, if you have types that you want to skip globally, for example, value return types, you can do:

@Bean 
AuthorizationProxyFactoryPredicate skipValueTypes() {
    return SkipAuthorizationProxyFactoryPredicate.skipValueTypes();
}

This is handy when using @AuthorizeReturnObject at the class level, in case some of the methods return value types (String, Integer, etc.) that you have no need to proxy,

@jzheaux jzheaux changed the title Gh 14597 Create Authorized Proxy of Return Values Feb 29, 2024
@rwinch
Copy link
Member

rwinch commented Mar 1, 2024

For return values of type Iterator, Collection, Stream, Array, and Map, the values are proxied instead of the collection.

We should consider how this works with @PostAuthorize and @PostFilter too.

@rwinch
Copy link
Member

rwinch commented Mar 1, 2024

I wonder if we could consider @AuthorizeReturnObject over @AuthorizedResult which

  • removes the d to align with PreAuthorize and PostAuthorize
  • Uses ReturnObject to align with SpEL keyword of returnObject

@jzheaux jzheaux force-pushed the gh-14597 branch 3 times, most recently from a8a6f5a to 2f3457e Compare March 1, 2024 21:59
@jzheaux jzheaux self-assigned this Mar 5, 2024
@jzheaux jzheaux force-pushed the gh-14597 branch 8 times, most recently from 769ed43 to 408295f Compare March 15, 2024 19:26
@jzheaux jzheaux marked this pull request as ready for review March 15, 2024 20:04
@jzheaux jzheaux added in: core An issue in spring-security-core type: enhancement A general enhancement labels Mar 15, 2024
@jzheaux jzheaux added this to the 6.3.0-M3 milestone Mar 15, 2024
@marcusdacoregio marcusdacoregio modified the milestones: 6.3.0-M3, 6.3.0-RC1 Mar 18, 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.

Thanks @jzheaux This looks good!

@jzheaux jzheaux merged commit ce54a6d into spring-projects:main Mar 19, 2024
3 checks passed
@jzheaux jzheaux deleted the gh-14597 branch March 19, 2024 16:27
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
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants