我想使用 Spring Boot 实现 AWS Cognito 服务器端流程。我不太明白流程应该是什么样的。我应该与它一起使用 spring oauth 吗?
需求是这样的。
作为管理员创建用户并授予这些创建的用户访问权限以使用 API Gateway 中的 API(让我们忽略 API Gateway 部分并假设我们现在只需要来自 cognito 的访问令牌)
如果我将 AWS cognito 与 spring oauth2 一起使用,我认为应该发生以下情况
用户点击localhost:8000/oauth/token
- 具有基本身份验证(用户名和密码)
它将使用用户凭据进行 API 调用。用户收到令牌并根据需要使用它。
- 这个流程安全吗?我应该一起使用 spring oauth 吗?
- 如何处理
respond to auth challenge
?用户在调用我的应用程序 API 时是否应该首次传递新密码?
@RestController
public class Oauth {
@PostMapping(path = "/oauth/token")
public AdminInitiateAuthResult token(@RequestHeader("username") String username, @RequestHeader("password") String password) {
AWSCognitoIdentityProvider provider = AWSCognitoIdentityProviderClientBuilder
.standard()
.withRegion(Regions.US_WEST_2)
.withCredentials(new AWSStaticCredentialsProvider()).build();
Map<String, String> authParams = new HashMap<>();
authParams.put("USERNAME", username);
authParams.put("PASSWORD", password);
AdminInitiateAuthRequest adminInitiateAuthRequest = new AdminInitiateAuthRequest()
.withClientId("{client-id}")
.withUserPoolId("{user-pool-id}")
.withAuthFlow(AuthFlowType.ADMIN_USER_PASSWORD_AUTH)
.withAuthParameters(authParams);
AdminInitiateAuthResult authResult = provider.adminInitiateAuth(adminInitiateAuthRequest);
return authResult.getAuthenticationResult().getIdToken();
}
}
业务需求非常简单,需要有一个用户池(在本例中为 cognito),他们可以获得某种令牌来访问少数 API。我想使用 Spring Boot 来实现这一点,因为 API 是使用 Spring Boot 编写的,而且我还使用 AWS Api Gateway
我应该与它一起使用 spring oauth 吗?
不需要。授权是通过 API Gateway 完成的。
API 客户端在使用 API 之前需要从 Cognito 获取令牌(即在那里进行身份验证)。无需在应用程序(Spring)方面执行任何操作。
详细信息是here https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html.
如果您想使用 Cognito 实现 API 客户端的身份验证,请参阅 Cognito 文档以获取示例和手册。
FYI 应用程序负载均衡器 https://docs.aws.amazon.com/elasticloadbalancing/latest/application/listener-authenticate-users.html#authentication-flow可用于处理 API 的所有身份验证流程。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)