Skip to content

Commit 97a42ba

Browse files
committed
Update SpEL Documentation
Closes gh-12974
1 parent a0e8fc9 commit 97a42ba

File tree

9 files changed

+263
-575
lines changed

9 files changed

+263
-575
lines changed

docs/modules/ROOT/nav.adoc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
** xref:servlet/authorization/index.adoc[Authorization]
6060
*** xref:servlet/authorization/architecture.adoc[Authorization Architecture]
6161
*** xref:servlet/authorization/authorize-http-requests.adoc[Authorize HTTP Requests]
62-
*** xref:servlet/authorization/expression-based.adoc[Expression-Based Access Control]
6362
*** xref:servlet/authorization/method-security.adoc[Method Security]
6463
*** xref:servlet/authorization/acls.adoc[Domain Object Security ACLs]
6564
*** xref:servlet/authorization/events.adoc[Authorization Events]

docs/modules/ROOT/pages/features/integrations/data.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ interface MessageRepository : PagingAndSortingRepository<Message?, Long?> {
6767

6868
This checks to see if the `Authentication.getPrincipal().getId()` is equal to the recipient of the `Message`.
6969
Note that this example assumes you have customized the principal to be an Object that has an id property.
70-
By exposing the `SecurityEvaluationContextExtension` bean, all of the xref:servlet/authorization/expression-based.adoc#common-expressions[Common Security Expressions] are available within the Query.
70+
By exposing the `SecurityEvaluationContextExtension` bean, all of the xref:servlet/authorization/method-security.adoc#authorization-expressions[Common Security Expressions] are available within the Query.

docs/modules/ROOT/pages/servlet/appendix/namespace/http.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ Defaults to `true`.
163163

164164
[[nsa-http-use-expressions]]
165165
* **use-expressions**
166-
Enables EL-expressions in the `access` attribute, as described in the chapter on xref:servlet/authorization/expression-based.adoc#el-access-web[expression-based access-control].
166+
Enables EL-expressions in the `access` attribute, as described in the chapter on xref:servlet/authorization/authorize-http-requests.adoc#authorization-expressions[expression-based access-control].
167167
The default value is true.
168168

169169

docs/modules/ROOT/pages/servlet/authorization/authorize-http-requests.adoc

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,82 @@ You will notice that since we are using the `hasRole` expression we do not need
651651
<6> Any URL that has not already been matched on is denied access.
652652
This is a good strategy if you do not want to accidentally forget to update your authorization rules.
653653

654+
[[authorization-expressions]]
655+
== Expressing Authorization with SpEL
656+
657+
While using a concrete `AuthorizationManager` is recommended, there are some cases where an expression is necessary, like with `<intercept-url>` or with JSP Taglibs.
658+
For that reason, this section will focus on examples from those domains.
659+
660+
Given that, let's cover Spring Security's Web Security Authorization SpEL API a bit more in depth.
661+
662+
Spring Security encapsulates all of its authorization fields and methods in a set of root objects.
663+
The most generic root object is called `SecurityExpressionRoot` and it forms the basis for `WebSecurityExpressionRoot`.
664+
Spring Security supplies this root object to `StandardEvaluationContext` when preparing to evaluate an authorization expression.
665+
666+
[[using-authorization-expression-fields-and-methods]]
667+
=== Using Authorization Expression Fields and Methods
668+
669+
The first thing this provides is an enhanced set of authorization fields and methods to your SpEL expressions.
670+
What follows is a quick overview of the most common methods:
671+
672+
* `permitAll` - The request requires no authorization to be invoked; note that in this case, xref:servlet/authentication/architecture.adoc#servlet-authentication-authentication[the `Authentication`] is never retrieved from the session
673+
* `denyAll` - The request is not allowed under any circumstances; note that in this case, the `Authentication` is never retrieved from the session
674+
* `hasAuthority` - The request requires that the `Authentication` have xref:servlet/authorization/architecture.adoc#authz-authorities[a `GrantedAuthority`] that matches the given value
675+
* `hasRole` - A shortcut for `hasAuthority` that prefixes `ROLE_` or whatever is configured as the default prefix
676+
* `hasAnyAuthority` - The request requires that the `Authentication` have a `GrantedAuthority` that matches any of the given values
677+
* `hasAnyRole` - A shortcut for `hasAnyAuthority` that prefixes `ROLE_` or whatever is configured as the default prefix
678+
* `hasPermission` - A hook into your `PermissionEvaluator` instance for doing object-level authorization
679+
680+
And here is a brief look at the most common fields:
681+
682+
* `authentication` - The `Authentication` instance associated with this method invocation
683+
* `principal` - The `Authentication#getPrincipal` associated with this method invocation
684+
685+
Having now learned the patterns, rules, and how they can be paired together, you should be able to understand what is going on in this more complex example:
686+
687+
.Authorize Requests Using SpEL
688+
====
689+
.Xml
690+
[source,java,role="primary"]
691+
----
692+
<http>
693+
<intercept-url pattern="/static/**" access="permitAll"/> <1>
694+
<intercept-url pattern="/admin/**" access="hasRole('ADMIN')"/> <2>
695+
<intercept-url pattern="/db/**" access="hasAuthority('db') and hasRole('ADMIN')"/> <3>
696+
<intercept-url pattern="/**" access="denyAll"/> <4>
697+
</http>
698+
----
699+
====
700+
<1> We specified a URL patters that any user can access.
701+
Specifically, any user can access a request if the URL starts with "/static/".
702+
<2> Any URL that starts with "/admin/" will be restricted to users who have the role "ROLE_ADMIN".
703+
You will notice that since we are invoking the `hasRole` method we do not need to specify the "ROLE_" prefix.
704+
<3> Any URL that starts with "/db/" requires the user to have both been granted the "db" permission as well as be a "ROLE_ADMIN".
705+
You will notice that since we are using the `hasRole` expression we do not need to specify the "ROLE_" prefix.
706+
<4> Any URL that has not already been matched on is denied access.
707+
This is a good strategy if you do not want to accidentally forget to update your authorization rules.
708+
709+
[[using_path_parameters]]
710+
=== Using Path Parameters
711+
712+
Additionally, Spring Security provides a mechanism for discovering path parameters so they can also be accessed in the SpEL expression as well.
713+
714+
For example, you can access a path parameter in your SpEL expression in the following way:
715+
716+
.Authorize Request using SpEL path variable
717+
====
718+
.Xml
719+
[source,xml,role="primary"]
720+
----
721+
<http>
722+
<intercept-url pattern="/resource/{name}" access="#name == authentication.name"/>
723+
<intercept-url pattern="/**" access="authenticated"/>
724+
</http>
725+
----
726+
====
727+
728+
This expression refers to the path variable after `/resource/` and requires that it is equal to `Authentication#getName`.
729+
654730
[[remote-authorization-manager]]
655731
=== Use an Authorization Database, Policy Agent, or Other Service
656732
If you want to configure Spring Security to use a separate service for authorization, you can create your own `AuthorizationManager` and match it to `anyRequest`.

0 commit comments

Comments
 (0)