13

我需要为使用 JAX-RS 标准实现的 REST API 提供授权和身份验证,这些 API 旨在从移动客户端和某些设备中使用。我有多个具有唯一设备标识的设备,可以发布一些数据。移动客户端只是使用 GET 请求来显示该数据。我更关心 POST 部分,我想在其中对客户端进行身份验证。另外,我想保持简单。我正在考虑通过 HTTPS 使用简单的 HTTP 基本授权和 API 密钥。我的问题是如何生成此 API 密钥?

4

4 回答 4

12

You could take a look into Shiro: http://shiro.apache.org It is a very nice framework to "secure" APIs (authorization, authentication and other things for security). You can implement a "basic authentication" to "login" your users (via a username/password), then provide them with an API key, which you can use to perform a "bearer token authentication" to allow them to access the resources of your API. To do that you would define what shiro calls "filters", which are defined over API resources... this is defined in a "shiro.ini" as the following:

[main]
authcBasicRealm = com.yourapp.shiro.UserAuthenticatorRealm
tokenValidatorFilter = com.yourapp.shiro.BearerAuthenticationTokenFilter
tokenValidatorRealm = com.yourapp.shiro.BearerAuthenticationTokenRealm

[urls]
/rest/hello/login/** = ssl[8443], noSessionCreation, authcBasic
/rest/hello/hello = ssl[8443], noSessionCreation, tokenValidatorFilter

You need to implement/extend some of the Shiro default filters to make them work with your DB to get your user authentication data, etc. The nice thing is that they provide many tools to handle security issues, e.g.: to generate API keys, to salt and encrypt, etc. Take a look on their tutorials, they are in general very good.

There are other frameworks, namely Java EE has support for security and also Spring provides support for security. Take a look at this very nice presentation by Mat Raible where he presents and demos these three frameworks: http://www.slideshare.net/mraible/java-web-application-security-denver-jug-2013

于 2013-07-30T14:51:29.393 回答
2

您可以为此使用UUID 。UUID 如下所示:

550e8400-e29b-41d4-a716-446655440000

每种编程语言都有生成 UUID 的库。

于 2013-07-31T06:52:10.453 回答
0

我也在研究这个问题,如果我想依赖 JAX-RS 标准并保持应用程序“纯”,我会使用容器附带的默认身份验证系统(通常是 BASIC 身份验证)。

这意味着容器需要像 Java EE 应用程序那样执行认证和课程级别授权,这些应用程序遵循标准而不是构建变通方法(即使用 Shiro)。

但是,如果您想使用 API 令牌等概念,同时让您的应用程序免于实现身份验证系统,则需要在其他地方(即容器)实现该工作。

不幸的是,基于容器的身份验证必须是特定于容器的。JAAS 没有描述标准容器 API 来执行身份验证领域。甚至他们的教程http://docs.oracle.com/javaee/6/tutorial/doc/bnbxj.html也谈到了 Glassfish 的特定配置。

如果您的组织足够大,DataPower 还支持 OAuth http://www.ibm.com/developerworks/websphere/library/techarticles/1208_rasmussen/1208_rasmussen.html,因此您可以使用它来管理身份验证并将正确的凭据传递给你的申请。然而。你仍然需要做一些特定于供应商的事情。

虽然这不是一件坏事,但我宁愿采用这种方法,也不愿用自己的身份验证系统污染应用程序,从而在身份验证系统发生变化时使事情变得不灵活。例如,SonarQube 有自己的身份验证系统,不支持客户端证书。

于 2014-01-11T05:34:07.653 回答
-2

找到了一些不错的文章,这些文章消除了我对 API 密钥生成和使用它们进行身份验证的疑问:

http://restcookbook.com/Basics/loggingin/ http://www.smartjava.org/content/protect-rest-service-using-hmac-play-20

于 2013-08-04T13:45:52.233 回答