Spanner0jjm
Spanner0jjm

Reputation: 331

How to programatically retrieve all ant matcher URLs

I have a basic Spring application that has various endpoints, and a login page, defined by configuring the HttpSecurity of the WebSecurityConfigurerAdapter.

I have a service that looks for all of the endpoints within my aplication and collects some basic information about them, which I acheived by autowiring the RequestMappingHandlerMapping class and iterating through the different handler methods.

I would like to collect some similar information for the paths that are defined by the WebSecurityCongigurer adapter.

For example, within the configue method, if I have, say:

http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and().authorizeRequests()
                 .antMatchers(HttpMethod.POST, "/login").permitAll()

From within the service I would like to collect the information: "/login" and HttpMethod.POST. Any help would be greatly appreciated!

Upvotes: 1

Views: 162

Answers (1)

Nikolas
Nikolas

Reputation: 44476

My post doesn't directly answer the question since I am not aware of a way to do that. However, there is a workaround and this idea deserves a chance:

I recommend you to have a source of mappings and set antMatchers dynamically. This solution both configures the adapter and leaves the source of mappings available for further use (I recommend keeping the values themselves immutable).

List<MatcherMapping> mappings = ....
for (MatcherMapping mapping: mappings) {
    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and().authorizeRequests()
              .antMatchers(mapping.getHttpMethod(), mapping.getUrl())
              .permitAll()
}

The class MatcherMapping would be just a simple data container.

public final class MatcherMapping {

    private final HttpMethod httpMethod;
    private final String url;

    // constructor and getters
}

Whether you get the data using a service or getting them directly doesn't matter is finally up to you.

Upvotes: 1

Related Questions