1

我的问题是使用 Oauth2 的 Cloudgateway 安全性。但是,Oauth2 的配置@EnableOAuth2Sso会导致以下错误:

描述:

org.springframework.cloud.gateway.config.GatewayAutoConfiguration 中方法 modifyRequestBodyGatewayFilterFactory 的参数 0 需要找不到类型为“org.springframework.http.codec.ServerCodecConfigurer”的 bean。

行动:

考虑在你的配置中定义一个 'org.springframework.http.codec.ServerCodecConfigurer' 类型的 bean。

当我在 Eureka 上对 Zuul 代理执行相同操作时,一切正常。请帮助我如何解决这个问题。

这是 Cloudgateway 项目,我正在尝试使其成为 Oauth2 客户端:

配置:

@Configuration
@EnableOAuth2Sso
public class UiSecurityConfig extends WebSecurityConfigurerAdapter {

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

应用程序.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

我通过授权码模型使用 Oauth2 服务器,参考这个问题

4

1 回答 1

7

Spring Cloud Gateway 依赖 Spring Webflux(使用 Netty Web Server),Spring Cloud OAuth2 依赖 Spring Boot Web(使用 Tomcat Web Server)……两个 web server 不能同时使用!

依赖关系图(仅重要的):

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

总之,如果排除Tomcat 依赖项,它可能会工作......

例如(对于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')
}
于 2018-10-12T05:47:26.750 回答