method-security.adoc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. ====
  8. .Custom Implementation of UserDetails
  9. [source,java]
  10. ----
  11. public class CustomUserDetails implements UserDetails {
  12. private final String username;
  13. private final String password;
  14. private final Collection<? extends GrantedAuthority> authorities;
  15. public boolean isAdmin() {
  16. return this.authorities.contains(new SimpleGrantedAuthority("ROLE_ADMIN"));
  17. }
  18. // constructors, getters and setters
  19. }
  20. ----
  21. ====
  22. And you want to use the `isAdmin()` method inside a `@PreAuthorize` annotation as follows:
  23. ====
  24. .Using isAdmin() to secure a method
  25. [source,java]
  26. ----
  27. @PreAuthorize("principal?.isAdmin()")
  28. public String hello() {
  29. return "Hello!";
  30. }
  31. ----
  32. ====
  33. [NOTE]
  34. ====
  35. 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.
  36. ====
  37. 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:
  38. [source]
  39. ----
  40. failed: java.lang.IllegalArgumentException: Failed to evaluate expression 'principal?.isAdmin()' with root cause
  41. org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method isAdmin() cannot be found on type com.mypackage.CustomUserDetails
  42. ----
  43. Which means that the `isAdmin()` method cannot be found on the `CustomUserDetails` class.
  44. This is because Spring Security uses reflection to invoke the `isAdmin()` method and GraalVM Native Image does not support reflection by default.
  45. To fix this issue, you need to give hints to GraalVM Native Image to allow reflection on the `CustomUserDetails#isAdmin()` method.
  46. 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].
  47. In this example we are going to use {spring-framework-reference-url}core.html#core.aot.hints.register-reflection-for-binding[the `@RegisterReflectionForBinding` annotation].
  48. [NOTE]
  49. ====
  50. You might need to register all your classes that you want to use in your `@PreAuthorize` and `@PostAuthorize` annotations.
  51. ====
  52. ====
  53. .Using @RegisterReflectionForBinding
  54. [source,java]
  55. ----
  56. @Configuration
  57. @RegisterReflectionForBinding(CustomUserDetails.class)
  58. public class MyConfiguration {
  59. //...
  60. }
  61. ----
  62. ====
  63. And that's it, now you can run the native image of your application and it should work as expected.