简体   繁体   English

使用会话检查客户端是否在REST API中通过了身份验证

[英]Using the session to check if client is authenticated in a REST API

I'm implementing a REST API with authentication. 我正在实现带有身份验证的REST API。 I was wondering which one of these two authentication solutions make more sense: 我想知道这两种身份验证解决方案中的哪一种更有意义:

1) Having each methods doing authentication, every time they are called 1)每次调用时都要让每种方法进行身份验证

Input api /endpoint/retrieveshoes 输入api / endpoint / retrieveshoes

{
    "username":"gingo",
    "password":"124",
    "shoes_type":"A"
}

2) Having a separated login method and using the session for the other methods to make sure the client is authenticated. 2)使用单独的登录方法,并将会话用于其他方法,以确保对客户端进行身份验证。

Input api /endpoint/login 输入api / endpoint / login

{
    "username":"gingo",
    "password":"124"
}

Input api /endpoint/retrieveshoes 输入api / endpoint / retrieveshoes

{
    "username":"gingo"
}

If I call retrieveshoes without first doing the login, I will receive the message "Invalid session". 如果我没有先登录就呼叫检索鞋,我将收到消息“无效会话”。

In the solution (1) I will have instead to repeat the authentication every time I call retrieveshoes. 在解决方案(1)中,每当我调用检索鞋时,我将不得不重复进行身份验证。

But I was wondering if using the session like in the case (2) is safe, even if I use HTTPS. 但是我想知道,即使使用HTTPS,使用情况(2)一样的会话是否安全? Which one of the two solutions is safe and effective? 两种解决方案中的哪一种是安全有效的? Do you know a third one? 你知道第三个吗?

Instead of default HTTP(S) sessions, you may want to consider using a authentication token that has a certain idle expiry time. 代替默认的HTTP(S)会话,您可能要考虑使用具有一定闲置到期时间的身份验证令牌。 This token can be generated by sending a request, lets say, to /endpoint/login with the username and password. 可以通过使用用户名和密码将请求发送至/endpoint/login来生成此令牌。 Then all subsequent requests can just send that token to authenticate. 然后,所有后续请求都可以仅发送该令牌进行身份验证。 To make it a tad bit more secure, you may want to bind the IP of the user to the token. 为了使其更加安全,您可能需要将用户的IP绑定到令牌。 Although, this has some drawbacks such as anyone on the same network with the same public address can use that token or if the user changes the network, he/she may need to login again. 尽管这样做有一些缺点,例如同一网络上具有相同公共地址的任何人都可以使用该令牌,或者如果用户更改了网络,则他/她可能需要再次登录。

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

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