Howto:如何AngularJS基本认证+ cookie来一个REST API服务器?基本、服务器、AngularJS、Howto

2023-09-13 04:19:03 作者:帥得不要不要的

我想处理基本身份验证与AngularJS的正确方法。我是pretty肯定的答案是SO地方,但我没有找到它。似乎每个人都有关于认证特别需要,我很困惑。请注意,我对认证没有特别的知识,为假人的问题很抱歉。

I'd like to handle Basic Auth with AngularJS the right way. I'm pretty sure the answer is somewhere on SO, but I didn't find it. It seems that everyone has particular need about authentication and I'm confused. Note that I have no particular knowledge about authentication so sorry for dummy questions.

案例

处理HTTP基本认证(服务器) Django的服务器上的REST API镶嵌在科尔多瓦/ PhoneGap的一个AngularJS应用程序中的智能手机中使用(客户端(S))在使用HTTP基本身份验证的客户端登录。它是无状态的,这意味着客户需要为每次调用API提供凭据,为每个Web服务。 A REST API on a Django server that handles HTTP Basic Auth (The server) An AngularJS application embedded in Cordova/Phonegap to be used in smartphones (The client(s)) The client logs in using HTTP basic auth. It's stateless, means the client need to provide credentials for each call to the API, for each web service.

什么为现在的作品

登录现在的工作是这样的:

Login works now this way:

用户看到一个登录页面,进入其电子邮件/密码和验证 angularJS客户端设置使用$的CookieStore 在一个cookie的凭据 angularJS客户端调用以$ HTTP的GET来 http://myapp.com/api/login 并与日HTTP头凭据(基本身份验证)如果该请求是成功的,用户登录视为之后,如cookie保存凭证,这些凭证是在每个Web服务调用的HTTP头发送。 User sees a login page, enters its email/pwd and validate angularJS client set the credentials in a cookie using $cookiestore angularJS client calls a GET with $http to http://myapp.com/api/login and with credentials in th HTTP header (basic authentication) If the request is a success, the user is considered logged. After that, as the cookie stores credentials, these credentials are sent in the HTTP Header at each web service call.

它按预期工作。

什么不对的

与该系统唯一的问题是,当智能手机应用程序(即在angularJS /科尔多瓦的应用程序)关闭,一切都失去了,用户需要重新登录。

The only problem with that system, is that when the smartphone application (ie. the angularJS/Cordova app) is closed, everything is lost and the user needs to log in again.

我怎么能解决这个问题?非常感谢。

How could I fix it? Thanks a lot.

推荐答案

实际上,似乎饼干(即使如果设置在将来的有效期限)由科尔多瓦删除当应用被关闭,并且现在有方式要解决这个问题。

Actually, it seems that cookies (even if the expiration date if set in the future) are deleted by Cordova when the application is closed, and there is now way to work around this.

然而,使用本地存储代替饼干完全解决问题。它基本上比cookie使用,因为它是一个键盘> value存储方式相同。这是更清洁。

However, using LocalStorage instead of cookies solves the problem perfectly. It's basically the same way to use than cookies since it's a key->value store. It's even cleaner.

本文是一个很好的介绍localStorage的。

This article is a good introduction to localStorage.

 
精彩推荐
图片推荐