我在 Google Cloud Endpoints Frameworks for App Engine (Java) 中有一个端点。端点仅限于需要这样的 API 密钥:
@ApiMethod(name = "echo", path = "echo", apiKeyRequired = AnnotationBoolean.TRUE, httpMethod = ApiMethod.HttpMethod.GET)
哪个有效。但是,如果我在调用时添加尾部斜杠,端点将返回没有 api 密钥要求的数据。
我试图在 api 定义中全局限制 api 访问,如下所示:
@Api(
name = "myapi",
version = "v1",
apiKeyRequired = AnnotationBoolean.TRUE,
然而,这似乎不起作用。我重新生成了 openapi.json 并重新部署了 openapi.js 和应用程序引擎应用程序,如果端点有斜杠,则仍然可以访问,但并非没有。
有谁知道我如何防止这种情况?任何见解都非常感谢。
我无法在 Google Endpoints 中解决这个问题,所以我使用了 tuckey 的 urlrewrite 来删除过滤器中的尾部斜杠
网页.xml
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>confPath</param-name>
<param-value>/WEB-INF/urlrewrite.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/_ah/api/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
urlrewrite.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.1//EN" "http://www.tuckey.org/res/dtds/urlrewrite3.1.dtd">
<urlrewrite>
<rule match-type="regex">
<note>Remove trailing slash</note>
<from>^(.*)/$</from>
<to type="redirect">$1</to>
</rule>
</urlrewrite>
更多信息:
http://www.tuckey.org/urlrewrite/manual/4.0/index.html
注意:到目前为止,它没有正确重定向。我正在继续努力,并将发布更新,但现在至少我得到了带有斜杠的版本的 404,而不是没有 api 密钥的响应数据,这满足了我目前的安全需求
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句