简体   繁体   English

Spring Boot 使用带有 Oauth2 的云网关

[英]Spring boot use Cloud gateway with Oauth2

My problem is Cloudgateway security with Oauth2.我的问题是 Oauth2 的 Cloudgateway 安全性。 However, Oauth2's config @EnableOAuth2Sso will cause the following error:但是,Oauth2 的配置@EnableOAuth2Sso会导致以下错误:

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. org.springframework.cloud.gateway.config.GatewayAutoConfiguration 中方法 modifyRequestBodyGatewayFilterFactory 的参数 0 需要一个无法找到的类型为“org.springframework.http.codec.ServerCodecConfigurer”的 bean。

Action:行动:

Consider defining a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' in your configuration.考虑在您的配置中定义一个 'org.springframework.http.codec.ServerCodecConfigurer' 类型的 bean。

When I did the same with Zuul proxy on Eureka, everything worked fine.当我在 Eureka 上对 Zuul 代理执行相同操作时,一切正常。 Please help me how to solve this problem.请帮助我如何解决这个问题。

This is Cloudgateway project and I'm trying to make it an Oauth2 client:这是 Cloudgateway 项目,我正在尝试使其成为 Oauth2 客户端:

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:应用程序.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 :我通过授权代码模型使用 Oauth2 服务器,参考这个问题

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! Spring Cloud Gateway 依赖 Spring Webflux(使用 Netty Web Server),Spring Cloud OAuth2 依赖 Spring Boot Web(使用 Tomcat Web Server)...... 两个 Web 服务器不能同时使用!

Dependency graph (only what matters):依赖关系图(只有重要的):

1) 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) 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 ...总之,如果排除Tomcat依赖,它可能会起作用......

eg (for gradle)例如(对于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')
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM