10

我在使用 Spring Cloud Gateway 时遇到问题

是否有任何依赖项直接或递归调用 spring-boot-starter-tomcat

它不起作用,因为它将启动嵌入式 tomcat 服务器而不是 spring 云网关使用的 netty 服务器

我开始通过排除这个依赖来解决这个问题

        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>

spring 云网关成功运行

但有时我想使用 spring-cloud-starter-oauth2 来使用 @EnableOAuth2Sso

我开始使用

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-oauth2</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

那时我面临着抛出异常的大问题

原因:java.lang.IllegalStateException:无法自省类 org.springframework.security.oauth2.config.annotation.web.configuration.OAuth2ClientConfiguration 上的注释方法......

引起:java.lang.NoClassDefFoundError: javax/servlet/Filter

4

3 回答 3

3

如您所见,Spring 云网关使用响应式模型,并且基于 netty 而不是 tomcat。被动更改是一个重大转变,目前 Spring Security 不支持,但工作正在进行中,您可以在https://github.com/spring-cloud/spring-cloud-gateway/issues/179跟踪它

于 2018-08-15T14:36:04.687 回答
0

使用以下依赖项(我从我的 build.gradle 复制)

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
    implementation 'org.springframework.cloud:spring-cloud-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
}

对您的网关应用程序进行最低限度的编码,如下所示

@SpringBootApplication
public class App {

    @Bean
    public ForwardedHeaderTransformer forwardedHeaderTransformer() {
        return new ForwardedHeaderTransformer();
    }

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

在 application.yml 中配置

spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-id: XXX
            client-secret: YYY

我正在积极构建我的堆栈,将 OAuth2 与 Docker Swarm Discovery https://github.com/trajano/spring-cloud-demo.git一起使用,这样您就可以看到它是如何工作的。

于 2020-04-26T16:04:29.997 回答
-2

spring boot 2.1 和 spring security 5 已经解决了这个问题,看这个例子

于 2019-01-11T20:28:40.747 回答