I'm currently working on avoid literals inside the security annotations. The reason is that in large applications having literals inside these annotations rely on several errors, also refactoring permissions names will be more easy.
I created a @Bean
it's purpose is register the Enum
packages that I want to then use in the @Preautorize
annotations in this way @PreAuthorize("hasAuthority(T(Permissions).CREATE)")
@Bean
static MethodSecurityExpressionHandler methodSecurityExpressionHandler() {
return new DefaultMethodSecurityExpressionHandler() {
@Override
public StandardEvaluationContext createEvaluationContext(Supplier<Authentication> authentication,
MethodInvocation mi) {
StandardEvaluationContext evaluationContext =
(StandardEvaluationContext) super.createEvaluationContext(authentication, mi);
((StandardTypeLocator) evaluationContext.getTypeLocator())
.registerImport(PermissionName.class.getPackage().getName());
return evaluationContext;
}
};
}
but the functions T()
can't resolve the class Permissions
.
About the technical information I'm working on Spring Boot 3.2.4
.
If you need more information or discuss about it fell free! Thanks for your help!
@Bean
method to ensure that Spring publishes the bean before it initializes Spring Security’s method security@Configuration
classes. You produce the bean by the@Component
annotation which does not satisfy this recommendation.