gpt4 book ai didi

oauth-2.0 - jhipster oauth2 客户端密码

转载 作者:行者123 更新时间:2023-12-04 22:59:03 25 4
gpt4 key购买 nike

我一直在试验 jhipster。
我已将我的应用程序配置为使用 oauth2。
为此,我的 application.yml 中有一个客户端 secret

根据我在此主题上找到的几篇文章,客户端 secret 应始终保密。例如,检查 https://aaronparecki.com/articles/2012/07/29/1/oauth2-simplified

The client secret must be kept confidential. If a deployed app cannot keep the secret confidential, such as Javascript or native apps, then the secret is not used.



我注意到虽然生成的 auth.oauth2.service.js 包含纯文本的 secret :

        return {
login: function(credentials) {
var data = "username=" + credentials.username + "&password="
+ credentials.password + "&grant_type=password&scope=read%20write&" +
"client_secret=mySecretOAuthSecret&client_id=myapp";
return $http.post('oauth/token', data, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
"Authorization": "Basic " + Base64.encode("myapp" + ':' + "mySecretOAuthSecret")
}
}).success(function (response) {
var expiredAt = new Date();
expiredAt.setSeconds(expiredAt.getSeconds() + response.expires_in);
response.expires_at = expiredAt.getTime();
localStorageService.set('token', response);
return response;
});
},


我知道在缩小的 javascript 中找到它会有点困难,但是任何寻找“client_secret”的人都会很快得到返回。

我错过了什么吗?还是 jHipster oauth 实现不安全?

谢谢,
安迪

最佳答案

由于像 jhipster 这样的 JS 客户端无法保留客户端 secret “ secret ”,因此使用客户端 secret 完全没有意义。 jhipster 使用的 OAuth2 资源所有者密码凭据授予流程适用于非常受信任的客户端——jhipster 的客户端就是这样。它允许您跳过正常的“授权”端点并直接转到“ token ”端点以使用您的用户凭据获取您的 token 。如果您的 Spring 授权服务器 (AS) 定义了客户端 secret ,则您需要从客户端 JS 传递该 secret 。但是,如果您从 AS 中的内存中客户端设置中删除 secret 定义(例如,在 OAuth2ServerConfiguration.java 中注释掉该行),您可以在 JS 中完全忽略它(见下文)

return {
login: function(credentials) {
var data = "username=" + credentials.username + "&password=" + credentials.password + "&grant_type=password&scope=read%20write&";
return $http.post('oauth/token', data, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
"Authorization": "Basic " + Base64.encode("myapp" + ':' + "")
}
}).success(function (response) {
var expiredAt = new Date();
expiredAt.setSeconds(expiredAt.getSeconds() + response.expires_in);
response.expires_at = expiredAt.getTime();
localStorageService.set('token', response);
return response;
});
},


删除您的客户端 secret 后,我认为您的应用程序并没有真正更安全,但感觉更干净和诚实——因为您承认使用仅使用 JS 的客户端,您只能如此安全。对于 JS 和 native 客户端,通常使用隐式流,它不会打扰客户端 secret 。它是从更强大的授权代码授予流程简化而来的,因为使用 JS 或 native 客户端无法保密。

无论如何,jhipster 可能不应该在 JS 源代码中包含客户端 secret ,但我认为它不会造成任何伤害(因为唯一的选择是拥有一个不再安全的空白客户端 secret )。你不是不安全的(因为规范允许这种事情)但是你会更安全地使用授权代码流(这需要在 jhipster 实现中做一些工作)或者让轻服务器代理添加客户端 -对“ token ”端点的请求的 secret ,而不是直接来自 JS。服务器到服务器的通信(例如通过代理)使浏览器无法看到 secret 。

看到这篇文章很好地讨论了带有 oauth2 的纯 JS 客户端的陷阱: http://alexbilbie.com/2014/11/oauth-and-javascript/

这是将 oauth2 与 angularjs 和 spring 通过代理一起使用的示例: https://spring.io/blog/2015/02/03/sso-with-oauth2-angular-js-and-spring-security-part-v

关于oauth-2.0 - jhipster oauth2 客户端密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32038904/

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