我使用特雷菲克作为(内部) OAuth2-provider前面的API网关。
直接使用OAuth2-provider可以很好地工作:
curl -XPOST -H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Accept: application/json' -d 'username=username&password=password' \
'http://my-oauth-provider/oauth/token?grant_type=password&client_id=client_id&client_secret=client_secret
// 200 OK, getting a Bearer-token在我的usecase中,我想将id隐藏在Traefik-配置中。一个API-使用者将只提供用户名/密码,client_id等将添加到HTTP由Traefik。
curl -XPOST -H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Accept: application/json' -d 'username=username&password=password' \
'http://my-traefik-api-gateway/oauth/token(如何)使用Traefik可以完成这一任务?
到目前为止我尝试了什么,
我希望像这样使用Traefik的ReplacePathRegex-修饰符来解决我的挑战:
[frontends]
[frontends.oauth-provider]
backend = "my-oauth-provider"
[frontends.oauth-provider.routes.route1]
rule = "ReplacePathRegex: ^(.*)$ $1?grant_type=client_credentials&client_id=client_id&client_secret=client_secret; Path: /oauth/token"这是行不通的。根据我假设的Traefik日志,附加的参数没有添加到实际的查询参数中,而是添加到后端。
发布于 2018-09-25 01:54:27
如果您愿意将此更改应用于traefik,那么它可能有效:
diff --git middlewares/replace_path_regex.go middlewares/replace_path_regex.go
index d753e86c..da21a259 100644
--- middlewares/replace_path_regex.go
+++ middlewares/replace_path_regex.go
@@ -33,7 +33,13 @@ func (s *ReplacePathRegex) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if s.Regexp != nil && len(s.Replacement) > 0 && s.Regexp.MatchString(r.URL.Path) {
r = r.WithContext(context.WithValue(r.Context(), ReplacePathKey, r.URL.Path))
r.Header.Add(ReplacedPathHeader, r.URL.Path)
- r.URL.Path = s.Regexp.ReplaceAllString(r.URL.Path, s.Replacement)
+ replacement := s.Regexp.ReplaceAllString(r.URL.Path, s.Replacement)
+ u, err := r.URL.Parse(replacement)
+ if err != nil {
+ log.Errorf("bad replacement %s: %s", replacement, err)
+ } else {
+ r.URL = u
+ }
r.RequestURI = r.URL.RequestURI()
}
s.Handler.ServeHTTP(w, r)https://stackoverflow.com/questions/48545586
复制相似问题