CSRF保护刷新令牌的Cookie在SPA [英] CSRF Protection for Refresh Token Cookie in SPA

查看:894
本文介绍了CSRF保护刷新令牌的Cookie在SPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了AngularJS SPA资源所有者密码凭据的OAuth 2.0流。有几篇文章(这里,的这里 ..)和这个问题的解释,答案我们不应该刷新令牌存储(网络)客户端(localStorage的),但其存储在一个仅Http的Cookie加密,并使用了代理API,我们实现了refreh令牌的解密转发给安全令牌服务。

I am using the Resource Owner Password Credentials OAuth 2.0 flow in a AngularJS SPA. There are several articles (here, here..) and the answer to this question that explain that we should not store refresh tokens on the (web) client (LocalStorage), but store them encrypted in an HttpOnly Cookie and use a proxy API where we implement the decryption of the refreh token to forward it to the security token service.

大多数文章给,我们应该通过共同保护机制之一关心CSRF的提示。我不知道什么是在一个单页应用的最佳解决方案。

Most articles give a hint that we should care about CSRF by using one of the common protection mechanisms. I'm wondering what's the best solution in a Single Page Application.

该角<一个href=\"https://docs.angularjs.org/api/ng/service/$http#cross-site-request-forgery-xsrf-protection\">$http参考解释了默认的机制,我们应该如何应对CSRF:服务器设置一个名为cookie的 XSRF-TOKEN 。此cookie必须的Javascript可读的,这样我们就可以将 X-XSRF-TOKEN HTTP在我们的请求头。这是机制足以保护refreh令牌的情况?

The Angular $http reference explains the default mechanism how we should counter CSRF: The server has to set a cookie called XSRF-TOKEN. This cookie has to be Javascript readable, so that we can set the X-XSRF-TOKEN HTTP header in our requests. Is this mechanism sufficient to protect the refreh token scenario?


  1. 启动应用程序的第一次。没有访问令牌,也可用饼干,我们有一个用户名和密码登录。 API /登录给了我们,我们在内存中保留一个访问令牌,并设置两个cookie。中HTTPOnly refreh令牌的cookie,以及JS读 XSRF-TOKEN 的cookie。

  1. Start the application the first time. No access token nor cookie available, we have to login with username and password. api/login gives us an access token that we keep in memory and sets two cookies. The HttpOnly refreh token cookie, and the JS readable XSRF-TOKEN cookie.

访问令牌到期。调用到 API /令牌只会验证 XSRF-TOKEN ,并使用该令牌cookie来返回一个新的访问令牌;设置一个新的刷新的Cookie

The access token expires. A call to api/token validates XSRF-TOKEN and uses the token cookie to return a new access token; sets a new refresh cookie

重新启动从应用程序缓存应用程序。在内存中,但可用的饼干没有访问令牌。使用 API /令牌 ...

Restart the application from AppCache. No access token in memory but cookies available. Use api/token...

坏人想偷我们的refreh的cookie。 ppared网页A $ P $使得请求 API /令牌与我们的饼干,但没有 X-XSRF-TOKEN HTTP头。

Bad guy wants to steal our refreh cookie. A prepared page makes request to api/token with our cookies, but no X-XSRF-TOKEN HTTP header.

任何严重的安全问题?

推荐答案

据我知道要做到这一点,最好的办法是,当服务器与CSFR呈现令牌里面的index.html,之后你作为标准AngularJS SPA。因此, index.html的然后与后端服务/框架生成令牌CSFR丰富。 SpringSecurity为此提供了注入令牌模板很好的支持。

As far as I know the best way to do this is when server renders index.html with the CSFR token inside and after that you function as standard AngularJS SPA. So the index.html is then enriched with CSFR token generated by backend service / framework. SpringSecurity provides nice support for this injecting tokens to the templates.

在这之后,你可以从模板中的令牌使用JavaScript和使用 httpInterceptor <将其设置为标题所有的 $ HTTP 请求/ code>的要求挂钩。 (或饼干)?我记不太清楚了什么正确的方式,但我相信它在你上面提到的文章)被描述

After that, you can get the token from the template with javascript and set it to all your $http requests in headers by using httpInterceptor's, request hook. (or cookie)? I don't remember clearly whats the proper way but I am sure it is described in articles you mentioned above)

这篇关于CSRF保护刷新令牌的Cookie在SPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