-
Notifications
You must be signed in to change notification settings - Fork 6k
AuthorizeReturnObject should target the authorized object within Spring Data components #15994
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
Comments
Thanks for this report, @noshua. Spring Data types are important for For now, you can place Or, if you want to support private final TargetVisitor page = (proxyFactory, target) -> {
if (target instanceof PageImpl<?> page) {
List<Object> content = (List<Object>) proxyFactory.proxy(page.getContent());
return new PageImpl<>(content, page.getPageable(), page.getTotalElements());
}
return null;
};
@Bean
Customizer<AuthorizationAdvisorProxyFactory> addVisitors() {
return (factory) -> factory.setTargetVisitor(TargetVisitor.of(page, TargetVisitor.defaults()));
} |
Sorry for the delayed response @jzheaux but I needed some time to investigate. Putting Is it possible to achieve that the I created a new sample branch to show this behaviour: https://github.com/noshua/authorize-spring-data/tree/authorizeController |
Thanks for the sample, that's very helpful. As with @Bean
Customizer<AuthorizationAdvisorProxyFactory> addVisitors() {
return (factory) -> factory.setTargetVisitor(TargetVisitor.of(responseEntity, TargetVisitor.defaults()));
}
private final TargetVisitor responseEntity = (proxyFactory, target) -> {
if (target instanceof ResponseEntity<?> entity) {
Object body = entity.getBody();
HttpHeaders header = entity.getHeaders();
HttpStatusCode code = entity.getStatusCode();
return new ResponseEntity<>(proxyFactory.proxy(body), header, code);
}
return null;
}; To propagate the @ExceptionHandler(HttpMessageNotWritableException.class)
View handleWrite(HttpMessageNotWritableException ex) {
if (ex.getRootCause() instanceof AuthorizationDeniedException denied) {
return new AbstractView() {
@Override
protected void renderMergedOutputModel(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
throw ex;
}
};
}
throw ex;
} I've added a PR to your sample repo to demonstrate both of these. |
Related to #14717 |
…ject Closes spring-projectsgh-15994 Signed-off-by: Evgeniy Cheban <[email protected]>
…ject Closes spring-projectsgh-15994 Signed-off-by: Evgeniy Cheban <[email protected]>
…ject Closes spring-projectsgh-15994 Signed-off-by: Evgeniy Cheban <[email protected]>
…ject Closes spring-projectsgh-15994 Signed-off-by: Evgeniy Cheban <[email protected]>
…ject Closes spring-projectsgh-15994 Signed-off-by: Evgeniy Cheban <[email protected]>
…ject Closes spring-projectsgh-15994 Signed-off-by: Evgeniy Cheban <[email protected]>
…ject Closes spring-projectsgh-15994 Signed-off-by: Evgeniy Cheban <[email protected]>
Describe the bug
Using Authorizing Arbitrary Objects of Spring Security in combination with a Pageable Spring Data result fails.
To Reproduce
java.lang.ClassCastException: class org.springframework.security.authorization.method.AuthorizationAdvisorProxyFactory$ContainerTypeVisitor$$Lambda/0x0000791458a2cb00 cannot be cast to class org.springframework.data.domain.Page (org.springframework.security.authorization.method.AuthorizationAdvisorProxyFactory$ContainerTypeVisitor$$Lambda/0x0000791458a2cb00 and org.springframework.data.domain.Page are in unnamed module of loader 'app')
Expected behavior
A paged result of security proxied objects should be returned from the repository method.
Sample
https://github.com/noshua/authorize-spring-data
The text was updated successfully, but these errors were encountered: