Skip to content

Provide Native Hints for Beans used in Method Security Annotations #14652

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Tracked by #14595
marcusdacoregio opened this issue Feb 26, 2024 · 2 comments · Fixed by #15794
Closed
Tracked by #14595

Provide Native Hints for Beans used in Method Security Annotations #14652

marcusdacoregio opened this issue Feb 26, 2024 · 2 comments · Fixed by #15794
Assignees
Labels
in: core An issue in spring-security-core type: enhancement A general enhancement

Comments

@marcusdacoregio
Copy link
Contributor

We should look into how to provide native hints for bean methods used inside Method Security annotations.

Currently, in order to make this work:

@Component
class Authz {
   boolean check(Authentication authentication, String id, String permission) {
      return "admin".equals(authentication.getName());
   }
}

@PreAuthorize("@authz.check(authentication, #id, 'read')")
String findById(String id) {

}

We need to register hints:

@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
	hints.reflection().registerType(Authz.class, MemberCategory.INVOKE_DECLARED_METHODS);
}

Related to

@marcusdacoregio
Copy link
Contributor Author

@sbrannen provided some insights on how we could achieve that:

  • Parse the SpEL expression and then walk the AST to find nodes of type BeanReference and then retrieve the beanName from that.
  • beanName is a private field with no getter, we can probably use reflection or parse the bean name from the string returned from toStringAST().
  • Once we know the beanName we can look up that bean in the ApplicationContext and register hints for the bean's concrete type.

@sbrannen
Copy link
Member

beanName is a private field with no getter, we can probably use reflection or parse the bean name from the string returned from toStringAST().

Please note that BeanReference will have a getName() method in Spring Framework 6.2.

If it's needed in 6.1.x, we could consider backporting it.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
in: core An issue in spring-security-core type: enhancement A general enhancement
Projects
None yet
2 participants