【问题标题】:how to set header authentication in dropwizard如何在 dropwizard 中设置标头身份验证
【发布时间】:2016-03-03 08:18:27
【问题描述】:

我已经构建了一个小 api。 我想为我的 api 实现标头身份验证。 当用户想要访问我的 api 时,他会将他的 apikey 与 api url 一起发送。如果 apikey 在我的数据库中,那么他将被授予访问权限,否则他无法访问 api。

【问题讨论】:

标签: api authentication dropwizard


【解决方案1】:

如果你想验证一个方法 然后您可以直接在方法本身中查找标头。像

    @GET
    public Response processRequest(
            @HeaderParam("X-APIKEY")              
            @DefaultValue("INVALID_OR_GUEST_KEY") String apikey , ...) 

如果您想为您的所有(或大多数)方法建立此功能,那么最好使用过滤器。看看 http://www.dropwizard.io/0.9.2/docs/manual/core.html#jersey-filtershttp://www.dropwizard.io/0.9.2/docs/manual/core.html#servlet-filters 。 您可以在其中实现您的键查找逻辑

(PS 你可能还想考虑缓存它们,但那是另一回事了)。

【讨论】:

  • 感谢 Daniele,它工作正常。我们如何为API建立缓存来存储apikeys,因为每次访问数据库都没有效率。
  • 你好,看看番石榴缓存可能是这样的:github.com/google/guava/wiki/CachesExplained 加载缓存似乎正是你想要的。
  • 您可以使用 memcached (memcached.org) 或(特别是如果您的应用程序需要可扩展)hazelcast (hazelcast.org) 之类的东西。他们都非常好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2011-11-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-10
  • 2017-03-15
相关资源
最近更新 更多