共计 3724 个字符,预计需要花费 10 分钟才能阅读完成。
官网链接
本教程的指标是通过身份验证和受权来爱护和部署产品列表应用程序,因而只有具备正确受权的用户能力取得身份验证应用程序中的产品。没有必要受权的用户能够登录应用程序,但看不到产品。
本教程的根底是一个 Node.js 应用程序,它应用 express 框架和 SAPUI5 来显示产品列表(参见屏幕截图)。
XSUAA and the Application Router
为了爱护此产品列表应用程序,应用了两个组件。一种称为 XSUAA 服务,另一种称为利用路由器。应用程序路由器与 XSUAA 服务联合应用来验证用户并将用户路由到受爱护的应用程序。
XSUAA 表演 OAuth 受权服务的角色,而应用程序路由器表演 OAuth 客户端的角色。此外,利用路由器充当利用的地方入口点。
为了避免在未经身份验证的状况下间接调用您的应用程序,有必要向您的应用程序增加一些代码。在咱们的示例中,您应用 Node.js 护照身份验证中间件并应用 XSUAA JWT 策略对其进行配置。
此代码可避免在没有无效 JWT 的状况下间接调用产品列表应用程序。
checkReadScope 函数确保只有具备正确权限的用户能力查看产品。
If you want to use SAP modules locally, you need to add the npm configuration:
npm config set @sap:registry https://npm.sap.com
approuter 将使您可能创立到您的应用程序的平安路由。
在 manifest.yaml 中,您必须为利用程序定义主机名并增加目的地。清单文件用于将 XSUAA 服务实例绑定到您的应用程序。
应用参数 route 为您的应用程序指定一个特定的主机名。路由在整个 Cloud Foundry 环境中必须是惟一的,因而请确保向路由增加随机局部,例如您的姓名首字母和出生日期,如 product-list-ap25 和 approuter-product-list-ap25。稍后您还须要路由来配置目的地。
name 参数与之前在文件 xs-app.json 中定义的雷同。url 参数是应用程序主机名和 Cloud Foundry 环境区域 (https://<hostname>.cfapps.<region>.hana.ondemand.com) 的后果。forwardAuthToken 参数设置为 true 可确保 approuter 将 JWT 令牌转发到目的地。
package.json 文件的依赖:
要应用 XSUAA 服务,须要一个名为 xs-security.json 的文件。该文件能够定义 XSUAA 服务实例的属性以及不同的角色和受权。在此示例中,该文件蕴含一个角色模板和一个具备产品列表查看者角色的角色汇合,使用户能够稍后查看产品。
新建一个 security 文件夹,xs-security.json 文件内容保护如下:
{ | |
"xsappname": "product-list", | |
"tenant-mode": "dedicated", | |
"scopes": [ | |
{ | |
"name": "$XSAPPNAME.read", | |
"description": "With this scope, USER can read products." | |
} | |
], | |
"role-templates": [ | |
{ | |
"name": "Viewer", | |
"description": "Role to get the list of products", | |
"scope-references": ["$XSAPPNAME.read"] | |
} | |
], | |
"role-collections": [ | |
{ | |
"name": "ProductListViewer", | |
"description": "Product List Viewer", | |
"role-template-references": ["$XSAPPNAME.Viewer"] | |
} | |
] | |
} |
这将创立一个具备角色模板的角色汇合和一个具备浏览范畴的角色,因而具备此角色的用户能够查看产品。
Prepare the approuter files
approuter 将使您可能创立到您的应用程序的平安路由。
- 将名为 approuter 的文件夹增加到您的产品列表文件夹中。
- 在该文件夹中创立一个名为 package.json 的文件。
- 增加以下内容:
{ | |
"name": "approuter", | |
"dependencies": {"@sap/approuter": "^9.0.2"}, | |
"scripts": {"start": "node node_modules/@sap/approuter/approuter.js"} | |
} |
xs-app.json 的内容:
{ | |
"routes": [{ | |
"source": "^/products", | |
"target": "/", | |
"destination": "products-destination" | |
}] | |
} |
这将创立一个名为 products-destination 的目的地。目的地稍后会在 manifest.yml 中援用。
Move static content to the application router
出于性能起因,最好将应用程序的图像放入带有应用程序路由器的动态资源文件夹中。
最初利用构造如下:
Update the manifest file
在清单文件中,您必须为利用程序定义主机名并增加目的地。清单文件用于将 XSUAA 服务实例绑定到您的应用程序。
applications: | |
# Application | |
- name: product-list | |
instances: 1 | |
memory: 128M | |
routes: | |
- route: product-list-jerry.cfapps.eu10.hana.ondemand.com | |
path: myapp | |
buildpacks: | |
- nodejs_buildpack | |
timeout: 180 | |
services: | |
- xsuaa-service-tutorial | |
# Application Router | |
- name: approuter | |
routes: | |
- route: approuter-product-list-jerry.cfapps.eu10.hana.ondemand.com | |
path: approuter | |
buildpacks: | |
- nodejs_buildpack | |
memory: 128M | |
services: | |
- xsuaa-service-tutorial | |
env: | |
destinations: > | |
[ | |
{"name":"products-destination", | |
"url":"https://product-list-jerry.cfapps.eu10.hana.ondemand.com", | |
"forwardAuthToken": true} | |
] |
Update the index.html file
因为您应用 /products 通过 approuter 调用产品列表,所以您须要在 index.html 文件中进行一些小的更改。
- 导航到 product-list/myapp/static 文件夹。
- 将 index.html 文件中的第 24 行替换为以下代码。
Create the XSUAA service instance
在部署应用程序之前,您须要为 XSUAA 创立服务实例。
- 应用 Cloud Foundry CLI 登录到您的 Cloud Foundry 帐户。
- 导航到产品列表文件夹。
- 应用 xs-security.json 平安描述符文件创建 XSUAA 服务实例。
cf create-service xsuaa application xsuaa-service-tutorial -c security/xs-security.json
最初 cf deploy 部署利用。
Call your application from its secure route
您的应用程序有两个在 manifest.yml 中定义的路由。应用程序的间接路由应该返回一条谬误音讯,指出未经受权(因为您还没有无效的 JWT)。通过 approuter 的平安路由重定向到登录屏幕。登录后,应用程序关上但显示音讯无数据。要查看产品数据,您须要为您的用户调配具备必要受权的角色汇合。
首先确保无奈通过其间接 URL 拜访您的应用程序:
https://product-list-ap25.cfa…
如果一切正常,这将导致读取未经受权的谬误音讯。
应用应用程序路由器的平安路由导航到您的应用程序:
https://approuter-product-lis…
输入您的试用帐户的电子邮件和明码。
您应该会看到 no data 音讯。这是因为您尚未调配查看产品的角色。您将在下一步中执行此操作。
Assign the role collection
为您的用户调配蕴含查看产品列表中产品所需角色的角色汇合。
- 关上 SAP BTP 主控室。导航到您的子帐户。
- 抉择平安选项卡,而后抉择信赖配置。抉择默认身份提供者。
- 输入您的电子邮件地址并抉择显示作业。
- 抉择调配角色汇合。抉择 ProductListViewer 角色汇合。
再次调用 approuter 的 URL(您之前可能必须删除您的 cookie/ 缓存)。
https://approuter-product-lis…
该应用程序当初将向您展现产品:
更多 Jerry 的原创文章,尽在:” 汪子熙 ”: