Skip to content

Commit f6731e8

Browse files
committed
Polish Method Security Preparation Steps
1 parent 04fa5af commit f6731e8

File tree

1 file changed

+81
-24
lines changed

1 file changed

+81
-24
lines changed

docs/modules/ROOT/pages/migration.adoc

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ Use 5.8 and the steps below to minimize changes when updating to 6.0.
66

77
== Servlet
88

9-
=== Change `@EnableGlobalMethodSecurity` to `@EnableMethodSecurity`
9+
=== Use `AuthorizationManager` for Method Security
1010

1111
xref:servlet/authorization/method-security.adoc[Method Security] has been xref:servlet/authorization/method-security.adoc#jc-enable-method-security[simplified] through {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[the `AuthorizationManager` API] and direct use of Spring AOP.
1212

13-
The public API difference between these two annotations is that {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableMethodSecurity.html[`@EnableMethodSecurity`] defaults `prePostEnabled` to `true`, while {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableGlobalMethodSecurity.html[`@EnableGlobalMethodSecurity`] defaults it to `false`.
14-
Also, `@EnableMethodSecurity` internally uses `AuthorizationManager` while `@EnableGlobalMethodSecurity` does not.
13+
'''
14+
15+
[[servlet-replace-globalmethodsecurity-with-methodsecurity]]
16+
[%interactive]
17+
* [ ] Replace xref:servlet/authorization/method-security.adoc#jc-enable-global-method-security[global method security] with xref:servlet/authorization/method-security.adoc#jc-enable-method-security[method security]
18+
19+
{security-api-url}org/springframework/security/config/annotation/method/configuration/EnableGlobalMethodSecurity.html[`@EnableGlobalMethodSecurity`] and xref:servlet/appendix/namespace/method-security.adoc#nsa-global-method-security[`<global-method-security>`] are deprecated in favor of {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableMethodSecurity.html[`@EnableMethodSecurity`] and xref:servlet/appendix/namespace/method-security.adoc#nsa-method-security[`<method-security>`], respectively.
20+
The new annotation and XML element activate Spring's xref:servlet/authorization/method-security.adoc#jc-enable-method-security[pre-post annotations] by default and use `AuthorizationManager` internally.
1521

1622
This means that the following two listings are functionally equivalent:
1723

@@ -27,9 +33,15 @@ This means that the following two listings are functionally equivalent:
2733
----
2834
@EnableGlobalMethodSecurity(prePostEnabled = true)
2935
----
36+
37+
.Xml
38+
[source,xml,role="secondary"]
39+
----
40+
<global-method-security pre-post-enabled="true"/>
41+
----
3042
====
3143

32-
changes to:
44+
and:
3345

3446
====
3547
.Java
@@ -43,9 +55,15 @@ changes to:
4355
----
4456
@EnableMethodSecurity
4557
----
58+
59+
.Xml
60+
[source,xml,role="secondary"]
61+
----
62+
<method-security/>
63+
----
4664
====
4765

48-
For applications not using `prePostEnabled`, make sure to turn it off to avoid activating unwanted behavior.
66+
For applications not using the pre-post annotations, make sure to turn it off to avoid activating unwanted behavior.
4967

5068
For example, a listing like:
5169

@@ -61,6 +79,12 @@ For example, a listing like:
6179
----
6280
@EnableGlobalMethodSecurity(securedEnabled = true)
6381
----
82+
83+
.Xml
84+
[source,xml,role="secondary"]
85+
----
86+
<global-method-security secured-enabled="true"/>
87+
----
6488
====
6589

6690
should change to:
@@ -77,15 +101,22 @@ should change to:
77101
----
78102
@EnableMethodSecurity(securedEnabled = true, prePostEnabled = false)
79103
----
104+
105+
.Xml
106+
[source,xml,role="secondary"]
107+
----
108+
<method-security secured-enabled="true" pre-post-enabled="false"/>
109+
----
80110
====
81111

82-
Additionally, note that `@EnableMethodSecurity` activates stricter enforcement of Spring Security's non-repeatable or otherwise incompatible annotations.
83-
If after moving to `@EnableMethodSecurity` you see ``AnnotationConfigurationException``s in your logs, follow the instructions in the exception message to clean up your application's method security annotation usage.
112+
'''
84113

85-
==== Publish your custom `PermissionEvaluator` as a `MethodSecurityExpressionHandler`
114+
[[servlet-replace-permissionevaluator-bean-with-methodsecurityexpression-handler]]
115+
[%interactive]
116+
* [ ] Publish a `MethodSecurityExpressionHandler` instead of a `PermissionEvaluator`
86117

87-
`@EnableMethodSecurity` does not pick up a `PermissionEvaluator` bean.
88-
Instead, it picks up the more generic `MethodSecurityExpressionHandler` to simplify the API.
118+
`@EnableMethodSecurity` does not pick up a `PermissionEvaluator`.
119+
This helps keep its API simple.
89120

90121
If you have a custom {security-api-url}org/springframework/security/access/PermissionEvaluator.html[`PermissionEvaluator`] `@Bean`, please change it from:
91122

@@ -94,17 +125,19 @@ If you have a custom {security-api-url}org/springframework/security/access/Permi
94125
[source,java,role="primary"]
95126
----
96127
@Bean
97-
PermissionEvaluator permissionEvaluator() {
128+
static PermissionEvaluator permissionEvaluator() {
98129
// ... your evaluator
99130
}
100131
----
101132
102133
.Kotlin
103134
[source,kotlin,role="secondary"]
104135
----
105-
@Bean
106-
fun permissionEvaluator(): PermissionEvaluator {
107-
// ... your evaluator
136+
companion object {
137+
@Bean
138+
fun permissionEvaluator(): PermissionEvaluator {
139+
// ... your evaluator
140+
}
108141
}
109142
----
110143
====
@@ -116,7 +149,7 @@ to:
116149
[source,java,role="primary"]
117150
----
118151
@Bean
119-
MethodSecurityExpressionHandler expressionHandler() {
152+
static MethodSecurityExpressionHandler expressionHandler() {
120153
var expressionHandler = new DefaultMethodSecurityExpressionHandler();
121154
expressionHandler.setPermissionEvaluator(myPermissionEvaluator);
122155
return expressionHandler;
@@ -126,21 +159,38 @@ MethodSecurityExpressionHandler expressionHandler() {
126159
.Kotlin
127160
[source,kotlin,role="secondary"]
128161
----
129-
@Bean
130-
fun expressionHandler(): MethodSecurityExpressionHandler {
131-
val expressionHandler = DefaultMethodSecurityExpressionHandler
132-
expressionHandler.setPermissionEvaluator(myPermissionEvaluator)
133-
return expressionHandler
162+
companion object {
163+
@Bean
164+
fun expressionHandler(): MethodSecurityExpressionHandler {
165+
val expressionHandler = DefaultMethodSecurityExpressionHandler
166+
expressionHandler.setPermissionEvaluator(myPermissionEvaluator)
167+
return expressionHandler
168+
}
134169
}
135170
----
136171
====
137172

173+
'''
174+
175+
[[servlet-check-for-annotationconfigurationexceptions]]
176+
[%interactive]
177+
* [ ] Check for ``AnnotationConfigurationException``s
178+
179+
`@EnableMethodSecurity` and `<method-security>` activate stricter enforcement of Spring Security's non-repeatable or otherwise incompatible annotations.
180+
If after moving to either you see ``AnnotationConfigurationException``s in your logs, follow the instructions in the exception message to clean up your application's method security annotation usage.
181+
138182
== Reactive
139183

140-
=== Activate `AuthorizationManager` in `@EnableReactiveMethodSecurity`
184+
=== Use `AuthorizationManager` for Method Security
141185

142186
xref:reactive/authorization/method.adoc[Method Security] has been xref:reactive/authorization/method.adoc#jc-enable-reactive-method-security-authorization-manager[improved] through {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[the `AuthorizationManager` API] and direct use of Spring AOP.
143187

188+
'''
189+
190+
[[reactive-change-to-useauthorizationmanager]]
191+
[%interactive]
192+
* [ ] Change `useAuthorizationManager` to `true`
193+
144194
In Spring Security 5.8, `useAuthorizationManager` was added to {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableReactiveMethodSecurity.html[`@EnableReactiveMethodSecurity`] to allow applications to opt-in to ``AuthorizationManager``'s features.
145195

146196
To opt in, change `useAuthorizationManager` to `true` like so:
@@ -175,9 +225,16 @@ changes to:
175225
----
176226
====
177227

178-
Note that in 6.0, `useAuthorizationManager` defaults to `true`.
228+
[NOTE]
229+
=====
230+
In 6.0, `useAuthorizationManager` defaults to `true`.
231+
=====
179232

180-
Additionally, note that `useAuthorizationManager` activates stricter enforcement of Spring Security's non-repeatable or otherwise incompatible annotations.
181-
If after turning on `useAuthorizationManager` you see ``AnnotationConfigurationException``s in your logs, follow the instructions in the exception message to clean up your application's method security annotation usage.
233+
'''
182234

235+
[[reactive-check-for-annotationconfigurationexceptions]]
236+
[%interactive]
237+
* [ ] Check for ``AnnotationConfigurationException``s
183238

239+
`useAuthorizationManager` activates stricter enforcement of Spring Security's non-repeatable or otherwise incompatible annotations.
240+
If after turning on `useAuthorizationManager` you see ``AnnotationConfigurationException``s in your logs, follow the instructions in the exception message to clean up your application's method security annotation usage.

0 commit comments

Comments
 (0)