"URL was not normalized" 使用 Spring Boot 2 和 Kotlin

"URL was not normalized" with Spring Boot 2 and Kotlin

在我当前的项目中,我们在 OpenshiftKubernetes 中部署了几个 Spring Boot 1.5.4.RELEASE 微服务。我们已经配置了一个 Apache 代理平衡器:

From                            To
/msa/microname1          ->     /
/msa/microname2          ->     /
...

最近我们介绍了 Spring Boot 2,我们用 Kotlin 开发了一个新的微服务。考虑到像 /health/info 这样的 url 被放置在 /actuator 路径下,我们以相同的方式配置了平衡器。 现在,当我们使用这个新微服务的任何端点(/health 或我们的任何端点)时,我们都会遇到这样的错误:

org.springframework.security.web.firewall.RequestRejectedException: The request was rejected because the URL was not normalized ...

我们在微服务中拦截的路径在开头有一个额外的斜线://<path_to_resource>

当我使用微服务时 url 我可以毫无问题地获取资源,但是当使用代理平衡器映射时,我们遇到了上述问题。

我们检查了我们的代理平衡器,它的配置方式与其他平衡器相同。 我们必须考虑 Spring Boot 2 上的任何额外配置吗? 这可能是与 Kotlin 相关的问题吗?

更新

作为一项调整,我们已将 DefaultHttpFirewall 配置为允许 url 编码斜杠,但这并不能解决双斜杠的问题。它只是掩盖了问题。

@Bean
fun allowUrlEncodedSlash(): HttpFirewall {
    var firewall: DefaultHttpFirewall = DefaultHttpFirewall()
    firewall.setAllowUrlEncodedSlash(true)
    return firewall
}

override fun configure(web: WebSecurity) {
    web.httpFirewall(allowUrlEncodedSlash())
}

检查这个答案:

这似乎与您的问题相同,但在 Java 而不是 Kotlin 中。

Spring 默认情况下不喜欢 URL 中的 //

我已经为您转换了链接答案中的Java:

@Bean
fun allowUrlEncodedSlashHttpFirewall(): HttpFirewall {
    val firewall = StrictHttpFirewall()
    firewall.setAllowUrlEncodedSlash(true)
    return firewall
}

通过在我们的平衡器中向应用程序添加上下文来解决。 现在我们所有的端点在我们的微服务中都有一个上下文,在 application.yml.

中定义
From                            To
/msa/microname1          ->     /
/msa/microname2          ->     /
/msa/Kotlinname/kt       ->     /kt