【发布时间】:2021-08-08 20:45:33
【问题描述】:
您好,我们正在构建自定义 Spring 安全库
我们需要将 {"/v1","/v2"} 路径通过主项目中存在的 @EnableMySpringSecurity(excludePaths = {"/v1","/v2"}) 传递到库 websecurity,以便我们可以从安全性中忽略这些端点
@EnableMySpringSecurity(excludePaths = {"/v1","/v2"})
@EnableWebMvc
public class WebAppConfiguration extends BaseWebAppConfiguration {
来自自定义 JAR 的网络安全配置
@EnableWebSecurity(debug = true)
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public static class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web){
web.ignoring().antMatchers(excludePaths );
如何将从@EnableMYSpringSecurity 传递的值传递给webSecuirty web.ignoring.antMatchers
我们的注解配置
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface EnableMySpringSecurity {
String[] excludePaths() default {};
}
我试过 ApplicationStartupListener 但问题是,它是在 websecuirty 配置后初始化的
public class ApplicationStartupListener implements
ApplicationListener<ContextRefreshedEvent> {
private ApplicationContext context;
private EnableMySSAnnotationProcessor processor;
public ApplicationStartupListener(ApplicationContext context,
EnableMySSAnnotationProcessor processor) {
this.context = context;
this.processor = processor;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
Optional<EnableMySpringSecurity> annotation =
context.getBeansWithAnnotation(EnableMySpringSecurity.class).keySet().stream()
.map(key -> context.findAnnotationOnBean(key, EnableMySpringSecurity.class))
.findFirst();
annotation.ifPresent(enableMySpringSecurity-> processor.process(enableMySpringSecurity));
}
}
【问题讨论】:
-
也许this 会有所帮助?不过,您的库可能更容易接受排除的路径作为配置属性,但
标签: spring-boot spring-security spring-annotations websecurity