Spring boot 由于重复的Bean注册httpSessionManager,无法在SpringBoot2.1中使用KeyClope
我想用KeyClope 4.5保护我的Spring Boot 2.1应用程序 由于以下错误,我当前无法启动应用程序:Spring boot 由于重复的Bean注册httpSessionManager,无法在SpringBoot2.1中使用KeyClope,spring-boot,spring-security,keycloak,Spring Boot,Spring Security,Keycloak,我想用KeyClope 4.5保护我的Spring Boot 2.1应用程序 由于以下错误,我当前无法启动应用程序: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with n
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.support.BeanDefinitionOverrideException:
Invalid bean definition with name 'httpSessionManager' defined in class path resource [dummy/service/SecurityConfig.class]:
Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=securityConfig; factoryMethodName=httpSessionManager; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [dummy/SecurityConfig.class]] for bean 'httpSessionManager':
There is already [Generic bean: class [org.keycloak.adapters.springsecurity.management.HttpSessionManager]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [jar:file:/.m2/repository/org/keycloak/keycloak-spring-security-adapter/4.5.0.Final/keycloak-spring-security-adapter-4.5.0.Final.jar!/org/keycloak/adapters/springsecurity/management/HttpSessionManager.class]] bound.
我的类SecurityConfig(见下文)是从KeyDopperWebSecurityConfigureAdapter扩展而来的。这个适配器已经定义了bean httpSessionManager
我理解为什么这是一个问题。问题是,我如何防止这种情况或解决我的冲突
到目前为止,我已经采取了以下步骤:
- 使用以下内容构建我的pom(见下文):
- SpringBootStarterWeb
- 弹簧启动安全
- 钥匙斗篷弹簧靴启动器
- dependencyManagement中的KeyClope适配器bom表
- 定义了自己的SecurityConfig扩展密钥斗篷WebSecurity配置适配器
。。。
org.springframework.boot
).计划修复5.x的KeyClope。然而,评论中有一个变通办法。只需将注释@keydeposeconfiguration替换为:
@配置
@组件扫描(
basePackageClasses=keydeposecurityComponents.class,
excludeFilters=@ComponentScan.Filter(type=FilterType.REGEX,pattern=“org.keydeport.adapters.springsecurity.management.HttpSessionManager”))
@启用Web安全性
看起来keydepot的Spring安全集成中有一个bug,这意味着子类为keydepowebsecurityconfigureradapter
的应用程序将尝试创建两个名为httpSessionManager
的bean。当两个bean使用相同的名称定义时,遇到的第二个定义将尝试覆盖第一个定义。在Spring Boot 2.1中,默认情况下禁止此重写。我建议将此报告为针对Key斗篷的Spring安全集成的bug。在等待bug得到解决的过程中,您可以通过在应用程序中设置spring.main.allow bean definition overriding=true
。properties
这帮助我解决了一个问题,删除@keydoveconfiguration
,然后改用它(从):
爪哇:
@配置
@组件扫描(
basePackageClasses=keydeposecurityComponents.class,
excludeFilters=@ComponentScan.Filter(type=FilterType.REGEX,pattern=“org.keydeport.adapters.springsecurity.management.HttpSessionManager”))
@启用Web安全性
科特林:
@配置
@组件扫描(
basePackageClasses=[KeyDopperSecurityComponents::class],
excludeFilters=[ComponentScan.Filter(type=FilterType.REGEX,pattern=[“org.keydape.adapters.springsecurity.management.HttpSessionManager”])]
)
@启用Web安全性
解决重复的HttpSessionManager
bean定义的首选方法是在SecurityConfig
中重写此bean的创建,并在其实例化上添加条件注释,如下所示:
@KeycloakConfiguration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Import(KeycloakWebSecurityConfigurerAdapter.class)
class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
@Bean
@Override
@ConditionalOnMissingBean(HttpSessionManager.class)
protected HttpSessionManager httpSessionManager() {
return new HttpSessionManager();
}
}
我在6.0.1版中使用了keydape-spring安全适配器
。使用特殊配置删除@keydapeconfiguration的解决方案对我不起作用
我的解决方案是在application.properties中添加以下行:
spring.main.allow-bean-definition-overriding: true
git消除此错误的最佳方法是在SpringBoot启动期间排除KeyClope自动配置类加载
只需在application.yaml文件中添加以下内容
春天:
自动配置:
排除:
org.keydeport.adapters.springboot.keydeposeautoconfiguration谢谢,这帮助我找到了问题()。这在使用最新的keydeport 8.0和Spring boot 2适配器时非常有用。