1.11. 测试应用程序
有关在开发模式 中测试应用程序的说明,请参阅前面的以 Dev 模式运行 应用程序。
您可以使用 curl 来测试在 JVM 或原生模式下运行的应用。
获取访问令牌
应用使用 bearer 令牌授权。要访问其资源,首先从 Keycloak 服务器获取访问令牌:
如果 quarkus.oidc.authentication.user-info-required 属性设置为 true,则应用要求访问令牌用于请求 UserInfo。在这种情况下,您必须将 scope=openid 查询参数添加到令牌授权请求中,例如:
前面的示例获取用户 alice 的访问令牌。
访问 /api/users/me 端点
具有有效访问令牌的任何用户可以访问 http://localhost:8080/api/users/me 端点,该端点会返回 JSON 有效负载,其中包含用户详情:
curl -v -X GET \ http://localhost:8080/api/users/me \ -H "Authorization: Bearer "$access_token
curl -v -X GET \
http://localhost:8080/api/users/me \
-H "Authorization: Bearer "$access_token
访问 /api/admin 端点
http://localhost:8080/api/admin 端点仅限于具有 admin 角色的用户。如果您尝试使用之前发布的访问令牌访问此端点,服务器会返回 403 Forbidden 响应:
curl -v -X GET \ http://localhost:8080/api/admin \ -H "Authorization: Bearer "$access_token
curl -v -X GET \
http://localhost:8080/api/admin \
-H "Authorization: Bearer "$access_token
获取管理员访问令牌
要访问 admin 端点,请为 admin 用户获取访问令牌: