1

My problem is Cloudgateway security with Oauth2. However, Oauth2's config @EnableOAuth2Sso will cause the following error:

Description:

Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' in your configuration.

When I did the same with Zuul proxy on Eureka, everything worked fine. Please help me how to solve this problem.

This is Cloudgateway project and I'm trying to make it an Oauth2 client:

Config:

@Configuration
@EnableOAuth2Sso
public class UiSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/**")
                .authorizeRequests()
                .antMatchers("/", "/login**")
                .permitAll()
                .anyRequest()
                .authenticated();
    }
}

application.yml:

server:
  port: 8080
  servlet:
    session:
      cookie:
        name: UISESSION
security:
  oauth2:
    client:
      clientId: SampleClientId
      clientSecret: secret
      accessTokenUri: http://localhost:8085/auth/oauth/token
      userAuthorizationUri: http://localhost:8085/auth/oauth/authorize
    resource:
      userInfoUri: http://localhost:8085/auth/principal
spring:
  application:
    name: gateway
  cloud:
    gateway:
      discovery:
         locator:
             enabled: false

      routes:
      - id: microservice1WelcomeRoute
        uri: http://localhost:8083/view/welcome
        predicates:
            - Path=/microservice1/welcome

I am using Oauth2 server by Authorization Code model, referring to this question:

aboger
  • 2,214
  • 6
  • 33
  • 47
Silver Light
  • 11
  • 1
  • 3
  • You may want to be aware of https://stackoverflow.com/questions/49795385/use-spring-cloud-gateway-with-oauth2 and the linked issue – Ryan Dawson Aug 17 '18 at 11:59

1 Answers1

7

Spring Cloud Gateway depends on Spring Webflux (which uses Netty Web Server), Spring Cloud OAuth2 depends on Spring Boot Web (which uses Tomcat Web Server) ... Both web servers cannot be used at the same time!

Dependency graph (only what matters):

1)

* org.springframework.cloud:spring-cloud-starter-gateway:2.0.1.RELEASE
|-* org.springframework.boot:spring-boot-starter-webflux:2.0.5.RELEASE
  |-* org.springframework.boot:spring-boot-starter-reactor-netty:2.0.5.RELEASE

2)

* org.springframework.cloud:spring-cloud-starter-oauth2:2.0.0.RELEASE
|-* org.springframework.cloud:spring-cloud-starter-security:2.0.0.RELEASE
  |-*org.springframework.cloud:spring-cloud-security:2.0.0.RELEASE
    |-*org.springframework.boot:spring-boot-starter-web:2.0.5.RELEASE
      |-*org.springframework.boot:spring-boot-starter-tomcat:2.0.5.RELEASE

In summary, if you exclude the Tomcat dependency, it will probably work ...

e.g (for gradle)

dependencies {
    // ...
    implementation('org.springframework.cloud:spring-cloud-starter-gateway')
    implementation('org.springframework.cloud:spring-cloud-starter-oauth2') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
    }
    // ...
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}
Marcelo C.
  • 3,822
  • 2
  • 22
  • 11