gpt4 book ai didi

java - 如何以编程方式检索所有 antmatcher URL

转载 作者:行者123 更新时间:2023-11-30 05:40:38 24 4
gpt4 key购买 nike

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

我有一个服务,可以查找应用程序中的所有端点并收集有关它们的一些基本信息,这是通过 Autowiring RequestMappingHandlerMapping 类并迭代不同的处理程序方法来实现的。

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

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

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

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

最佳答案

我的帖子没有直接回答这个问题,因为我不知道如何做到这一点。然而,有一个解决方法,这个想法值得尝试一下:

我建议您拥有映射源并动态设置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
}

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

关于java - 如何以编程方式检索所有 antmatcher URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55728522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com