简体   繁体   English

RESTful令牌认证澄清

[英]RESTful Token Authentication Clarification

I've read almost every answer on SO and some blog postings, but I can't figure out one simple thing. 我已经阅读了几乎所有关于SO和一些博客帖子的答案,但我无法弄清楚一件简单的事情。 In a simple token authentication scheme where the server generates a token and sends it back to the user after verifying credentials, how does the client store and then resend that token in each request? 在简单的令牌身份验证方案中,服务器生成令牌并在验证凭据后将其发送回用户,客户端如何在每个请求中存储然后重新发送该令牌? I have seen both cookie examples and header examples. 我看过两个cookie示例和标题示例。 I would like to use the HTTP Headers if possible, but I can't figure out the mechanics of how to send the token to the client, where it will sit, and then have it sent back in the header upon requesting a REST resource. 如果可能的话,我想使用HTTP Headers,但我无法弄清楚如何将令牌发送到客户端的机制,它将在哪里,然后在请求REST资源时将其发送回头部。

I am using Jersey/Guice with AngularJS on the front end. 我在前端使用Jersey / Guice和AngularJS。 Here are the resources I started with: 以下是我开始使用的资源:

http://porterhead.blogspot.co.uk/2013/01/writing-rest-services-in-java-part-6.html http://porterhead.blogspot.co.uk/2013/01/writing-rest-services-in-java-part-6.html

Session management : How to generate Authentication token for REST service ? 会话管理:如何为REST服务生成身份验证令牌? (Jersey) (新泽西州)

It depends on your needs. 这取决于您的需求。 You can use HTTP basic or digest auth, if it is appropriate for you. 如果适合您,您可以使用HTTP基本或摘要身份验证。 If not, then if you don't need a permanent storage, you can store credentials in memory. 如果没有,那么如果您不需要永久存储,则可以将凭证存储在内存中。 If you need a permanent storage, then you can store them in localstorage, or any other client side storage, but aware, that they are considered not secure . 如果您需要永久存储,则可以将它们存储在localstorage或任何其他客户端存储中,但要注意,它们被认为是不安全的

Anyways I think if your client or service is compromised somehow with xss, then you lost, and it does not matter what else you do about it. 无论如何,我认为如果您的客户或服务以某种方式与xss妥协,那么您就输了,并且您对此做了什么并不重要。 Otherwise you can send the credentials in plain text securely as long as you use HTTPS with proper settings. 否则,只要您使用具有适当设置的HTTPS,就可以安全地以纯文本形式发送凭据。 (But that's just an opinion, I am not a security expert, at least not in this topic.) So I think you should concentrate on not being xss vulnerable. (但这只是一个意见,我不是安全专家,至少不是这个主题。)所以我认为你应该专注于不被xss弱化。 For example you should use the proper headers and filter the input against js injection (and by firefox data URI injection). 例如,您应该使用正确的标头并针对js注入过滤输入(并通过firefox数据URI注入)。 And use TextNode in your client instead of innerHTML wherever it is possible. 并且尽可能在客户端使用TextNode而不是innerHTML。

例如,如果您使用的是Javascript,则可以将令牌存储在localstorage中,如客户端的window.localStorage [“token_id”]。

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

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