问题标签 [spring-cloud-gateway]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
101 浏览

spring-security-oauth2 - 在重定向之前在标头中设置令牌

成功登录后,我在 Spring Cloud Gateway 中使用 WebFluxSecurity 发生重定向,但是重定向 url 的标头不包含 access_token。

重定向前如何在header中设置access_token?任何指针?

这是我的弹簧属性:

我一直在关注这个回购:https ://github.com/mraible/webflux-oauth2

0 投票
2 回答
2296 浏览

java - Spring Cloud 网关不调用源服务

我第一次使用spring cloud gateway。我的服务端点是http://localhost:8080/student/getlist

问题是,当我尝试使用云网关调用我的服务时,它给了我 404。尤里卡正确显示了服务 URL

我的网关属性如下

下面是网关中的执行器路由

我试图通过网关调用服务端点

上面的 URL 但这不起作用。我究竟做错了什么。任何微服务都没有上下文路径。示例代码在

0 投票
1 回答
1909 浏览

spring-boot - Spring Boot(微服务)网关在使用 JWT 进行身份验证时失败

我用 Spring Boot Gateway 构建了一个微服务网关,它可以工作。接下来我添加了安全性,但我无法将网关转发身份验证请求发送到身份验证器微服务。如何将请求转发到身份验证器微服务?据我了解,请求在网关中失败。在这里,我描述了我所做的事情,源代码在 git 上:

https://github.com/pavelmorozov/SpringBootGateway , https://github.com/pavelmorozov/EurekaServer , https://github.com/pavelmorozov/ConfigMicroService , https://github.com/pavelmorozov/AuthenticatorMicroService

这里是我用来验证的请求 auth.sh

它输出到控制台:

我试图设置断点并找出网关和身份验证器中的问题所在,但这无济于事,因为我的应用程序类中的任何断点都没有执行。只有当我将日志设置为调试时,我才在日志中发现异常:

我不明白这个异常的真正含义,我发现很多人都会遇到相同的异常,但它可能会在不同的情况下抛出。

要构建网关,我主要遵循https://medium.com/omarelgabrys-blog/microservices-with-spring-boot-authentication-with-jwt-part-3-fafc9d7187e8上的指南

那里使用了 Zuul 但没有使用 Spring 云网关,尽管我的网关以前工作过,并且知道如何构建路由应该没问题。ZUUL config 中只有一件事,我没有找到该怎么做:

更新添加了调试日志。它包含应用程序负载和本文顶部描述的一个 curl 调用。警告!文件大小超过 1 mb

https://raw.githubusercontent.com/pavelmorozov/SpringBootGateway/master/doc/debug.log

更新我试图简化网关,以找出问题所在。我注释掉了spring-boot-starter-security依赖和安全类,我也必须注释掉spring-boot-starter-tomcat依赖。网关工作。使用 maven 导入 Tomcat 后,请求时出现错误:

之后我找到了线程,好像说 TomCat 与 spring-cloud-gateway 不兼容https://github.com/spring-cloud/spring-cloud-gateway/issues/145

然后这导致 spring-cloud-gateway 也无法与 Spring Security 一起使用,因为 OncePerRequest 过滤器方法描述需要 HTTPServletRequest、HttpServletResponse 并且我导入 TomCat 以将它们放在类路径中

更新实际上 Spring Security 似乎可以与 Spring Cloud Gateway 一起使用,但它应该以反应方式配置。

0 投票
1 回答
1399 浏览

spring-cloud - 可以将 Spring Cloud Gateway App 启用为 Cloud Config Server 吗?

我正在尝试启用 Spring Cloud Gateway 应用程序以即时自动刷新其路由配置 yml。我已经能够设置一个 Cloud Config 服务器来托管 YAML 路由,并启用 Spring Cloud Gateway 通过@Scheduled contextRefresher.refresh()使用来自动刷新其配置。但是,这需要两个正在运行的应用程序,我们希望尽量减少支持此要求所需的额外服务器数量。

Spring Cloud Config Server 文档建议任何启动应用程序都可以通过@EnableConfigServer注释嵌入配置服务器:https ://cloud.spring.io/spring-cloud-static/spring-cloud.html#_spring_cloud_config_server

