gpt4 book ai didi

java - AngularJS - Java EE REST 安全性

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:25:02 25 4
gpt4 key购买 nike

我在 Java EE 7 后端上使用 AngularJS 和 REST API。该项目部署在 Wildfly 应用服务器上,我对证券评级有一些疑问:

  1. 为了加密/解密数据,我使用 CryptoJS 在服务器端 (Java) 加密和解密,但显然我们必须以明文形式发送密码,密码和盐仅被加密。我的问题是为什么密码是明文?它应该是 secret 的,然后也加密,不是吗?

  2. 对于 REST API,Java EE 7、HTTP 安全 header (basic-auth) 使用的标准是什么? Json 访问 token ?以及它是如何工作的,在 cookie 上将用户 session / token 存储在哪里?我只想知道如何使用 Angular 来完成。

  3. 也许我可以使用具有基于表单的身份验证的经典 JAAS,然后在服务器端使用 request.login() 进行身份验证,然后我的 EJB 将全部受到@Role 的保护。

  4. 在 AngularJS 中保护页面的方法是什么?目前我正在使用 web.xml 并放置 URL 模式,也许有更好的方法?

我已经找到了很多这样的例子:

AngularJs and Jboss and JAAS (全方位安全)

how to integrate angularjs and java jaas based authentication?

一些用户提到了这一点:

* index.html page should contain token inside html to avoid CSRF
* token shouldn't be stored in a cookie storage
* Each request should be signed with header param
* Server should validate every request by passed header
* If cookie usage is a must you should validate referer in order to prevent CSRF

但是没有具体的例子说明如何实现这个,尤其是CSRF。

最佳答案

To Encrypt/decrypt data I am using CryptoJS to encrypt and decrypt on server side ( Java ) but apparently we have to send the passphrase in clear, the cipher and salt are only encrypted. My question is why the passphrase is clear text ? it should be secret and then encrypted as well no ?

一旦您以明文形式发送 key (密码?) - 加密就没有用了。

要实现合理的客户端-服务器安全,请使用 HTTPS。简单、有效且更安全。通常,在 Web 应用程序端加密不是一个好主意,因为用户或“中间人”可以检索或修改 key 和数据。

不同的情况是端到端安全性,当客户端加密、发布加密数据并按原样存储/处理它们时,加密 key 仅对用户可用。如果不是这种情况,并且服务需要数据以进行进一步操作,则 HTTPS 是可行的方法。

For the REST API, what is the standard to use for Java EE 7, HTTP security header (basic-auth) ? Json Access token ? and how it really works, where to store user session/token, on a cookie ? I just want to know how to do it with Angular.

有效地列出了您的选项。这是你的决定。每个选项都有其优点和缺点。基本上 - 如果您谈论的是 (REST) 服务,那么使用什么技术应该无关紧要。

对于直接从浏览器调用的 REST 服务,我将省略基本身份验证(否则用户会弹出身份验证窗口)

您可以使用 JWT token (由应用程序 secret 签名,只需添加一些到期日期),但您无法“注销”用户,只能等到 token 到期。优点是, token 是“自给自足”的,您无需担心 session 管理。客户端在 Authorization HTTP header 中发送 JWT token ,您只需对其进行解码、验证,然后您就可以从 token 中获取身份。

另一个选项是 session token (cookie 或作为授权 header 发送),您需要在其中管理 session (存储 token ,在注销时清除 token ,...)。使用应用程序服务器 session cookie 会使您的服务无法被其他应用程序使用(仍然是一个问题 - 您是否希望/需要第三方可重用服务),但您实现了内置授权(JAAS、 Angular 色等)。

Maybe I can use the classic JAAS with form-based authentication and then having request.login() on server side to be authenticated then my EJB will be all protected by @Role.

确实,这是一种验证和授权用户并颁发 token (jwt、cookie 等...)的方式。

What is the way to protect pages in AngularJS ? For the moment I am using the web.xml and putting the URL patterns, maybe there is a better way ?

默认的web授权应该就可以了。

仍然 - 保持简单。根据我的经验,静态资源(网页、图像、脚本、css)应该是静态的,它们是否公开可用并不重要。重要的是执行(操作、数据...)作为服务公开,这就是您进行适当身份验证和授权的地方。

玩得开心

关于java - AngularJS - Java EE REST 安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30864398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com