可以通过以下代码获取authorizeHttpRequests中放行的所有url和method:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Autowired
    private HttpSecurity httpSecurity;
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests()
                .antMatchers("/public/**").permitAll()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .anyRequest().authenticated();
    }
    
    @PostConstruct
    public void init() {
        Map<RequestMatcher, Collection<ConfigAttribute>> requestMap = httpSecurity.getSharedObject(RequestMatcher.class);
        for (Map.Entry<RequestMatcher, Collection<ConfigAttribute>> entry : requestMap.entrySet()) {
            RequestMatcher requestMatcher = entry.getKey();
            Collection<ConfigAttribute> configAttributes = entry.getValue();
            System.out.println(requestMatcher.toString());
            for (ConfigAttribute configAttribute : configAttributes) {
                System.out.println(configAttribute.toString());
            }
        }
    }
}

这里通过httpSecurity.getSharedObject(RequestMatcher.class)获取所有的RequestMatcher和对应的ConfigAttribute集合,然后遍历输出即可。

SpringBoot 获取httpsecuity配置authorizeHttpRequests中的放行的所有url和method

原文地址: https://www.cveoy.top/t/topic/bHZz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录