简体   繁体   English

以角度存储 JWT 令牌的位置?

[英]Where to store JWT token in angular?

I am building an app with Django and angular.我正在用 Django 和 angular 构建一个应用程序。 Currently, I am storing a JWT issued by the backend on local storage.目前,我正在将后端发布的 JWT 存储在本地存储上。 But, I am concerned about XSS attacks.但是,我担心 XSS 攻击。 Should I store the token using HTTP only cookie?我应该使用仅 HTTP cookie 存储令牌吗? I am also thinking of storing the token in my auth service class in a variable field.我也在考虑将令牌存储在我的身份验证服务类中的变量字段中。 But I am not entirely sure if angular shares the service across the entire app.但我不完全确定 angular 是否在整个应用程序中共享服务。 Will there be a single instance of my auth service?我的身份验证服务会有一个实例吗?

Assuming you are using standard dependency injection, a new instance of your service is instantiated each time, so therefore a field in the service class will not be stored.假设您使用标准依赖注入,每次都会实例化您的服务的一个新实例,因此不会存储服务类中的字段。

Session or local storage is fine though.会话或本地存储虽然很好。 The JWT mechanism prevents the contents from being altered easily by a client (as you must be verifying it on your downstream backend services). JWT 机制可防止客户端轻易更改内容(因为您必须在下游后端服务上对其进行验证)。

Conceivably you could retain some original request signature in the JWT payload, and check any secondary requests under this match the same.可以想象,您可以在 JWT 负载中保留一些原始请求签名,并检查此匹配项下的任何辅助请求。 For example, IP address, user agent string etc.例如,IP 地址、用户代理字符串等。

Personally (provided it is implemented correctly), I consider this more than enough security for the majority of web facing applications.就个人而言(如果它被正确实现),我认为这对于大多数面向 Web 的应用程序来说已经足够安全了。 Obviously banking / finance apps may wish to go the extra mile, with 2 factor authentication, etc.显然,银行/金融应用程序可能希望加倍努力,使用 2 因素身份验证等。

Let get things straight:让事情直截了当:

If you got XSS'd - it's game over.如果您遇到了 XSS - 游戏就结束了。 period.时期。

That said,那说,

one approach that passed external Penetration Tests in my current workplace would be placing JWT token in an httpOnly secure samesite=strict cookie.在我当前的工作场所通过外部Penetration Tests一种方法是将JWT令牌放在httpOnly secure samesite=strict cookie 中。

To further prevent CSRF you can add an identifier in the web storage, attach it to every xhr as a header.为了进一步防止 CSRF,您可以在 Web 存储中添加一个标识符,将其作为标题附加到每个xhr

In the server, extract the identifier from the jwt and compare it with the header value.在服务器中,从jwt提取标识符并将其与标头值进行比较。

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

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