I have a list of paths which I want to provide to antMatchers()
. But since this list is dynamic, I cannot provide paths comma separated. I am currently looping through the list and adding eachPath to antMatcher()
. Is there a better way to do this where I can apply antMatcher to a list of paths?
Current Code snippet:
http.authorizeRequests()
.antMatchers("/signup","/about")
.hasRole("ADMIN")
.anyRequest().authenticated();
and I have a list of paths. For example:
List<String> pathList = Arrays.asList(new String[]{"/signup","/about"});
and I want to apply the antMatchers something like:
http.authorizeRequests()
.antMatchers(pathList)
.hasRole("ADMIN")
.anyRequest().authenticated();
Reference: https://spring.io/blog/2013/07/11/spring-security-java-config-preview-readability/ spring security http antMatcher with multiple paths
The antMatchers accepts string array.
public C antMatchers(String... antPatterns) {
....
}
You can convert the list to string array, and then pass it to the antMatchers
,
String[] pathArray = new String[]{"/signup","/about"};
http.authorizeRequests()
.antMatchers(pathArray)
...
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