如何在 Traefik 1.7 中对后端条目进行授权转发
how to do auth forwarding to backend entry in Traefik 1.7
Traefik 1.7
Docker
Spring开机
我需要使用Traefik的auth转发功能
我的身份验证端点由 Traefik 后面的 spring 启动组件公开,并在 Traefik 仪表板中公开为 "backend-authentication" 和 URI“http://123.1.23.5:8081”。
在我的配置中,traefik 使用 "PathPrefix" 规则将所有内容从“http://api-dev.mycompany.com”路由到后端 API。
因此,我的身份验证组件可用作“http://api-dev.mycompany.com/authentication”
当我像这样进行授权转发时:
[entryPoints]
[entryPoints.https]
address = ":443"
[entryPoints.https.auth.forward]
address = "http://api-dev.mycompany.com/commerce/authentication/v1/ldap/auth"
trustForwardHeader = true
authResponseHeaders = ["Authorization"]
Traefik 经历无限的转发循环。
当我使用以下配置时,它可以正常工作:
[entryPoints]
[entryPoints.https]
address = ":443"
[entryPoints.https.auth.forward]
address = "http://123.1.23.5:8081/commerce/authentication/v1/ldap/auth"
trustForwardHeader = true
authResponseHeaders = ["Authorization"]
我想使用与后端身份验证相关的服务名称,如 Traefik 仪表板中所示,但是当我尝试该配置时:
[entryPoints]
[entryPoints.https]
address = ":443"
[entryPoints.https.auth.forward]
address = "http://backend-authentication/commerce/authentication/v1/ldap/auth"
trustForwardHeader = true
authResponseHeaders = ["Authorization"]
我运行进入错误500。
我确实需要使用逻辑名称而不是 IP 的能力,因为它们可能会发生变化。
我不能 运行 另一个端口或另一个网络上的组件...任何想法都将不胜感激。
也许你可以升级到 v2,那里更清楚一点:
根据 docs 在 Traefik v2 中,您必须使用 forwardAuth 作为中间件。你必须像这样创建一个路由器:
## Dynamic configuration
[http.routers]
[http.routers.my-router] <-- name it auth-router or whatever
rule = "Path(`/foo`)"
# declared in next code block
middlewares = ["test-auth"]
service = "youre-service-docker-or-file" <-- probably your "backend-authentication"
你的中间件在哪里:
# Forward authentication to authserver.com
[http.middlewares]
[http.middlewares.test-auth.forwardAuth]
address = "https://authserver.com/auth" <--- Your auth server here
可选,查看 v1.7 文档,您可以设置
authResponseHeaders = ["X-Auth-User", "X-Secret"]
在入口点下方,也许可以尝试添加一些受信任的 ips:
[entryPoints]
[entryPoints.http]
address = ":80"
# Enable Forwarded Headers
[entryPoints.http.forwardedHeaders]
# List of trusted IPs
#
# Required
# Default: []
#
trustedIPs = ["127.0.0.1/32", "192.168.1.7"]
Traefik 1.7 Docker Spring开机
我需要使用Traefik的auth转发功能
我的身份验证端点由 Traefik 后面的 spring 启动组件公开,并在 Traefik 仪表板中公开为 "backend-authentication" 和 URI“http://123.1.23.5:8081”。
在我的配置中,traefik 使用 "PathPrefix" 规则将所有内容从“http://api-dev.mycompany.com”路由到后端 API。 因此,我的身份验证组件可用作“http://api-dev.mycompany.com/authentication”
当我像这样进行授权转发时:
[entryPoints]
[entryPoints.https]
address = ":443"
[entryPoints.https.auth.forward]
address = "http://api-dev.mycompany.com/commerce/authentication/v1/ldap/auth"
trustForwardHeader = true
authResponseHeaders = ["Authorization"]
Traefik 经历无限的转发循环。
当我使用以下配置时,它可以正常工作:
[entryPoints]
[entryPoints.https]
address = ":443"
[entryPoints.https.auth.forward]
address = "http://123.1.23.5:8081/commerce/authentication/v1/ldap/auth"
trustForwardHeader = true
authResponseHeaders = ["Authorization"]
我想使用与后端身份验证相关的服务名称,如 Traefik 仪表板中所示,但是当我尝试该配置时:
[entryPoints]
[entryPoints.https]
address = ":443"
[entryPoints.https.auth.forward]
address = "http://backend-authentication/commerce/authentication/v1/ldap/auth"
trustForwardHeader = true
authResponseHeaders = ["Authorization"]
我运行进入错误500。
我确实需要使用逻辑名称而不是 IP 的能力,因为它们可能会发生变化。
我不能 运行 另一个端口或另一个网络上的组件...任何想法都将不胜感激。
也许你可以升级到 v2,那里更清楚一点:
根据 docs 在 Traefik v2 中,您必须使用 forwardAuth 作为中间件。你必须像这样创建一个路由器:
## Dynamic configuration
[http.routers]
[http.routers.my-router] <-- name it auth-router or whatever
rule = "Path(`/foo`)"
# declared in next code block
middlewares = ["test-auth"]
service = "youre-service-docker-or-file" <-- probably your "backend-authentication"
你的中间件在哪里:
# Forward authentication to authserver.com
[http.middlewares]
[http.middlewares.test-auth.forwardAuth]
address = "https://authserver.com/auth" <--- Your auth server here
可选,查看 v1.7 文档,您可以设置
authResponseHeaders = ["X-Auth-User", "X-Secret"]
在入口点下方,也许可以尝试添加一些受信任的 ips:
[entryPoints]
[entryPoints.http]
address = ":80"
# Enable Forwarded Headers
[entryPoints.http.forwardedHeaders]
# List of trusted IPs
#
# Required
# Default: []
#
trustedIPs = ["127.0.0.1/32", "192.168.1.7"]