-
Notifications
You must be signed in to change notification settings - Fork 6k
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
(Spring Boot 2.7->3.2) Duplicate @PreAuthorize annotation error across class hierarchy #15097
Comments
Hi, @arnaldop, thanks for the suggestion. Can you please clarify what in your sample you want to be able to do? I can't tell if you want to be able to uncomment the commented code, if you want to be able to replace the commented line with the uncommented one, etc. |
@jzheaux, as far as my code sample above, the uncommented version is how it worked in Spring Boot 2.7. The methods in
Instead, I want to be able to define overriding An example of this in Spring Data is the Considering the Spring Security test class
|
If you use v6.3.0,you can Using Meta Annotations as below:
|
@kse-music, your proposed solution did not work, at least with Spring Security 6.2.4 (dependency from Spring Boot 3.2). And it makes sense. All that we did with the meta-annotation was to move the duplicate Here is my meta-annotation:
Here is the code using the meta-annotation:
Here is the error:
|
@arnaldop after moving the PreAuthorize annotation value on UserDependentController to MetaPreAuthorize, you also need to remove the PreAuthorize annotation on parent class and interface. |
Meta-annotations are available prior to 6.3. Here is the doc for 6.2.4:
https://docs.spring.io/spring-security/reference/6.2/servlet/authorization/method-security.html#meta-annotations
So I feel like Meta-Annotations is a false solution to the problem. As I mentioned previously, unless I am grossly misunderstanding Meta-Annotations, the `@PreAuthorize` annotation will still be present multiple times, meaning I still get the warning. I tested this theory and found it to be true.
So at this time, I see that my original report is indeed an issue, and that there is no workaround for it. But if I am incorrect, please advise. Thank you!
|
- Now searches methods and classes together in the hierarchical search instead of first the method hierarchy and then the class hierarchy - Stops when it finds annotations on a method or class, sees if the closest annotation is not duplicated. Closes spring-projectsgh-15097
Hi @jzheaux , when is the fix available? I also face this issue on Spring boot 3.2.0 (Webflux, Security). Thanks |
Will this be released as well to spring security 5.8.x branch? I'm facing the same issue with 5.8.14 |
Hey everyone, Just wanted to share that the annoying AnnotationConfigurationException related to @PreAuthorize annotations in Spring Security has finally been fixed! The Problem: Previously, Spring Security had trouble with @PreAuthorize annotations when used in class hierarchies. If both a parent class and a child class had the annotation, even with different expressions, it would throw an AnnotationConfigurationException because it incorrectly detected them as duplicates. The Solution: This issue has been resolved in Spring Security version 6.4.0-M2. The framework now correctly handles @PreAuthorize inheritance, allowing you to define different security expressions at different levels of your class hierarchy without conflicts. How to Update: To get this fix, simply update your Spring Security dependency to version 6.4.0-M2 or higher: org.springframework.security spring-security-core 6.4.0-M2 emember to update all your Spring Security dependencies to the same version for consistency.Important Note: Keep in mind that 6.4.0-M2 is a pre-release version (milestone). While it includes this important fix, it might contain other bugs or instabilities. If you're looking for a more stable release, you might consider waiting for the official 6.4.0 release. Hope this helps! Let me know if you have any questions. |
Describe the bug
I have an abstract class that has the
@PreAuthorize
annotation. Its subclass also has an identical@PreAuthorize
annotation.To Reproduce
Attempting to invoke an endpoint in the subclass results in this error message:
Expected behavior
In Spring Boot 2.7.3, this code worked as is. (
org.springframework.security:spring-security-core:jar:5.7.11:compile
)After migrating to Spring Boot 3.2, this no longer works. (
org.springframework.security:spring-security-core:jar:6.2.4:compile
)I was able to remove exact duplicates, but as the code sample below reveals, there are places there the SpEL is not the same, so they should not be considered duplicated.
Ideally, I should be able to define the
@PreAuthorize
annotation in the superclass, and only override it as needed in subclasses. This is how it worked previously.Sample
The text was updated successfully, but these errors were encountered: