|
@@ -370,8 +370,9 @@ This will give you access to the entire project history (including all releases
|
|
|
== What's new in Spring Security 4.1
|
|
|
|
|
|
* Meta Annotation Support
|
|
|
-** <<test-method-meta-annotations>>
|
|
|
-** <<method-security-meta-annotations>>
|
|
|
+** <<test-method-meta-annotations,Test Meta Annotations>>
|
|
|
+** <<method-security-meta-annotations,Method Security Meta Annotations>>
|
|
|
+* <<el-access-web-path-variables,Path Variables in Web Security Expressions>>
|
|
|
|
|
|
=== What's new in Spring Security 4.0
|
|
|
|
|
@@ -4569,7 +4570,10 @@ The base class for expression root objects is `SecurityExpressionRoot`. This pro
|
|
|
|
|
|
[[el-access-web]]
|
|
|
=== Web Security Expressions
|
|
|
-To use expressions to secure individual URLs, you would first need to set the `use-expressions` attribute in the `<http>` element to `true`. Spring Security will then expect the `access` attributes of the `<intercept-url>` elements to contain Spring EL expressions. The expressions should evaluate to a boolean, defining whether access should be allowed or not. For example:
|
|
|
+To use expressions to secure individual URLs, you would first need to set the `use-expressions` attribute in the `<http>` element to `true`.
|
|
|
+Spring Security will then expect the `access` attributes of the `<intercept-url>` elements to contain Spring EL expressions.
|
|
|
+The expressions should evaluate to a boolean, defining whether access should be allowed or not.
|
|
|
+For example:
|
|
|
|
|
|
[source,xml]
|
|
|
----
|
|
@@ -4582,9 +4586,92 @@ To use expressions to secure individual URLs, you would first need to set the `u
|
|
|
|
|
|
----
|
|
|
|
|
|
-Here we have defined that the "admin" area of an application (defined by the URL pattern) should only be available to users who have the granted authority "admin" and whose IP address matches a local subnet. We've already seen the built-in `hasRole` expression in the previous section. The expression `hasIpAddress` is an additional built-in expression which is specific to web security. It is defined by the `WebSecurityExpressionRoot` class, an instance of which is used as the expression root object when evaluation web-access expressions. This object also directly exposed the `HttpServletRequest` object under the name `request` so you can invoke the request directly in an expressio
|
|
|
-If expressions are being used, a `WebExpressionVoter` will be added to the `AccessDecisionManager` which is used by the namespace. So if you aren't using the namespace and want to use expressions, you will have to add one of these to your configuration.
|
|
|
+Here we have defined that the "admin" area of an application (defined by the URL pattern) should only be available to users who have the granted authority "admin" and whose IP address matches a local subnet.
|
|
|
+We've already seen the built-in `hasRole` expression in the previous section.
|
|
|
+The expression `hasIpAddress` is an additional built-in expression which is specific to web security.
|
|
|
+It is defined by the `WebSecurityExpressionRoot` class, an instance of which is used as the expression root object when evaluation web-access expressions.
|
|
|
+This object also directly exposed the `HttpServletRequest` object under the name `request` so you can invoke the request directly in an expression.
|
|
|
+If expressions are being used, a `WebExpressionVoter` will be added to the `AccessDecisionManager` which is used by the namespace.
|
|
|
+So if you aren't using the namespace and want to use expressions, you will have to add one of these to your configuration.
|
|
|
+
|
|
|
+[[el-access-web-beans]]
|
|
|
+==== Referring to Beans in Web Security Expressions
|
|
|
+
|
|
|
+If you wish to extend the expressions that are available, you can easily refer to any Spring Bean you expose.
|
|
|
+For example, assumming you have a Bean with the name of `webSecurity` that contains the following method signature:
|
|
|
+
|
|
|
+[source,java]
|
|
|
+----
|
|
|
+public class WebSecurity {
|
|
|
+ public boolean check(Authentication authentication, HttpServletRequest request) {
|
|
|
+ ...
|
|
|
+ }
|
|
|
+}
|
|
|
+----
|
|
|
+
|
|
|
+You could refer to the method using:
|
|
|
+
|
|
|
+[source,xml]
|
|
|
+----
|
|
|
+<http>
|
|
|
+ <intercept-url pattern="/user/**"
|
|
|
+ access="@webSecurity.check(authentication,request)"/>
|
|
|
+ ...
|
|
|
+</http>
|
|
|
+----
|
|
|
+
|
|
|
+or in Java configuration
|
|
|
+
|
|
|
+
|
|
|
+[source,java]
|
|
|
+----
|
|
|
+http
|
|
|
+ .authorizeUrls()
|
|
|
+ .antMatchers("/user/**").access("@webSecurity.check(authentication,request)")
|
|
|
+ ...
|
|
|
+----
|
|
|
+
|
|
|
+[[el-access-web-path-variables]]
|
|
|
+==== Path Variables in Web Security Expressions
|
|
|
+
|
|
|
+At times it is nice to be able to refer to path variables within a URL.
|
|
|
+For example, consider a RESTful application that looks up a user by id from the URL path in the format `/user/{userId}`.
|
|
|
+
|
|
|
+You can easily refer to the path variable by placing it in the pattern.
|
|
|
+For example, if you had a Bean with the name of `webSecurity` that contains the following method signature:
|
|
|
+
|
|
|
+[source,java]
|
|
|
+----
|
|
|
+public class WebSecurity {
|
|
|
+ public boolean checkUserId(Authentication authentication, int id) {
|
|
|
+ ...
|
|
|
+ }
|
|
|
+}
|
|
|
+----
|
|
|
+
|
|
|
+You could refer to the method using:
|
|
|
+
|
|
|
+[source,xml]
|
|
|
+----
|
|
|
+<http>
|
|
|
+ <intercept-url pattern="/user/{userId}/**"
|
|
|
+ access="@webSecurity.checkUserId(authentication,userId)"/>
|
|
|
+ ...
|
|
|
+</http>
|
|
|
+----
|
|
|
+
|
|
|
+or in Java configuration
|
|
|
+
|
|
|
+[source,java]
|
|
|
+----
|
|
|
+http
|
|
|
+ .authorizeUrls()
|
|
|
+ .antMatchers("/user/{userId}/**").access("@webSecurity.checkUserId(authentication,userId)")
|
|
|
+ ...
|
|
|
+----
|
|
|
|
|
|
+In both configurations URLs that match would pass in the path variable (and convert it) into checkUserId method.
|
|
|
+For example, if the URL were `/user/123/resource`, then the id passed in would be `123`.
|
|
|
|
|
|
=== Method Security Expressions
|
|
|
Method security is a bit more complicated than a simple allow or deny rule. Spring Security 3.0 introduced some new annotations in order to allow comprehensive support for the use of expressions.
|