Previously in the tutorial on dynamic permission control, we implemented dynamic permission control by customizing FilterInvocationSecurityMetadataSource
and AccessDecisionManager
two interfaces. There are more things we need to do here, and there is a certain learning cost. Today to introduce a more simple and easy to understand approach to implement dynamic permission control.
Expression-based access control
Needless to say, after we configure the expression hasRole('admin')
, Spring Security will call the hasRole(String role)
method of SecurityExpressionRoot
to determine if the current user holds the role admin
and thus make a decision on whether to release or not. This approach allows for dynamic access control in addition to static access control.
Bean-based access control expressions
Spring Security extends expressions to support references to any public Spring bean. Suppose we have a Spring Bean that implements the following interface :
|
|
JDBC-based role checking, preferably using caching here.
|
|
We can then configure HttpSecurity
like this.
With Authentication
in RoleChecker
we can get the information about the current user, especially the permission set. With HttpServletRequest
we can get the URI of the current request. This URI intersects the permission set in the system with the user’s permission set to make the correct access decision.
Path parameters
Sometimes our access URI also contains a path parameter, such as /foo/{id}
. We can also control this with a bean-based access control expression combined with a specific id
value. This would be written like this.
|
|
The corresponding configurations are as follows.
So that when the /foo/123
request is intercepted, 123
will be assigned to the id
handler in the check
method.
Summary
This expression for dynamic permission control is much easier to grasp and understand than the previous approach. But it also has its limitations, such as the expression has a single parameter type in the method. The FilterInvocationSecurityMetadataSource
approach is more powerful and can customize some access decisions for more complex scenarios.
Reference
https://felord.cn/easy-dyn-acl-spring-security.html