You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/manual/src/docs/asciidoc/_includes/servlet/authorization/method-security.adoc
+163Lines changed: 163 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,169 @@ It provides support for JSR-250 annotation security as well as the framework's o
6
6
From 3.0 you can also make use of new <<el-access,expression-based annotations>>.
7
7
You can apply security to a single bean, using the `intercept-methods` element to decorate the bean declaration, or you can secure multiple beans across the entire service layer using the AspectJ style pointcuts.
8
8
9
+
=== EnableMethodSecurity
10
+
11
+
In 5.5, we can enable annotation-based security using the `@EnableMethodSecurity` annotation on any `@Configuration` instance.
12
+
13
+
[NOTE]
14
+
For earlier versions, please read about similar support with <<jc-enable-global-method-security, @EnableGlobalMethodSecurity>>.
15
+
16
+
For example, the following would enable Spring Security's `@PreAuthorize` annotation:
17
+
18
+
[source,java]
19
+
----
20
+
@EnableMethodSecurity
21
+
public class MethodSecurityConfig {
22
+
// ...
23
+
}
24
+
----
25
+
26
+
Adding an annotation to a method (on a class or interface) would then limit the access to that method accordingly.
27
+
Spring Security's native annotatino support defines a set of attributes for the method.
28
+
These will be passed to the `AuthorizationMethodInterceptor` for it to make the actual decision:
29
+
30
+
[source,java]
31
+
----
32
+
public interface BankService {
33
+
34
+
@PreAuthorize("hasRole('USER')")
35
+
Account readAccount(Long id);
36
+
37
+
@PreAuthorize("hasRole('USER')")
38
+
Account[] findAccounts();
39
+
40
+
@PreAuthorize("hasRole('TELLER')")
41
+
Account post(Account account, double amount);
42
+
}
43
+
----
44
+
45
+
You can enable support for Spring Security's `@Secured` annotation using:
46
+
47
+
[source,java]
48
+
----
49
+
@EnableMethodSecurity(secureEnabled = true)
50
+
public class MethodSecurityConfig {
51
+
// ...
52
+
}
53
+
----
54
+
55
+
or JSR-250 using:
56
+
57
+
[source,java]
58
+
----
59
+
@EnableMethodSecurity(jsr250Enabled = true)
60
+
public class MethodSecurityConfig {
61
+
// ...
62
+
}
63
+
----
64
+
65
+
==== Customizing Authorization
66
+
67
+
Spring Security's `@PreAuthorize`, `@PostAuthorize`, `@PreFilter`, and `@PostFilter` ship with rich expression-based support.
68
+
69
+
If you need to customize the way that expressions are handled, you can expose a custom `MethodSecurityExpressionHandler`, like so:
Method authorization is a combination of before- and after-method authorization.
96
+
97
+
[NOTE]
98
+
Before-method authorization is performed before the method is invoked.
99
+
If that authorization denies access, the method is not invoked and an `AccessDeniedException` is thrown
100
+
After-method authorization is performed after the method is invoked, but before the method returns to the caller.
101
+
If that authorization denies access, the value is not returned and an `AccessDeniedException` is thrown
102
+
103
+
You can customize before-method authorization by publishing your own `AuthorizationMethodBeforeAdvice` bean, which includes your custom authorization manager as well as the `Pointcut` that describes when your manager should be used.
104
+
105
+
For example, you may want to apply a default authorization rule to all methods in your service layer.
106
+
To do this, you'll supply the pointcut as well as the rule, like so:
107
+
108
+
[source,java]
109
+
----
110
+
@Bean
111
+
public AuthorizationMethodBeforeAdvice<MethodAuthorizationContext> authorizationMethodBeforeAdvice() {
112
+
JdkRegexpMethodPointcut pattern = new JdkRegexpMethodPointcut();
0 commit comments