简体   繁体   English

使用令牌身份验证创建restapi

[英]create restapi with token authentication

I'm trying to create RestApi first time. 我正在尝试第一次创建RestApi。 Looking for assistance after reading text present online. 阅读在线文本后寻求帮助。

My requirement is, I want to create an rest api which will be having username and password. 我的要求是,我想创建一个将具有用户名和密码的rest api。 Password will be in encrypted format. 密码将采用加密格式。 So when this api client will request to a web server, that password will be decrypted first on server side and then if the user name and password authenticates then it will send back a temporary token with expiry date. 因此,当此api客户端向网络服务器请求时,该密码将首先在服务器端解密,然后,如果用户名和密码进行了身份验证,则它将发送回带有到期日期的临时令牌。 Then again that token will be used in rest api to request data from the web server in xml format. 然后,该令牌将再次在rest api中使用,以xml格式从Web服务器请求数据。

How can we achieve this ? 我们怎样才能做到这一点?

And I also want to understand if we encrypt a password in client server then how its get decrypts on web server side. 而且我还想了解我们是否在客户端服务器中对密码进行加密,然后如何在Web服务器端对其进行解密。 Is the same instance travels from client to web server side while making request ? 发出请求时,同一实例是否从客户端传播到Web服务器端?

Second, The data which I'm trying to consume from web server are the email ids of users which registers on website. 其次,我要从Web服务器使用的数据是在网站上注册的用户的电子邮件ID。 My question, If user is getting registered on website then website must be storing those email ids somewhere like in database right ? 我的问题是,如果用户正在网站上注册,则网站必须将这些电子邮件ID存储在数据库中的某个位置,对吗? And my restApi will be accessing the code on web-server side which is responsible to get email ids from database in xml format. 我的restApi将在Web服务器端访问代码,该代码负责以xml格式从数据库获取电子邮件ID。 Is my understanding correct ? 我的理解正确吗?

First, don't concern yourself directly with encrypting details client-side and decrypting them server-side. 首先,不要直接在客户端加密细节并在服务器端解密细节。 If you are using TLS/HTTPS (which you should be ) then all is well, everything is already encrypted. 如果您正在使用TLS / HTTPS( 应该使用 ),那么一切都很好,所有内容均已加密。

The token generation is slightly more difficult but still easy enough. 令牌生成稍微困难一些,但仍然很容易。 A commonly used and simple to implement method is to use JWT tokens. 一种常用且易于实现的方法是使用JWT令牌。 The general idea is that you create a JSON object like the following: 通常的想法是,您创建一个如下所示的JSON对象:

{ "userID": "FC5A47CC", "expiry": "12/10/2017" }

And then run it through an HMAC using a key only your server knows. 然后使用仅服务器知道的密钥通过HMAC运行它。 You append the result of the HMAC to the JSON object using base64 encoding and then send this to your client after they have logged in. 您可以使用base64编码将HMAC的结果附加到JSON对象,然后在他们登录后将其发送给客户端。

Using this method, authentication is very fast, as your web server won't need to make any requests to your database server to determine if the token is valid. 使用此方法,身份验证非常快,因为您的Web服务器不需要向数据库服务器发出任何请求来确定令牌是否有效。 You can read more about JWT here . 您可以在此处阅读有关JWT的更多信息。 I've answered a similar question in more detail here . 我在这里已经详细回答了类似的问题。

As your question asks, these userIDs will obviously need to be stored in your database. 正如您的问题所要求的那样,这些用户ID显然需要存储在数据库中。

Seems like you want client app to consume resources on behalf of user. 似乎您希望客户端应用程序代表用户消耗资源。 I propose OAuth 2.0, which provides mechanism, which you have described to access protected resources without storing passwords. 我建议使用OAuth 2.0,它提供了一种机制,您已经描述了该机制可以访问受保护的资源而不存储密码。 Client app requests a username and password from the user (for example by using a login form) and then send that credentials to the server. 客户端应用程序向用户请求用户名和密码(例如,通过使用登录表单),然后将该凭据发送到服务器。 Upon receipt and validation server returns token to the client. 收到并验证服务器后,令牌将其返回给客户端。 Client stores token locally and discards username and password. 客户端将令牌存储在本地,并丢弃用户名和密码。 All subsequent request are authorized by token, which can be accomplished using a custom HTTP header, for example X-Auth-Token. 所有后续请求均由令牌授权,这可以使用自定义HTTP标头(例如X-Auth-Token)完成。 Server can optionally provide a refresh token along with the access token, which is used by client to obtain new access token, once current expires. 服务器可以选择提供刷新令牌以及访问令牌,一旦电流到期,客户端将使用刷新令牌来获取新的访问令牌。 HTTPS/SSL technology is required by OAuth 2.0, so data over wire will be encrypted. OAuth 2.0需要HTTPS / SSL技术,因此有线数据将被加密。

There are 4 roles defined by OAuth 2.0 : OAuth 2.0定义了4个角色:

1) Authorization Server — does identity verification and grants token to the client app. 1)授权服务器-执行身份验证并将令牌授予客户端应用。 2) Resource Server — Server which hosts actual protected user resource. 2)资源服务器-托管实际受保护用户资源的服务器。 3) Resource Owner — User willing to provide access to his protected resource. 3)资源所有者-用户愿意提供对其受保护资源的访问。 4) Client — application that gets access to a user's resources. 4)客户端-可以访问用户资源的应用程序。

You can use Spring Security OAuth framework to implement this requirement. 您可以使用Spring Security OAuth框架来实现此要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM