【问题标题】:Spring boot use Cloud gateway with Oauth2Spring Boot 使用 Cloud Gateway 和 Oauth2
【发布时间】:2019-01-09 16:50:58
【问题描述】:

我的问题是使用 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();
    }
}

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

我正在使用授权码模式的Oauth2服务器,参考这个question

【问题讨论】:

标签: java spring-boot oauth-2.0 spring-security-oauth2 spring-cloud-gateway


【解决方案1】:

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

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

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')
}

【讨论】:

    猜你喜欢
    • 2021-10-20
    • 2020-07-04
    • 2020-06-20
    • 2022-11-08
    • 2021-05-07
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 2018-08-18
    相关资源
    最近更新 更多