【问题标题】:Spring Boot custom annotationsSpring Boot 自定义注解
【发布时间】:2021-10-29 05:34:52
【问题描述】:

在我的所有项目中,我都需要相同的 WebSecurityConfig。所以我复制了扩展 WebSecurityConfigurerAdapter 的相同实现。

我喜欢将它添加到我在每个项目中使用的公共库中,并为此创建一个注释。 但是注解类不能有超类。

我试过了:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Configuration
@EnableWebSecurity
public @interface EnableGlobalSecurity extends WebSecurityConfigurerAdapter {
}

有没有更好的方法来让它工作?我只想通过添加注释或类似内容来添加我的 SecurityConfiguration。

【问题讨论】:

  • 如果我理解正确,您是否想要一个通用的安全配置并将其保留为jar,然后通过使用例如@EnableGlobalSecurity 注释任何项目来使用此配置并设置安全性?跨度>
  • 是的。我在所有项目中都有相同的安全选项(例如,路径需要经过完全身份验证的用户并且所有人都可以访问。

标签: java spring-boot annotations


【解决方案1】:

您应该能够在每个应该使用它的应用程序中只使用@Import 共享的@Configuration 类。

另一种方法是create a custom auto-configuration。这样,声明对共享库的依赖就足够了。

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 1970-01-01
    • 2019-03-15
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 2019-06-14
    • 1970-01-01
    相关资源
    最近更新 更多