简体   繁体   English

如何使用 Spring Boot Security 修复 Spring Boot Webflux 应用程序中的“名称为 requestMappingHandlerAdapter 的无效 bean 定义”错误

[英]How to fix 'Invalid bean definition with name requestMappingHandlerAdapter' error in Spring Boot Webflux app with Spring Boot Security

I would like to use a spring security with spring boot webflux, but I get a following error when I start the application:我想在 spring boot webflux 中使用 spring security,但是当我启动应用程序时出现以下错误:

org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration; factoryMethodName=requestMappingHandlerAdapter; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]] for bean 'requestMappingHandlerAdapter': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.web.reactive.config.DelegatingWebFluxConfiguration; factoryMethodName=requestMappingHandlerAdapter; initMethodName=null; destroyMethodName=(inferred); defined in org.springframework.web.reactive.config.DelegatingWebFluxConfiguration] bound.
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:897) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:274) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:141) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:117) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:327) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]

gradle dependencies are: gradle 依赖项是:

  • org.springframework.boot:spring-boot-starter-webflux:2.1.3.RELEASE org.springframework.boot:spring-boot-starter-webflux:2.1.3.RELEASE
  • org.springframework.boot:spring-boot-starter-security:2.1.3.RELEASE org.springframework.boot:spring-boot-starter-security:2.1.3.RELEASE

Security config contains @EnableWebFluxSecurity and a minimal userDetailsService and a SecurityWebFilterChain bean definition.安全配置包含 @EnableWebFluxSecurity 和一个最小的 userDetailsService 以及一个 SecurityWebFilterChain bean 定义。

    @Bean
    fun userDetailsService(): MapReactiveUserDetailsService {
        val user = User.withDefaultPasswordEncoder()
                .username("user")
                .password("user")
                .roles("USER")
                .build()
        return MapReactiveUserDetailsService(user)
    }

    @Bean
    fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http
                .authorizeExchange()
                .anyExchange().authenticated()
                .and()
                .httpBasic().and()
                .formLogin()
        return http.build()
    }

It seems to me that spring-boot-starter-webflux is not compatible with spring-boot-starter-security.在我看来,spring-boot-starter-webflux 与 spring-boot-starter-security 不兼容。 I have tried spring-boot-starter-security-reactive library also, but it has only 2.0.0.M5 version.我也尝试过 spring-boot-starter-security-reactive 库,但它只有 2.0.0.M5 版本。 Is there any way to use spring-boot-starter-webflux with spring-boot-starter-security?有没有办法将 spring-boot-starter-webflux 与 spring-boot-starter-security 一起使用?

Can you post your configuration beans?你能发布你的配置bean吗? Do you have both你有两个吗

@EnableWebflux @EnableWebflux

and

@EnableWebMvc @EnableWebMvc

?

Do you rely on overriding beans on purpose?您是否故意依赖覆盖 bean? https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.1-Release-Notes#bean-overriding https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.1-Release-Notes#bean-overriding

Regards, Alex问候, 亚历克斯

Encountered this today, and it happened due to this dependency今天遇到这个,就是因为这个依赖
org.springframework.boot:spring-boot-starter-websocket

Apparently, this starter package for websocket depends on显然,这个 websocket 的入门包取决于
org.springframework.boot:spring-boot-starter-web

After I removed it, everything comes back to normal.我删除它后,一切恢复正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建名为“requestMappingHandlerAdapter”的 bean 时出错 Spring Boot - Error creating bean with name 'requestMappingHandlerAdapter' Spring Boot Spring Boot - 创建名为“requestMappingHandlerAdapter”的bean时出错,NoSuchFieldError:defaultReader - Spring Boot - Error creating bean with name 'requestMappingHandlerAdapter', NoSuchFieldError: defaultReader 在WebFlux中创建名称为requestMappingHandlerMapping的bean时出错(没有Spring Boot) - Error creating bean with name requestMappingHandlerMapping in WebFlux (without Spring Boot) 我为我的 spring 启动应用程序收到在 class 路径资源中定义的名称为 'requestMappingHandlerAdapter' 的创建 bean 时出错 - I get the 'Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource' for my spring boot application Spring Boot-如何使用Cors和Spring Security修复Session Bean - Spring Boot - How to fix Session bean with cors and spring security Spring Boot-bean定义 - Spring Boot - bean definition 如何在Spring Boot和Spring WebFlux中使用“功能bean定义Kotlin DSL”? - How to use “Functional bean definition Kotlin DSL” with Spring Boot and Spring WebFlux? Spring 引导升级错误 - 名称为 org.springframework.transaction.config.internalTransactionalEventListenerFactory 的无效 bean 定义 - Spring Boot Upgrade Error - Invalid bean definition with name org.springframework.transaction.config.internalTransactionalEventListenerFactory 如何修复名称为'requestMappingHandlerAdapter'的bean创建错误 - How to fix Error creating bean with name 'requestMappingHandlerAdapter' 在 Spring 引导应用程序中创建名为“liquibase”的 bean 时出错 - Error creating bean with name 'liquibase' in Spring Boot App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM