Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

antMatchers for a list of paths

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

like image 349
Nitin1706 Avatar asked Mar 04 '23 22:03

Nitin1706


1 Answers

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)
...
like image 99
chaoluo Avatar answered Mar 27 '23 12:03

chaoluo