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

Throw AuthorizationDeniedException when AuthorizationResult is available #15706

Closed
mauromol opened this issue Aug 28, 2024 · 3 comments
Closed
Assignees
Labels
in: core An issue in spring-security-core type: enhancement A general enhancement
Milestone

Comments

@mauromol
Copy link

Expected Behavior

It would be very useful if the AccessDeniedException contained the AuthorizationDecision which led to the authorization failure.

Current Behavior

No way to get the AuthorizationDecision from the AccessDeniedException. Seems like this info is only available by means of an AuthorizationEvent.

Context

Writing a REST API. Applying method security on RestController methods with @Secured. Using a ControllerAdvice to intercept AccessDeniedExceptions. Using ErrorResponse/ProblemDetail facilities of Spring 6 to produce RFC 9457-compliant error responses, and a proper message bundle feeding the application context message source. I would like to have an error detail message code like this for this kind of error:

problemDetail.org.springframework.security.access.AccessDeniedException=User is missing the required authority; one of: {0}

The list of allowed authorities for the target resource would be available in the AuthorityAuthorizationDecision created by the AuthorityAuthorizationManager when checking for access grant: if I had access to it, I could easily use it to supply the necessary message arguments to get the final problem detail message from the AccessDeniedException in my ControllerAdvice.
The AuthorizationFilter has all of this in its hands, it could just pass the decision to the exception it creates.

Suggestions to any alternative way to achieve this would be welcome.

@mauromol mauromol added status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement labels Aug 28, 2024
@marcusdacoregio
Copy link
Contributor

Hi @mauromol, thanks for the report.

We have recently created AuthorizationDeniedException as part of #14601, and that exception contains the AuthorizationResult.

Since AuthorizationDeniedException extends AccessDeniedException I believe we could change AuthorizationFilter to throw it, the same could be done in org.springframework.security.authorization.AuthorizationManager#verify. I'll bring this discussion to the team and I'll get back to you.

@marcusdacoregio marcusdacoregio self-assigned this Aug 30, 2024
@marcusdacoregio marcusdacoregio added for: team-attention This ticket should be discussed as a team before proceeding and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 30, 2024
@mauromol
Copy link
Author

mauromol commented Sep 2, 2024

That would be great, thanks for your feedback!

@marcusdacoregio marcusdacoregio added the in: core An issue in spring-security-core label Sep 9, 2024
@marcusdacoregio marcusdacoregio added this to the 6.4.0-M4 milestone Sep 9, 2024
@marcusdacoregio marcusdacoregio removed the for: team-attention This ticket should be discussed as a team before proceeding label Sep 10, 2024
@marcusdacoregio
Copy link
Contributor

marcusdacoregio commented Sep 10, 2024

Thanks for the report @mauromol, this is now merged into main and is scheduled for the 6.4.0-M4 release on Sept 16th.

@marcusdacoregio marcusdacoregio changed the title Add AuthorizationDecision to AccessDeniedException Throw AuthorizationDeniedException when AuthorizationResult is available Sep 10, 2024
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
Status: No status
Development

No branches or pull requests

2 participants