但是,当尝试spring-cloud-config-server在网关的 build.gradle 中引入模块时,我在启动时遇到以下错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.http.codec.ServerCodecConfigurer' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

从这里的讨论(https://github.com/spring-cloud/spring-cloud-gateway/issues/319)看来,出现上述问题是因为 spring-boot-starter-web 与网关不兼容;Gateway 是一个 Netty 应用程序,spring-boot-starter-web 使用 tomcat/servlets。尝试spring-boot-starter-web从 cloud-config-server 模块中排除时,应用程序再次失败,原因如下:

onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [org.springframework.cloud.config.server.config.ConfigServerAutoConfiguration]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.class] cannot be opened because it does not exist

是否无法将 Spring Cloud Gateway 应用程序启用为 CloudConfig 服务器?或者我错过了什么。FWIW 我的 sprincCloudVersion 是Finchley.SR1

0 投票
1 回答
1689 浏览

spring-cloud - 如何解决 java.lang.IllegalStateException: Only one connection receive subscriber allowed

我使用spring-cloud-gateway搭建了网关服务,但是当服务接收到POST请求时,出现这个异常:“java.lang.IllegalStateException: Only one connection received subscriber allowed”。如何解决这个问题?下面是我的代码。谢谢你。

0 投票
3 回答
3869 浏览

spring-security - 更改 Spring Security WebFilter 的顺序

更改 Spring Security WebFilter 的顺序

我有一个使用 Spring Cloud Gateway 实现的 API Gateway,它使用 Spring Security。WebFlux 的 Spring Security 在过滤器链的开头作为 WebFilter 实现。所以在认证成功后,请求会被转发到 Spring Cloud Gateway 的 RoutePredicateHandlerMapping,它会尝试根据 URL 模式推断目的地,然后它会去 FilteringWebHandler 执行 Spring Cloud Gateway 的其他过滤器。

我的问题如下:我已经实现了一个自定义的身份验证算法,它使用查询字符串和标头变量作为根据项目要求进行身份验证的凭据,这是没有任何问题的。当我们需要为独立于路径的身份验证算法添加一个小的自定义项时,就会出现问题。当请求到达 Spring Security 的 WebFilter 时,模式匹配还没有完成,所以我不知道它指向哪个应用程序,例如:

应用程序1:

