gpt4 book ai didi

java - 将 Cognito 身份验证与弹性负载均衡器结合使用

转载 作者:行者123 更新时间:2023-12-01 19:15:58 25 4
gpt4 key购买 nike

我有一个 ReactJS 前端网站,它使用 AWS Cognito 进行身份验证,工作正常,我可以登录、注册、退出等。我的后端是一组在 AWS Fargate 中的 Docker 容器中运行的 Java Web 服务全部都在弹性负载均衡器后面。

我注意到,在 ELB 中,您可以向监听器添加身份验证,监听器将检查 jwt token 的 HTTP header 并在转发到相关微服务之前对其进行身份验证。我遇到了一个问题,当我设置“验证...”规则时,它返回一个错误,我需要添加客户端 key 。我无法将客户端 key 添加到我的 Cognito 设置中,因为我从 ReactJS 访问它,并且显然 Javascript 无法与添加了客户端 key 的 Cognito 一起使用。

因此,如果我无法使用此方法,那么当 HTTPS 请求到达我的 Java 微服务时,我需要某种方法对其进行身份验证。从我的 Java 服务中,我感觉我需要以某种方式访问​​ AWS Cognito 来检查用户 session ,但这感觉不对。

有什么想法应该如何运作吗?

谢谢

最佳答案

您可以考虑使用纯JS通过AWS Cognito进行身份验证,无需客户端 key (这是可选的)。我确实创建了一个没有 Client SecretApp Client 并且它确实有效。

引用:https://docs.aws.amazon.com/cognito/latest/developerguide/authentication.html

// Amazon Cognito creates a session which includes the id, access, and refresh tokens of an authenticated user.

var authenticationData = {
Username : 'username',
Password : 'password',
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var poolData = {
UserPoolId : 'us-east-1_ExaMPle',
ClientId : '1example23456789'
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username : 'username',
Pool : userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
var accessToken = result.getAccessToken().getJwtToken();

/* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer */
var idToken = result.idToken.jwtToken;
},

onFailure: function(err) {
alert(err);
},

});

此外,由于您使用的是 ReactJS,您可以尝试 Amplify:

https://aws-amplify.github.io/docs/js/authentication

我知道,虽然您有很多层,有时您想根据需要进行配置,但您可以尝试使用 AWS Lambda 为自己创建一个简单的身份验证函数。

关于java - 将 Cognito 身份验证与弹性负载均衡器结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59417140/

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