I try to implement spring security, but I have many roles and privileges, then I want to add the roles dynamically to each other resources. Like it:
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
//PageRequest p = new PageRequest(0, 1000);
List<RolePrivilegeConfig> rolePrivilegesConfig=rolePrivilegeConfigService.findAll();
for (RolePrivilegeConfig rolePrivilegeConfig : rolePrivilegesConfig) {
http.authorizeRequests()
.antMatchers("/login").permitAll()
.antMatchers(rolePrivilegeConfig.getResource())
.access(rolePrivilegeConfig.getRoleName())
.anyRequest().authenticated();
} }
I have this error:
Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalStateException: Can't configure antMatchers after anyRequest.
How can I put all matchers and call request after?
For adding a Spring Boot Security to your Spring Boot application, we need to add the Spring Boot Starter Security dependency in our build configuration file. Maven users can add the following dependency in the pom. xml file. Gradle users can add the following dependency in the build.
The antMatchers() is a Springboot HTTP method used to configure the URL paths from which the Springboot application security should permit requests based on the user's roles. The antmatchers() method is an overloaded method that receives both the HTTP request methods and the specific URLs as its arguments.
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
//PageRequest p = new PageRequest(0, 1000);
List<RolePrivilegeConfig> rolePrivilegesConfig=rolePrivilegeConfigService.findAll();
http.authorizeRequests()
.antMatchers("/login").permitAll();
for (RolePrivilegeConfig rolePrivilegeConfig : rolePrivilegesConfig) {
http.authorizeRequests()
.antMatchers(rolePrivilegeConfig.getResource())
.access(rolePrivilegeConfig.getRoleName());
}
http.authorizeRequests()
.anyRequest().authenticated();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With