Joe*_*Joe 5 oauth spring-boot kubernetes kubernetes-ingress nginx-ingress
我在 kubernetes 中部署了一些服务,并使用 NGINX 入口访问外部。(使用 EC2 实例进行所有集群设置)。能够通过与入口绑定的主机访问服务。现在,我不是直接访问 svc,而是尝试在访问服务之前添加身份验证。并重定向到登录页面,用户输入凭据并应重定向到所询问的页面。到目前为止尝试了以下代码 snipet II。请指导寻找解决方案。
我的入口.yml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: my-ingress
namespace: mynamespace
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
nginx.ingress.kubernetes.io/affinity: cookie
nginx.ingress.kubernetes.io/session-cookie-name: JSESSIONID
nginx.ingress.kubernetes.io/ssl-passthrough: "false"
nginx.ingress.kubernetes.io/ssl-redirect: "true"
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
kubernetes.io/ingress.class: "nginx"
kubernetes.io/ingress.allow-http: "false"
nginx.ingress.kubernetes.io/auth-signin: "https://auth.mysite.domain/api/auth/login" #will show login page
nginx.ingress.kubernetes.io/auth-url: "https://auth.mysite.domain/api/auth/token/validate"
nginx.ingress.kubernetes.io/auth-response-headers: "authorization"
spec:
tls:
- hosts:
- mysite.domain
#secretName: ${TLS_TOKEN_NAME}
rules:
- host: a.mysite.domain
http:
paths:
- path: /
backend:
serviceName: myservice1
servicePort: 9090
Run Code Online (Sandbox Code Playgroud)
所以首先它会调用“/token/validate”并且将被未经授权然后进入“auth/login”并且登录页面将在输入进入“/token/validate”的凭据并再次登录页面后显示。实际上应该重定向到被调用的页面。
如何实现这一点?[如果成功认证后,如果我们可以在被调用的 ling 中添加标题我认为可以解决但不确定如何]
后端:Java Spring
@RequestMapping("login")
public String login() {
return "login.html";
}
Run Code Online (Sandbox Code Playgroud)
登录.html
<form action="validate-user" method="post" enctype="application/x-www-form-urlencoded">
<label for="username">Username</label>
<input type="text" id="username" value="admin" name="username" autofocus="autofocus" /> <br>
<label for="password">Password</label>
<input type="password" id="password" value="password" name="password" /> <br>
<input id="submit" type="submit" value="Log in" />
</form>
Run Code Online (Sandbox Code Playgroud)
后端:Java Spring
@PostMapping("validate-user")
@ResponseBody
public ResponseEntity<?> validateUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
...
HttpStatus httpStatus=HttpStatus.FOUND;
//calling authentication api and validating
//else
httpStatus=HttpStatus.UNAUTHORIZED;
HttpHeaders responseHeaders= new HttpHeaders();
responseHeaders.set("Authoriztion", token);
//responseHeaders.setLocation(new URI("https://a.mysite.domain")); ALSO TRIED BUT NOT WORKED
return new ResponseEntity<>(responseHeaders,httpStatus);
}
Run Code Online (Sandbox Code Playgroud)
UPDATE1:我正在使用我自己的自定义身份验证 api,如果我使用来自邮递员的自定义标题“Authorization”:“bearer token”点击 url,那么响应是可以的,但是来自浏览器是不可能的,所以仅来自上游 svc(成功后)登录)标题应该包含在重定向页面中,我们该怎么办?
我缺少任何注释吗?
UPDATE2:在成功身份验证后重定向时,我将令牌作为查询字符串传递,就像responseHeaders.setLocation(new URI("https://a.mysite.domain/?access_token="+token)重定向后要验证一样。成功验证后进入下游 svc[预期]。但是,当该 svc 正在路由时说,a.mysite.domain/route1查询字符串消失了,并且 auth svc 无法401再次获得令牌。它应该像a.mysite.domain/route1/?access_token=token。有什么办法可以做到吗?如果每条路线都具有相同的查询字符串,那么将起作用。[这是我的 PLAN-B ......但 passwing 令牌仍然是标头是我的优先事项]
UPDATE3:我尝试使用注释,如:
nginx.ingress.kubernetes.io/auth-signin: 'https://auth.example.com/api/auth-service-ui/login'
nginx.ingress.kubernetes.io/auth-response-headers: 'UserID, Authorization, authorization'
nginx.ingress.kubernetes.io/auth-snippet: |
auth_request_set $token $upstream_http_authorization;
proxy_set_header Foo-Header1 $token; //not showing as request header AND this value only need LOOKS $token val is missed
proxy_set_header Foo-Header headerfoo1; //showing as request header OK
more_set_input_headers 'Authorization: $token';//not showing as request header AND this value only need LOOKS $token val is missed
nginx.ingress.kubernetes.io/configuration-snippet: |
auth_request_set $token1 $upstream_http_authorization;
add_header authorization2 QQQAAQ1; //showing as response header OK no use
add_header authorization $token; //showing as response header OK how to send as request header on next call
more_set_input_headers 'Authorization11: uuu1';//showing as request header in next call
more_set_input_headers 'Authorization: $token1';//not showing as request header and need this val ONLY
Run Code Online (Sandbox Code Playgroud)
**我错过了什么注释?
UPDATE4 PLAN-C:现在尝试将 jwt 令牌存储在 cookie 中。
nginx.ingress.kubernetes.io/configuration-snippet: |
auth_request_set $token5 $upstream_http_authorization;
add_header Set-Cookie "JWT_TOKEN=$token5";
Run Code Online (Sandbox Code Playgroud)
在每个请求中都设置了相同的 cookie,但每次都在浏览器中存储。即多个相同的cookie。如何只设置一次?
朝着你的B 计划:
在标头中传递令牌是我的首要任务。
我认为仅使用以下注释来执行此操作没有任何问题:
nginx.ingress.kubernetes.io/auth-url: "https://auth.mysite.domain/api/auth/token/validate"
nginx.ingress.kubernetes.io/auth-response-headers: "authorization
Run Code Online (Sandbox Code Playgroud)
跟进官方的external-auth-headers示例:
修改其external-auth-service的源代码,使其在成功认证时引入。响应所需的标头 ( "Authorization": "Bearer <token>"):
w.Header().Add("UserRole", "admin")
w.Header().Add("Other", "not used")
w.Header().Add("Authorization", "Bearer foobar" ) <--- here
fmt.Fprint(w, "ok")
Run Code Online (Sandbox Code Playgroud)
重建 docker 镜像并更新 Deployment 中的 Pod 规范以应用您的自定义镜像
确保您的 Ingress 定义看起来相似:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/auth-response-headers: UserID, authorization
nginx.ingress.kubernetes.io/auth-url: http://demo-auth-
service.default.svc.cluster.local?code=200
Run Code Online (Sandbox Code Playgroud)
检查 auth-service 自定义标头是否注入到对其后端的 Ingress 调用(我使用的是httpbin.org/headers的集群部署版本)
从客户端和Ingress后端角度看到的请求内容:
客户要求:
> GET /headers HTTP/1.1
> Host: custom.example.com
> User-Agent: curl/7.58.0
> Accept: */*
> User: centrino
>
< HTTP/1.1 200 OK
< Server: nginx/1.17.8
< Date: Tue, 14 Jul 2020 11:55:43 GMT
< Content-Type: application/json
< Content-Length: 327
< Connection: keep-alive
< Vary: Accept-Encoding
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Credentials: true
<
Run Code Online (Sandbox Code Playgroud)
在后端看到的客户端请求富含 ext-auth-svc 标头:
{
"headers": {
"Accept": "*/*",
"Authorization": "Bearer foobar", <---is this what you are looking for ?
"Host": "custom.example.com",
"User": "centrino",
"User-Agent": "curl/7.58.0",
"Userid": "8674665223082153551",
"X-Forwarded-Host": "custom.example.com",
"X-Scheme": "http",
"X-Using-Nginx-Controller": "true"
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7404 次 |
| 最近记录: |