【问题标题】:Restricting my endpoints with API Key in java在 java 中使用 API 密钥限制我的端点
【发布时间】:2019-01-31 00:08:50
【问题描述】:

我正在使用适用于 java 的云端点 v2。我的问题是任何人都可以通过 API Explorer 或直接从某人知道的 URL 访问这些端点方法。我想保护我的端点。我阅读了如何使用 API KEY 限制整个 API 或某些方法的文档。 Restricting API Access with API Keys

这是我正在尝试的。

@Api(
        name = "zeem",
        version = "v1"
)

public class Account {
@ApiMethod(name = "getRegistration", path = "getRegistration", apiKeyRequired = AnnotationBoolean.TRUE)
public Registered getRegistration(@Named("phone") Long phone){
  // code ....
}

我可以在没有任何 API 密钥的情况下运行此方法,并且它可以成功运行。
即使我尝试直接从 url 访问此方法,它也可以工作。

http://localhost:8080/_ah/api/zeem/v1/getRegistration?phone=123 // Successfully getting response

你能告诉我我做错了什么吗?有什么我想念的吗?

更新-OpenAPI 文档

是的,我正在添加 API 管理这里是 openapi.json 这个函数的样子。

    "/zeem/v1/getRegistration": {
   "get": {
    "operationId": "ZeemGetRegistration",
    "parameters": [
     {
      "name": "phone",
      "in": "query",
      "required": true,
      "type": "integer",
      "format": "int64"
     }
    ],
    "responses": {
     "200": {
      "description": "A successful response",
      "schema": {
       "$ref": "#/definitions/Registered"
      }
     }
    },
    "security": [
     {
      "api_key": [ ]
     }
    ]
   }
  },

这是控制台的外观。

我错过了什么?

更新:Web.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- [START_EXCLUDE] -->
<!--
  Copyright 2016 Google Inc.
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
        http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- [END_EXCLUDE] -->
<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">

  <welcome-file-list>
    <welcome-file>welcome</welcome-file>
  </welcome-file-list>

  <!-- OBJECTIFY -->
  <filter>
    <filter-name>ObjectifyFilter</filter-name>
    <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
  </filter>
    <filter-mapping>
        <filter-name>ObjectifyFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

  <!-- ENDPOINTS -->
  <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>
                org.octabyte.zeem.API.Account,
                org.octabyte.zeem.API.CommentApi,
                org.octabyte.zeem.API.FriendApi,
                org.octabyte.zeem.API.ListApi,
                org.octabyte.zeem.API.PostApi,
                org.octabyte.zeem.API.SearchApi,
                org.octabyte.zeem.API.UserApi,
                org.octabyte.zeem.API.StoryApi
            </param-value>
        </init-param>
    </servlet>
    <!-- Route API method requests to the backend. -->
    <servlet-mapping>
        <servlet-name>EndpointsServlet</servlet-name>
        <url-pattern>/_ah/api/*</url-pattern>
    </servlet-mapping>


    <!-- Security -->
    <security-role>
        <role-name>admin</role-name>
    </security-role>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>admin</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>admin</role-name>
        </auth-constraint>
    </security-constraint>

</web-app>

【问题讨论】:

  • 是的,我添加了,请检查更新
  • 请同时发布您的web.xml
  • 是的,我添加了,请检查更新

标签: java google-app-engine google-cloud-platform google-cloud-endpoints-v2


【解决方案1】:

对什么是 API 密钥访问限制存在误解。 API key restriction 用于限制密钥可以访问的API,它不以任何形式或形式处理用户的身份验证。

有几种方法可以对 Endpoints 的用户进行身份验证,例如,您可以使用 API management 或者您可以考虑使用 OpenAPI

【讨论】:

  • 是的,我添加了 API 管理和 OpenAPI 文档 请检查更新。但是我仍然可以在没有任何 api 密钥的情况下访问端点方法
  • 我对你想要什么有点困惑,所以我会给你两个选择。如果您担心未经身份验证的用户可以访问您的端点,您需要this;如果您担心您的端点可能会被互联网随机命中(保护端点免受随机匿名意外流量的影响),那么您需要block all access without API Key
  • 我想要第二个选项。但正如你在我的问题中看到的那样,我正在尝试这个,但它不起作用。我想知道为什么它不起作用。我错过了什么?
  • 好的,我明白你的意思了。所以你有一种受项目 API 密钥限制的 API 方法(一些随机的互联网流量无法访问它)。您是否尝试过实际部署它以在生产中对其进行测试(可能是测试版本)?此外,您可以点击 URL,但它是否返回有意义的数据(就像它让您实际运行代码一样)?如果是这样,那么您似乎可以向我们的issue tracker 报告这个问题,以便我们可以从头到尾进行调查,谢谢。
  • 是的,我在实际部署中尝试过。同样的问题。我在这里发布了这个问题issuetracker.google.com/issues/113347216
猜你喜欢
  • 2021-12-09
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
  • 2019-10-18
  • 2020-08-06
  • 2016-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多