method-security.adoc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. [[native-image-method-security]]
  2. = Method Security in GraalVM Native Image
  3. Although xref:servlet/authorization/method-security.adoc[Method Security] is supported in GraalVM Native Image, there are some use cases that need additional hints provided by the application.
  4. == Using `@PreAuthorize` and `@PostAuthorize` Annotations
  5. Using `@PreAuthorize` and `@PostAuthorize` annotations require additional hints if you have a custom implementation of `UserDetails` or `Authentication` classes.
  6. Let's take an example where you have a custom implementation of `UserDetails` class as follows and that implementation is returned by your `UserDetailsService`:
  7. .Custom Implementation of UserDetails
  8. [source,java]
  9. ----
  10. public class CustomUserDetails implements UserDetails {
  11. private final String username;
  12. private final String password;
  13. private final Collection<? extends GrantedAuthority> authorities;
  14. public boolean isAdmin() {
  15. return this.authorities.contains(new SimpleGrantedAuthority("ROLE_ADMIN"));
  16. }
  17. // constructors, getters and setters
  18. }
  19. ----
  20. And you want to use the `isAdmin()` method inside a `@PreAuthorize` annotation as follows:
  21. .Using isAdmin() to secure a method
  22. [source,java]
  23. ----
  24. @PreAuthorize("principal?.isAdmin()")
  25. public String hello() {
  26. return "Hello!";
  27. }
  28. ----
  29. [NOTE]
  30. ====
  31. Remember that you need to xref:servlet/authorization/method-security.adoc#jc-enable-method-security[add `@EnableMethodSecurity` annotation] to your configuration class to enable method security annotations.
  32. ====
  33. If you https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html#native-image.developing-your-first-application[run the native image] of your application with the above configuration, you will get an error similar to the following when trying to invoke the `hello()` method:
  34. [source]
  35. ----
  36. failed: java.lang.IllegalArgumentException: Failed to evaluate expression 'principal?.isAdmin()' with root cause
  37. org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method isAdmin() cannot be found on type com.mypackage.CustomUserDetails
  38. ----
  39. Which means that the `isAdmin()` method cannot be found on the `CustomUserDetails` class.
  40. This is because Spring Security uses reflection to invoke the `isAdmin()` method and GraalVM Native Image does not support reflection by default.
  41. To fix this issue, you need to give hints to GraalVM Native Image to allow reflection on the `CustomUserDetails#isAdmin()` method.
  42. We can do that by providing a https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html#native-image.advanced.custom-hints[custom hint].
  43. In this example we are going to use {spring-framework-reference-url}core.html#core.aot.hints.register-reflection-for-binding[the `@RegisterReflectionForBinding` annotation].
  44. [NOTE]
  45. ====
  46. You might need to register all your classes that you want to use in your `@PreAuthorize` and `@PostAuthorize` annotations.
  47. ====
  48. .Using @RegisterReflectionForBinding
  49. [source,java]
  50. ----
  51. @Configuration
  52. @RegisterReflectionForBinding(CustomUserDetails.class)
  53. public class MyConfiguration {
  54. //...
  55. }
  56. ----
  57. And that's it, now you can run the native image of your application and it should work as expected.