java - apiKeyRequired 在 Google Cloud Endpoints 项目中不起作用
问题描述
我有一个配置了 Cloud Endpoints Framework 的 java 8 项目。
我在这里遵循了文档:https ://cloud.google.com/endpoints/docs/frameworks/java/get-started-frameworks-java
我尝试使用 API 密钥保护 API。我在这里遵循了文档:https ://cloud.google.com/endpoints/docs/frameworks/java/restricting-api-access-with-api-keys-frameworks
问题是我总是可以访问端点,无论我是否设置了 API 密钥。
这是API:
@Api(
name = "myApi",
title = "My API",
version = "v1",
description = "My API description",
apiKeyRequired = AnnotationBoolean.TRUE
)
public class MyApiEndpoint {
@ApiMethod(httpMethod = GET, path = "list", apiKeyRequired = AnnotationBoolean.TRUE)
public ApiEntityList list() throws Exception {
return new ApiEntityList();
}
}
这是 web.xml:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<filter>
<filter-name>endpoints-api-controller</filter-name>
<filter-class>com.google.api.control.extensions.appengine.GoogleAppEngineControlFilter</filter-class>
<init-param>
<param-name>endpoints.projectId</param-name>
<param-value>${app.deploy.project}</param-value>
</init-param>
<init-param>
<param-name>endpoints.serviceName</param-name>
<param-value>${app.deploy.project}.appspot.com</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>endpoints-api-controller</filter-name>
<servlet-name>EndpointsServlet</servlet-name>
</filter-mapping>
<filter>
<filter-name>endpoints-api-configuration</filter-name>
<filter-class>com.google.api.control.ServiceManagementConfigFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>endpoints-api-configuration</filter-name>
<servlet-name>EndpointsServlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>EndpointsServlet</servlet-name>
<servlet-class>com.google.api.server.spi.EndpointsServlet</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value>com.myproject.MyApiEndpoint</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>EndpointsServlet</servlet-name>
<url-pattern>/_ah/api/*</url-pattern>
</servlet-mapping>
appengine-web.xml:
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<runtime>java8</runtime>
<threadsafe>true</threadsafe>
<service>core</service>
<url-stream-handler>urlfetch</url-stream-handler>
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
<env-variables>
<env-var name="ENDPOINTS_SERVICE_NAME" value="${app.deploy.project}.appspot.com" />
</env-variables>
</appengine-web-app>
我在 Google Cloud Platform 项目中创建了 API Key 作为新凭证,没有任何限制。
我可以在 GCP 上部署的 openapi.json 文件中看到以下行:
"/myApi/v1/list": {
"get": {
"operationId": "MyApiList",
"parameters": [ ],
"responses": {
"200": {
"description": "A successful response",
"schema": {
"$ref": "#/definitions/ApiEntityList"
}
}
},
"security": [
{
"api_key": [ ]
}
]
}
},
"securityDefinitions": {
"api_key": {
"type": "apiKey",
"name": "key",
"in": "query"
}
},
下面的所有电话都没有被拒绝,但我希望它们是:
- https://core-dot-gcp-project.appspot.com/_ah/api/myApi/v1/list
- https://core-dot-gcp-project.appspot.com/_ah/api/myApi/v1/list?key=FAKE_API_KEY
- 未配置 API 密钥的 API Explorer 调用
- 甚至来自本地服务器:http://localhost:8080/_ah/api/myApi/v1/list
看起来apiKeyRequired
注释参数没有任何效果。
我在这里想念什么吗?
解决方案
您确定您的 API 已启用吗?我的意思是,当您创建 Cloud Endpoints 项目时,它还会有效地将这些端点声明为“私有 API”。然后,您必须启用它才能使 API 密钥生效
- 在您的控制台中,输入“API 和服务”
- 单击“+ 启用 API 和服务”|| 侧边菜单中的“库”
- 在私有 API 的侧边菜单中单击“私有”
- 您应该(希望)能够看到您通过 Cloud Endpoints 创建的“API”
对于某些人来说,它似乎是自动完成的,但似乎有相当数量的人不是。老实说,我不知道为什么会这样。
希望有帮助!
推荐阅读
- javascript - JavaScript - 获取条目范围映射条目
- php - Laravel 选择列,然后按日期对列值进行分组
- flutter - 在文本 ios Flutter 末尾设置键盘光标
- python - Softmax 函数在神经网络中返回 nan 或全零
- c# - Hangfire Server:将业务代码更改部署到服务器 - 最佳实践
- c++ - CMake 找不到 GLEW(随 apt 安装)
- linux - Ghost博客重定向问题
- android - 主题时,BottomSheetDialogFragment 崩溃
- java - 使用高级 for 循环遍历 2 个列表
- python - Pandas Dataframe:为每个数据框行分配大于或小于值的条件值