Express Gateway - 使用 Bearer Token 的代理请求
Express Gateway - Proxy request with Bearer Token
我正在尝试弄清楚如何让 Express Gateway 在 api 服务的上游请求中使用 Auth Bearer 令牌。
以下是我目前的配置。
http:
port: 8080
admin:
port: 9876
host: localhost
apiEndpoints:
api:
host: localhost
paths:
- '/truck-api/*'
- '/car-api/*'
serviceEndpoints:
truck-service:
url: 'http://10.0.0.2:5010/api'
car-service:
url: 'http://10.0.0.2:5011/api'
policies:
- basic-auth
- cors
- expression
- key-auth
- log
- oauth2
- proxy
- rate-limit
- rewrite
- request-transformer
pipelines:
default:
apiEndpoints:
- api
policies:
- proxy:
-
condition:
name: pathMatch
pattern: "^/truck-api/(.*)"
action:
serviceEndpoint: truck-service
prependPath: true
ignorePath: false
stripPath: true
changeOrigin: true
-
condition:
name: pathMatch
pattern: "^/car-api/(.*)"
action:
serviceEndpoint: car-service
prependPath: true
ignorePath: false
stripPath: true
changeOrigin: true
前端使用适当的 auth 承载令牌调用 express 网关,但是 express 网关不会将该 auth 承载令牌转发到上游服务。
建议?
经过一些挖掘和大量的反复试验,解决方案相对简单,使用请求转换器。
将此添加到政策部分:
- request-transformer:
- action:
headers:
remove: ['Authorization']
add:
Authorization: "'Bearer ***'"
对我来说删除:['Authorization'] 没有用。
我正在使用以下方式对快速网关进行身份验证:
"Authorization: apiKey ${keyId}:${keySecret}"
并使用授权对后端服务进行身份验证:request-transformer 添加的“'Bearer ***'”。我希望删除:['Authorization'] 将删除“授权:apiKey ${keyId}:${keySecret}”,在我已经在快速网关成功通过身份验证并添加:授权:“'Bearer ***'" 将添加新的授权 header,因此新构建的请求会将 apiKey 替换为 Bearer 令牌,但使用 remove: ['Authorization'] 会导致无法在快速网关进行身份验证。
我通过使用 apiKeyHeader (https://www.express-gateway.io/docs/policies/key-authorization/).
更改用于快速网关端授权的 HTTP header 解决了这个问题。
policies:
- request-transformer:
- action:
headers:
add:
# pass oauth2 token
Authorization: "'Bearer ***'"
- key-auth:
- action:
# arbitrarily rename default header name from Authorization
apiKeyHeader: open_sesame
open_sesame header 授予我们访问 api 网关的权限,并且 request-transformer 使用适当的令牌向访问后端服务的请求添加授权 header .
curl -v -H "open_sesame: apiKey ${keyId}:${keySecret}"...
我正在尝试弄清楚如何让 Express Gateway 在 api 服务的上游请求中使用 Auth Bearer 令牌。
以下是我目前的配置。
http:
port: 8080
admin:
port: 9876
host: localhost
apiEndpoints:
api:
host: localhost
paths:
- '/truck-api/*'
- '/car-api/*'
serviceEndpoints:
truck-service:
url: 'http://10.0.0.2:5010/api'
car-service:
url: 'http://10.0.0.2:5011/api'
policies:
- basic-auth
- cors
- expression
- key-auth
- log
- oauth2
- proxy
- rate-limit
- rewrite
- request-transformer
pipelines:
default:
apiEndpoints:
- api
policies:
- proxy:
-
condition:
name: pathMatch
pattern: "^/truck-api/(.*)"
action:
serviceEndpoint: truck-service
prependPath: true
ignorePath: false
stripPath: true
changeOrigin: true
-
condition:
name: pathMatch
pattern: "^/car-api/(.*)"
action:
serviceEndpoint: car-service
prependPath: true
ignorePath: false
stripPath: true
changeOrigin: true
前端使用适当的 auth 承载令牌调用 express 网关,但是 express 网关不会将该 auth 承载令牌转发到上游服务。
建议?
经过一些挖掘和大量的反复试验,解决方案相对简单,使用请求转换器。
将此添加到政策部分:
- request-transformer:
- action:
headers:
remove: ['Authorization']
add:
Authorization: "'Bearer ***'"
对我来说删除:['Authorization'] 没有用。
我正在使用以下方式对快速网关进行身份验证:
"Authorization: apiKey ${keyId}:${keySecret}"
并使用授权对后端服务进行身份验证:request-transformer 添加的“'Bearer ***'”。我希望删除:['Authorization'] 将删除“授权:apiKey ${keyId}:${keySecret}”,在我已经在快速网关成功通过身份验证并添加:授权:“'Bearer ***'" 将添加新的授权 header,因此新构建的请求会将 apiKey 替换为 Bearer 令牌,但使用 remove: ['Authorization'] 会导致无法在快速网关进行身份验证。
我通过使用 apiKeyHeader (https://www.express-gateway.io/docs/policies/key-authorization/).
policies:
- request-transformer:
- action:
headers:
add:
# pass oauth2 token
Authorization: "'Bearer ***'"
- key-auth:
- action:
# arbitrarily rename default header name from Authorization
apiKeyHeader: open_sesame
open_sesame header 授予我们访问 api 网关的权限,并且 request-transformer 使用适当的令牌向访问后端服务的请求添加授权 header .
curl -v -H "open_sesame: apiKey ${keyId}:${keySecret}"...