首页 > 解决方案 > 如何以编程方式检索所有蚂蚁匹配器 URL

问题描述

我有一个基本的 Spring 应用程序,它具有各种端点和一个登录页面,通过配置 WebSecurityConfigurerAdapter 的 HttpSecurity 来定义。

我有一个服务,它在我的应用程序中查找所有端点并收集有关它们的一些基本信息,我通过自动装配 RequestMappingHandlerMapping 类并遍历不同的处理程序方法来实现这些信息。

我想为 WebSecurityConfigurer 适配器定义的路径收集一些类似的信息。

例如,在配置方法中,如果我有,请说:

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

我想从服务中收集信息:"/login"HttpMethod.POST。任何帮助将不胜感激!

标签: javaspringspring-security

解决方案


我的帖子没有直接回答这个问题,因为我不知道有办法做到这一点。但是,有一个解决方法,这个想法值得一个机会:

我建议你有一个映射源并antMatchers动态设置。该解决方案既配置了适配器,又保留了映射源以供进一步使用(我建议保持值本身不可变)。

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

该类MatcherMapping将只是一个简单的数据容器。

public final class MatcherMapping {

    private final HttpMethod httpMethod;
    private final String url;

    // constructor and getters
}

无论您是使用服务获取数据还是直接获取数据,最终都取决于您。


推荐阅读