Closed
Description
Describe the bug
I perforrmed the method security migration, to replace deprecated EnableGlobalMethodSecurity
annotations with the new EnableMethodSecurity
annotations, and I've got an exception when using CGLIB and the PreAuthorize
annotation.
To Reproduce
- Clone the sample repository
- Run the application
- Navigate to the http://localhost:8080/hello
- Provide credentials: username: user, password: pass
- Notice the exception:
org.springframework.core.annotation.AnnotationConfigurationException: Found more than one annotation of type interface org.springframework.security.access.prepost.PreAuthorize attributed to public org.springframework.http.ResponseEntity com.example.preauth.SayHelloController.sayHello() Please remove the duplicate annotations and publish a bean to handle your authorization logic.
at org.springframework.security.authorization.method.AuthorizationAnnotationUtils.findUniqueAnnotation(AuthorizationAnnotationUtils.java:65) ~[spring-security-core-6.1.2.jar:6.1.2]
at org.springframework.security.authorization.method.PreAuthorizeExpressionAttributeRegistry.findPreAuthorizeAnnotation(PreAuthorizeExpressionAttributeRegistry.java:71) ~[spring-security-core-6.1.2.jar:6.1.2]
Expected behavior
- Clone the sample repository
- Comment line 15 in the
SecurityConfiguration
class - Uncomment line 17 in the
SecurityConfiguration
class - Run the application
- Navigate to the http://localhost:8080/hello
- Provide credentials: username: user, password: pass
- Application produces hello response
Sample