This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.10.2.2. 在 Service Mesh 1.x 和 OpenShift Serverless 中使用 JSON Web 令牌身份验证
					您可以使用 Service Mesh 1.x 和 OpenShift Serverless 在 Knative 服务中使用 JSON Web Token (JWT) 身份验证。要做到这一点,您必须在作为 ServiceMeshMemberRoll 对象的成员的应用程序命名空间中创建策略。您还必须为该服务启用 sidecar 注入。
				
						在启用了 Kourier 时,不支持在系统命名空间中向 pod 添加 sidecar 注入,如 knative-serving 和 knative-serving-ingress。
					
如果需要对这些命名空间中的 pod 进行 sidecar 注入,请参阅 OpenShift Serverless 文档中的原生将 Service Mesh 与 OpenShift Serverless 集成。
先决条件
- 您已在集群中安装了 OpenShift Serverless Operator、Knative Serving 和 Red Hat OpenShift Service Mesh。
 - 
							安装 OpenShift CLI (
oc) 。 - 您已创建了一个项目,或者具有适当的角色和权限访问项目,以便在 OpenShift Container Platform 中创建应用程序和其他工作负载。
 
流程
在您的服务中添加
sidecar.istio.io/inject="true"注解:服务示例
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 应用
Service资源:oc apply -f <filename>
$ oc apply -f <filename>Copy to Clipboard Copied! Toggle word wrap Toggle overflow 在作为
ServiceMeshMemberRoll对象的成员的无服务器应用程序命名空间中创建策略,该策略只允许具有有效 JSON Web Tokens(JWT)的请求:重要路径
/metrics和/healthz必须包含在excludePaths中,因为它们是从knative-serving命名空间中的系统 pod 访问的。Copy to Clipboard Copied! Toggle word wrap Toggle overflow 应用
Policy资源:oc apply -f <filename>
$ oc apply -f <filename>Copy to Clipboard Copied! Toggle word wrap Toggle overflow 
验证
如果您尝试使用
curl请求来获取 Knative 服务 URL,则会被拒绝:curl http://hello-example-default.apps.mycluster.example.com/
$ curl http://hello-example-default.apps.mycluster.example.com/Copy to Clipboard Copied! Toggle word wrap Toggle overflow 输出示例
Origin authentication failed.
Origin authentication failed.Copy to Clipboard Copied! Toggle word wrap Toggle overflow 使用有效 JWT 验证请求。
获取有效的 JWT 令牌:
TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -
$ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -Copy to Clipboard Copied! Toggle word wrap Toggle overflow 使用
curl请求标头中的有效令牌访问该服务:curl http://hello-example-default.apps.mycluster.example.com/ -H "Authorization: Bearer $TOKEN"
$ curl http://hello-example-default.apps.mycluster.example.com/ -H "Authorization: Bearer $TOKEN"Copy to Clipboard Copied! Toggle word wrap Toggle overflow 现在允许请求:
输出示例
Hello OpenShift!
Hello OpenShift!Copy to Clipboard Copied! Toggle word wrap Toggle overflow