SpringBoot 获取 HttpSecurity authorizeHttpRequests 放行 URL 和 Method
可以通过以下方式获取 authorizeHttpRequests 中放行的所有 URL 和 Method:
- 在 SpringBoot 配置类中注入
HttpSecurity对象:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 配置 authorizeHttpRequests
}
}
- 在该类中添加一个方法,利用
HttpSecurity对象获取所有放行的 URL 和 Method:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 配置 authorizeHttpRequests
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
// 全局用户配置
}
@Autowired
public void getAuthorizedUrls(HttpSecurity http) throws Exception {
List<Filter> filters = http.getSharedObject(FilterChainProxy.class).getFilters();
for (Filter filter : filters) {
if (filter instanceof FilterSecurityInterceptor) {
FilterSecurityInterceptor filterSecurityInterceptor = (FilterSecurityInterceptor) filter;
List<ConfigAttribute> configAttributes = filterSecurityInterceptor.getSecurityMetadataSource().getAllConfigAttributes();
for (ConfigAttribute configAttribute : configAttributes) {
String url = ((org.springframework.security.access.SecurityConfig) configAttribute).getAttribute();
HttpMethod method = filterSecurityInterceptor.getObserveOncePerRequestFilter().getProtectedHttpMethod(url);
System.out.println('url: ' + url + ', method: ' + method);
}
}
}
}
}
通过以上方式,就可以获取到所有放行的 URL 和 Method。其中,URL 是 String 类型,Method 是 HttpMethod 类型。
原文地址: https://www.cveoy.top/t/topic/nbia 著作权归作者所有。请勿转载和采集!