Skip to content

Commit f9b6d17

Browse files
Add initial Native section to reference docs
Also add the first subsection approaching how to handle method security in a native image Closes gh-12029 Closes gh-13226
1 parent 55c374d commit f9b6d17

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

docs/modules/ROOT/nav.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,5 @@
164164
**** xref:reactive/test/web/csrf.adoc[Testing CSRF]
165165
**** xref:reactive/test/web/oauth2.adoc[Testing OAuth 2.0]
166166
** xref:reactive/configuration/webflux.adoc[WebFlux Security]
167+
* xref:native-image/index.adoc[GraalVM Native Image Support]
168+
** xref:native-image/method-security.adoc[Method Security]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
= GraalVM Native Image Support
2+
3+
Spring Boot 3.0 provides https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html#native-image.introducing-graalvm-native-images[support for generating native images with GraalVM].
4+
Spring Security integrates with that support and provides its features ready for native images.
5+
6+
However, as mentioned in the https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html#native-image.introducing-graalvm-native-images.understanding-aot-processing.hint-file-generation[Spring Boot documentation], there are some cases where we need to provide hints to be used by GraalVM.
7+
8+
This section aims to provide guidance in some Spring Security features that likely need to have additional hints provided by the application.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[[native-image-method-security]]
2+
= Method Security in GraalVM Native Image
3+
4+
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.
5+
6+
== Using `@PreAuthorize` and `@PostAuthorize` Annotations
7+
8+
Using `@PreAuthorize` and `@PostAuthorize` annotations require additional hints if you have a custom implementation of `UserDetails` or `Authentication` classes.
9+
10+
Let's take an example where you have a custom implementation of `UserDetails` class as follows and that implementation is returned by your `UserDetailsService`:
11+
12+
====
13+
.Custom Implementation of UserDetails
14+
[source,java]
15+
----
16+
public class CustomUserDetails implements UserDetails {
17+
18+
private final String username;
19+
20+
private final String password;
21+
22+
private final Collection<? extends GrantedAuthority> authorities;
23+
24+
public boolean isAdmin() {
25+
return this.authorities.contains(new SimpleGrantedAuthority("ROLE_ADMIN"));
26+
}
27+
28+
// constructors, getters and setters
29+
}
30+
----
31+
====
32+
33+
And you want to use the `isAdmin()` method inside a `@PreAuthorize` annotation as follows:
34+
35+
====
36+
.Using isAdmin() to secure a method
37+
[source,java]
38+
----
39+
@PreAuthorize("principal?.isAdmin()")
40+
public String hello() {
41+
return "Hello!";
42+
}
43+
----
44+
====
45+
46+
[NOTE]
47+
====
48+
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.
49+
====
50+
51+
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:
52+
53+
[source]
54+
----
55+
failed: java.lang.IllegalArgumentException: Failed to evaluate expression 'principal?.isAdmin()' with root cause
56+
org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method isAdmin() cannot be found on type com.mypackage.CustomUserDetails
57+
----
58+
59+
Which means that the `isAdmin()` method cannot be found on the `CustomUserDetails` class.
60+
This is because Spring Security uses reflection to invoke the `isAdmin()` method and GraalVM Native Image does not support reflection by default.
61+
62+
To fix this issue, you need to give hints to GraalVM Native Image to allow reflection on the `CustomUserDetails#isAdmin()` method.
63+
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].
64+
In this example we are going to use {spring-framework-reference-url}core.html#core.aot.hints.register-reflection-for-binding[the `@RegisterReflectionForBinding` annotation].
65+
66+
[NOTE]
67+
====
68+
You might need to register all your classes that you want to use in your `@PreAuthorize` and `@PostAuthorize` annotations.
69+
====
70+
71+
====
72+
.Using @RegisterReflectionForBinding
73+
[source,java]
74+
----
75+
@Configuration
76+
@RegisterReflectionForBinding(CustomUserDetails.class)
77+
public class MyConfiguration {
78+
//...
79+
}
80+
----
81+
====
82+
83+
And that's it, now you can run the native image of your application and it should work as expected.

0 commit comments

Comments
 (0)