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/expression-based.adoc
+115-9Lines changed: 115 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -114,7 +114,9 @@ So if you aren't using the namespace and want to use expressions, you will have
114
114
If you wish to extend the expressions that are available, you can easily refer to any Spring Bean you expose.
115
115
For example, assuming you have a Bean with the name of `webSecurity` that contains the following method signature:
116
116
117
-
[source,java]
117
+
====
118
+
.Java
119
+
[source,java,role="primary"]
118
120
----
119
121
public class WebSecurity {
120
122
public boolean check(Authentication authentication, HttpServletRequest request) {
@@ -123,6 +125,17 @@ public class WebSecurity {
123
125
}
124
126
----
125
127
128
+
.Kotlin
129
+
[source,kotlin,role="secondary"]
130
+
----
131
+
class WebSecurity {
132
+
fun check(authentication: Authentication?, request: HttpServletRequest?): Boolean {
133
+
// ...
134
+
}
135
+
}
136
+
----
137
+
====
138
+
126
139
You could refer to the method using:
127
140
128
141
.Refer to method
@@ -167,7 +180,9 @@ For example, consider a RESTful application that looks up a user by id from the
167
180
You can easily refer to the path variable by placing it in the pattern.
168
181
For example, if you had a Bean with the name of `webSecurity` that contains the following method signature:
169
182
170
-
[source,java]
183
+
====
184
+
.Java
185
+
[source,java,role="primary"]
171
186
----
172
187
public class WebSecurity {
173
188
public boolean checkUserId(Authentication authentication, int id) {
@@ -176,6 +191,17 @@ public class WebSecurity {
176
191
}
177
192
----
178
193
194
+
.Kotlin
195
+
[source,kotlin,role="secondary"]
196
+
----
197
+
class WebSecurity {
198
+
fun checkUserId(authentication: Authentication?, id: Int): Boolean {
199
+
// ...
200
+
}
201
+
}
202
+
----
203
+
====
204
+
179
205
You could refer to the method using:
180
206
181
207
.Path Variables
@@ -234,22 +260,42 @@ Their use is enabled through the `global-method-security` namespace element:
234
260
The most obviously useful annotation is `@PreAuthorize` which decides whether a method can actually be invoked or not.
235
261
For example (from the "Contacts" sample application)
236
262
237
-
[source,java]
263
+
====
264
+
.Java
265
+
[source,java,role="primary"]
238
266
----
239
267
@PreAuthorize("hasRole('USER')")
240
268
public void create(Contact contact);
241
269
----
242
270
271
+
.Kotlin
272
+
[source,kotlin,role="secondary"]
273
+
----
274
+
@PreAuthorize("hasRole('USER')")
275
+
fun create(contact: Contact?)
276
+
----
277
+
====
278
+
243
279
which means that access will only be allowed for users with the role "ROLE_USER".
244
280
Obviously the same thing could easily be achieved using a traditional configuration and a simple configuration attribute for the required role.
245
281
But what about:
246
282
247
-
[source,java]
283
+
====
284
+
.Java
285
+
[source,java,role="primary"]
248
286
----
249
287
@PreAuthorize("hasPermission(#contact, 'admin')")
250
288
public void deletePermission(Contact contact, Sid recipient, Permission permission);
251
289
----
252
290
291
+
.Kotlin
292
+
[source,kotlin,role="secondary"]
293
+
----
294
+
@PreAuthorize("hasPermission(#contact, 'admin')")
295
+
fun deletePermission(contact: Contact?, recipient: Sid?, permission: Permission?)
296
+
----
297
+
====
298
+
253
299
Here we're actually using a method argument as part of the expression to decide whether the current user has the "admin"permission for the given contact.
254
300
The built-in `hasPermission()` expression is linked into the Spring Security ACL module through the application context, as we'll <<el-permission-evaluator,see below>>.
255
301
You can access any of the method arguments by name as expression variables.
Behind the scenes this is implemented using `AnnotationParameterNameDiscoverer` which can be customized to support the value attribute of any specified annotation.
fun findContactByName(@Param("n") name: String?): Contact?
368
+
----
369
+
====
370
+
297
371
+
298
372
299
373
Behind the scenes this is implemented using `AnnotationParameterNameDiscoverer` which can be customized to support the value attribute of any specified annotation.
@@ -311,12 +385,22 @@ Any Spring-EL functionality is available within the expression, so you can also
311
385
For example, if you wanted a particular method to only allow access to a user whose username matched that of the contact, you could write
Here we are accessing another built-in expression, `authentication`, which is the `Authentication` stored in the security context.
321
405
You can also access its "principal" property directly, using the expression `principal`.
322
406
The value will often be a `UserDetails` instance, so you might use an expression like `principal.username` or `principal.enabled`.
@@ -333,13 +417,24 @@ Spring Security supports filtering of collections, arrays, maps and streams usin
333
417
This is most commonly performed on the return value of a method.
334
418
For example:
335
419
336
-
[source,java]
420
+
====
421
+
.Java
422
+
[source,java,role="primary"]
337
423
----
338
424
@PreAuthorize("hasRole('USER')")
339
425
@PostFilter("hasPermission(filterObject, 'read') or hasPermission(filterObject, 'admin')")
340
426
public List<Contact> getAll();
341
427
----
342
428
429
+
.Kotlin
430
+
[source,kotlin,role="secondary"]
431
+
----
432
+
@PreAuthorize("hasRole('USER')")
433
+
@PostFilter("hasPermission(filterObject, 'read') or hasPermission(filterObject, 'admin')")
434
+
fun getAll(): List<Contact?>
435
+
----
436
+
====
437
+
343
438
When using the `@PostFilter` annotation, Spring Security iterates through the returned collection or map and removes any elements for which the supplied expression is false.
344
439
For an array, a new array instance will be returned containing filtered elements.
345
440
The name `filterObject` refers to the current object in the collection.
@@ -412,13 +507,24 @@ For example, consider the following:
412
507
413
508
Instead of repeating this everywhere, we can create a meta annotation that can be used instead.
0 commit comments