Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 9e41b0b

Browse files
eleftheriassdeleuze
authored andcommitted
Improve SpEL support in method security annotations
Support use of filterObject and returnObject in PreFilter and PostFilter annotations. Closes gh-1518
1 parent 5bc60e7 commit 9e41b0b

File tree

6 files changed

+39
-2
lines changed

6 files changed

+39
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package com.example.methodsecurity;
22

3+
import java.util.List;
4+
35
import org.springframework.security.access.prepost.PreAuthorize;
6+
import org.springframework.security.access.prepost.PreFilter;
47

58
public interface GreetingService {
69

710
String hello();
811

912
@PreAuthorize("hasRole('ADMIN')")
1013
String adminHello();
14+
15+
@PreFilter("filterObject == 'Hello'")
16+
String echo(List<String> greetings);
1117
}

samples/security-method/src/main/java/com/example/methodsecurity/GreetingServiceImpl.java

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.example.methodsecurity;
22

3+
import java.util.List;
4+
35
import org.springframework.stereotype.Service;
46

57
@Service
@@ -14,4 +16,9 @@ public String hello() {
1416
public String adminHello() {
1517
return "Goodbye!";
1618
}
19+
20+
@Override
21+
public String echo(List<String> greetings) {
22+
return String.join(",", greetings);
23+
}
1724
}

samples/security-method/src/main/java/com/example/methodsecurity/MainController.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.example.methodsecurity;
22

3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
37
import org.springframework.security.access.prepost.PreAuthorize;
48
import org.springframework.web.bind.annotation.GetMapping;
59
import org.springframework.web.bind.annotation.RestController;
@@ -22,5 +26,13 @@ public String hello() {
2226
public String adminHello() {
2327
return greetingService.adminHello();
2428
}
25-
29+
30+
@GetMapping("/filter/hello")
31+
public String helloFilter() {
32+
List<String> greetings = new ArrayList<>();
33+
greetings.add("Hello");
34+
greetings.add("Goodbye");
35+
return greetingService.echo(greetings);
36+
}
37+
2638
}

samples/security-method/src/test/java/com/example/methodsecurity/MethodSecurityApplicationTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.springframework.test.web.servlet.MockMvc;
1010

1111
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
1213
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1314

1415
@SpringBootTest
@@ -43,4 +44,12 @@ public void accessAdminPageAsAdminThenOk() throws Exception {
4344
mockMvc.perform(get("/admin/hello"))
4445
.andExpect(status().isOk());
4546
}
47+
48+
@Test
49+
@WithMockUser
50+
public void accessHelloFilterThenOk() throws Exception {
51+
mockMvc.perform(get("/filter/hello"))
52+
.andExpect(status().isOk())
53+
.andExpect(content().string("Hello"));
54+
}
4655
}

samples/security-method/verify.sh

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ wait_command_output "curl -I localhost:8080/admin/private" "HTTP/1.1 401" || RC=
88
wait_http user:password@localhost:8080/hello "Hello!" || RC=$?
99
wait_http admin:password@localhost:8080/admin/hello "Goodbye!" || RC=$?
1010
wait_http admin:password@localhost:8080/admin/private "bye" || RC=$?
11+
wait_http user:password@localhost:8080/filter/hello "Hello" || RC=$?
1112

1213
exit $RC

spring-native-configuration/src/main/java/org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityHints.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import org.springframework.nativex.type.NativeConfiguration;
2424

2525
@NativeHint(trigger=GlobalMethodSecurityConfiguration.class, types = {
26-
@TypeHint(typeNames= "org.springframework.security.access.expression.method.MethodSecurityExpressionRoot", access = TypeAccess.DECLARED_CONSTRUCTORS),
26+
@TypeHint(
27+
typeNames= "org.springframework.security.access.expression.method.MethodSecurityExpressionRoot",
28+
access = {TypeAccess.DECLARED_CONSTRUCTORS, TypeAccess.DECLARED_METHODS, TypeAccess.DECLARED_FIELDS}),
2729
})
2830
@NativeHint(jdkProxies = @JdkProxyHint(types = {
2931
org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication.class,

0 commit comments

Comments
 (0)