Spring-cloud-gateway: gateway+oauth2 error

Created on 7 Aug 2018  ·  13Comments  ·  Source: spring-cloud/spring-cloud-gateway

Question:
An error occurred when the service was started

demo project address: gateway-oauth2

Bug report(error log):

2018-08-07 21:47:46.119 ERROR 732 --- [ost-startStop-1] o.s.b.web.embedded.tomcat.TomcatStarter : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'gatewayControllerEndpoint' defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration$GatewayActuatorConfiguration.class]: Unsatisfied dependency expressed through method 'gatewayControllerEndpoint' parameter 2; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'modifyRequestBodyGatewayFilterFactory' defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration.class]: Unsatisfied dependency expressed through method 'modifyRequestBodyGatewayFilterFactory' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.http.codec.ServerCodecConfigurer' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Most helpful comment

Why are you closing this issue? What are the plans for supporting cloud gateway + oauth2 ? Im forced now to discard cloud gateway. :(

All 13 comments

Please learn how to format code on GitHub.

Can you provide a complete, minimal, verifiable sample that reproduces the problem? It should be available as a GitHub (or similar) project or attached to this issue as a zip file.

Sounds like you might have spring-boot-starter-web on the classpath.

Also spring-cloud-starter-oauth does not yet have support for the gateway https://github.com/spring-cloud/spring-cloud-security/issues/141

@ryanjbaxter
demo address:
https://github.com/gaofeipeng/gateway-oauth2

“Sounds like you might have spring-boot-starter-web on the classpath.”

Yes. I found spring-boot-starter-web on the classpath.Thank you for your reminding.

Did the error go away after removing spring-boot-starter-web?

I have the same issue, I just made a small modification in spring-cloud-gateway-sample. only adding the following two dependencies for I need to support oauth (if I does not use gateway , my oauth works well).

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
        <groupId>org.springframework.security.oauth.boot</groupId>
        <artifactId>spring-security-oauth2-autoconfigure</artifactId>
        <version>2.0.0.RELEASE</version>
        </dependency>

then I added "@EnableOAuth2Sso" to main class. When I start my gateway project. it reports the following error:
...
java.lang.Thread.run(Thread.java:748)
2018-08-08 19:46:09.047 WARN 16540 --- [ost-startStop-1] o.a.c.loader.WebappClassLoaderBase : The web application [ROOT] appears to have started a thread named [reactor-http-nio-7] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(WindowsSelectorImpl.java:296)
sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(WindowsSelectorImpl.java:278)
sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:159)
sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
io.netty.channel.nio.SelectedSelectionKeySetSelector.select(SelectedSelectionKeySetSelector.java:62)
io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:753)
io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:409)
io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
java.lang.Thread.run(Thread.java:748)
2018-08-08 19:46:09.047 WARN 16540 --- [ost-startStop-1] o.a.c.loader.WebappClassLoaderBase : The web application [ROOT] appears to have started a thread named [reactor-http-nio-8] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(WindowsSelectorImpl.java:296)
sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(WindowsSelectorImpl.java:278)
sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:159)
sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
io.netty.channel.nio.SelectedSelectionKeySetSelector.select(SelectedSelectionKeySetSelector.java:62)
io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:753)
io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:409)
io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
java.lang.Thread.run(Thread.java:748)
2018-08-08 19:46:09.051 WARN 16540 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
2018-08-08 19:46:09.060 INFO 16540 --- [ main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-08-08 19:46:09.194 ERROR 16540 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

Parameter 0 of method hystrixGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration$HystrixConfiguration required a bean of type 'org.springframework.web.reactive.DispatcherHandler' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.web.reactive.DispatcherHandler' in your configuration.

Process finished with exit code 1
...

If I remove the spring-boot-starter-web

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

when I started ,the error is like this

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.example.demogateway.DemogatewayApplication]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.security.oauth2.config.annotation.web.configuration.OAuth2ClientConfiguration
...
Caused by: java.lang.NoClassDefFoundError: javax/servlet/Filter

then I added javax.servlet-api dependeancy, the new error is like this.

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at com.example.demogateway.DemogatewayApplication.main(DemogatewayApplication.java:80) [classes/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:204) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    ... 8 common frames omitted

Notes:
When the gateway runs standalone, it start netty,
When my oauth runs standalone,it start tomcat,
How to integrate gateway and oauth?

Apprecate your help!
You can see my code here.

@ryanjbaxter

"Did the error go away after removing spring-boot-starter-web?"

Yes.But when I add the @ResourceServerConfig config,new error occurred when the service was started

demo address(Branch:exclusion-spring-boot-starter-web):
https://github.com/gaofeipeng/gateway-oauth2/tree/exclusion-spring-boot-starter-web

Bug report(error log):

2018-08-08 01:05:58.318 ERROR 12608 --- [           main] o.s.boot.SpringApplication               : Application run failed
org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfiguration':
        Post-processing of merged bean definition failed; nested exception is java.lang.TypeNotPresentException: Type javax.servlet.Filter not present
...

@gaofeipeng @yqbjtu like I said above support for spring cloud gateway has not yet been added to spring-cloud-security it is not surprising it doesnt work.

@ryanjbaxter , Thank you! Spring Cloud gateway is very excelent project/product. Thank you for your contribution!
It is very common to configure OAuth2 in gateway now, It is better to add support gateway in spring-cloud-security ASAP.

Why are you closing this issue? What are the plans for supporting cloud gateway + oauth2 ? Im forced now to discard cloud gateway. :(

@TinaRanic because there is already an issue open to add this support spring-cloud/spring-cloud-security#141

I expect that the issue can be resolved one day.

@wendrewshay No problem ,haha

这个问题解决了吗?同一个问题。@yqbjtu

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Burt-L picture Burt-L  ·  25Comments

sincang picture sincang  ·  41Comments

re6exp picture re6exp  ·  36Comments

adrianbrad picture adrianbrad  ·  30Comments

tony-clarke-amdocs picture tony-clarke-amdocs  ·  32Comments