-路径:/app1/**

应用程序2:

-路径:/app2/**

这意味着我应该进行路由映射-> 身份验证-> 过滤Web 处理程序,而不是进行身份验证-> 路由映射-> 过滤Web 处理程序。并不是说这三个组件不相似,其中一个是过滤器,另一个是映射器,最后一个是 Web 处理程序。现在我知道如何自定义它们,但问题是我不知道如何拦截 Netty 服务器构建过程以更改这些操作的顺序。我需要等待构建过程结束并在服务器启动之前更改服务器的内容。我怎样才能做到这一点?

0 投票
1 回答
1125 浏览

spring-security - 带证书的 SSL/TLS 配置

我遇到了 TSL/SSL 配置问题。一切都像文档中一样配置https://cloud.spring.io/spring-cloud-gateway/multi/multi__tls_ssl.html

使用此配置,无法使用http://localhost:8080/home调用端点- 这是一种理想的行为。

尽管进行了配置,但我可以调用所有端点,例如 https:localhost:8080/home 而无需应用适当的证书。在 chrome 中,我只需单击“仍然继续”即可获得端点的内容。

我不想允许这样做 - 如果您想从外部世界调用端点,您需要拥有客户端证书。这是我的目标,但我错过了一些东西。

如何使用 Spring Cloud Gateway 实现这一点?

0 投票
1 回答
524 浏览

spring-mvc - 部分反序列化存储在 hazelcast 中的 spring session 对象

我在 Spring 应用程序中使用 hazelcast 进行会话复制,这是一个 mvc 应用程序。我有一个单独的应用程序,它是一个 api 网关(弹簧云网关),它具有 hazelcast 客户端,能够从 mvc 应用程序读取 hazelcast 会话详细信息。

Spring session 以以下格式将 session 详细信息存储在 hazelcast 中:

我的 hazelcast 客户端尝试使用会话 ID 读取会话映射时

失败并出现错误HazelcastSerializationException,导致SecurityContextImpl.class not found。这个类不会出现在 Spring Cloud Gateway 应用程序中,因为它是响应式的。我不关心网关端的 SPRING_SECURITY_CONTEXT 属性。我只需要我设置的其他会话属性。那么是否可以忽略 SecurityContextImpl 并仍然反序列化 MapSession?请帮忙。

0 投票
1 回答
812 浏览

spring - 如何在 Spring Cloud Gateway 中为发现定位器编写复杂的谓词?

我想自定义发现定位器行为。例如,在我的案例中,路由从gateway_host/prohibitions名为prohibitions-ui. 为此,我正在使用此配置:

然后报错:

我认为由于传递给substring方法的两个参数而导致此错误上升。如果我将方法调用更改为substring(0)然后应用程序成功启动,但这样的配置对我来说没有意义: predicates: Path='/'+serviceId.substring(0)+'/**'

0 投票
1 回答
2467 浏览

ssl - 如何在 Spring Cloud 网关中使用 SSL

配置文件如下:</p>

java keytool 生成的密钥库。项目可以正常启动,当我通过网关请求时,它是错误的。消息如下:

io.netty.handler.ssl.NotSslRecordException:不是 SSL/TLS 记录:474554202f55414d532f75616d737465737420485454502f312e310d0a486f73743a203139322e3136382e302e3131313a383038300d0a436f6e6e656374696f6e3a206b6565702d616c6976650d0a43616368652d436f6e74726f6c3a206d61782d6167653d300d0a557067726164652d496e7365637572652d52657175657374733a20310d0a557365722d4167656e743a204d6f7a696c6c612f352e30202857696e646f7773204e542031302e303b20574f57363429204170706c655765624b69742f3533372e333620284b48544d4c2c206c696b65204765636b6f29204368726f6d652f36392e302e333439372e313030205361666172692f3533372e33360d0a4163636570743a20746578742f68746d6c2c6170706c69636174696f6e2f7868746d6c2b786d6c2c6170706c69636174696f6e2f786d6c3b713d302e392c696d6167652f776562702c696d6167652f61706e672c2a2f2a3b713d302e380d0a4163636570742d456e636f64696e673a20677a69702c206465666c6174650d0a4163636570742d4c616e67756167653a207a682d434e2c7a683b713d302e392c656e3b713d302e380d0a436f6f6b69653a2053455353494f4e3d61663534343537362d376363342d343832332d383163362d6662343936663165326232370d0a0d0a at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1156) [netty-handler-4.1.27.Final.jar:4.1.27.Final] at io.netty.handler.ssl.SslHandler.解码(SslHandler.java:1221)[netty-handler-4.1.27.Final.jar:4.1.27.Final] 在 io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:489)[netty-codec -4.1.27.Final.jar:4.1.27.Final] 在 io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:428) [netty-codec-4.1.27.Final.jar:4.1.27 .Final] 在 io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:265) [netty-codec-4.1.27.Final.jar:4.1.27.Final] 在 io.netty.channel.AbstractChannelHandlerContext。invokeChannelRead(AbstractChannelHandlerContext.java:362) [netty-transport-4.1.27.Final.jar:4.1.27.Final] 在 io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [netty-transport-4.1 .27.Final.jar:4.1.27.Final] 在 io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [netty-transport-4.1.27.Final.jar:4.1.27.Final] 在io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434) [netty-transport-4.1.27.Final.jar:4.1.27.Final] 在 io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext. java:362) [netty-transport-4.1.27.Final.jar:4.1.27.Final] 在 io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [netty-transport-4.1.27.Final.jar:4.1.27.Final] 在 io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965) [netty-transport-4.1.27.Final.jar :4.1.27.Final] 在 io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) [netty-transport-4.1.27.Final.jar:4.1.27.Final] 在 io。 netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:646) [netty-transport-4.1.27.Final.jar:4.1.27.Final] 在 io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop. java:581) [netty-transport-4.1.27.Final.jar:4.1.27.Final] 在 io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:498) [netty-transport-4.1.27 .Final.jar:4.1.27.Final] 在 io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:460) [netty-transport-4.1.27.Final.jar:4.1.27.Final] 在 io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884) [netty-common-4.1.27.Final.jar:4.1.27.Final] 在java.lang.Thread.run(Thread.java:748) [na:1.8.0_171]