After browsing the official document of Spring Security, I found there is no such a solution to help with dynamically configure url authorization. By default, Spring Security will initialize the url authorization as the applilcation starts.
There is a question in the document 44.4.6. How do I define the secured URLs within an application dynamically?. After reading the solution to this problem, I draw a solution with two steps:
- Provide data which describes the rules of access.
SecurityMetadataSource
obtains the metadata for a particular method or filter invocation - Customize an interceptor and add it into the Spring Security’s filterChain.
Authorization Datasource
1 | /** |
Customize Dynamically Url Interceptor
1 | /** |
Decision Manager
1 | /** |
Spring Security provided several authorization strategy, here we are using RoleVoter.
Configure filter
1 | //filter |
Add filter into Spring Security’s Filter
1 | http |