gpt4 book ai didi

quarkus - 在 Quarkus 中使用自定义身份提供商

转载 作者:行者123 更新时间:2023-12-03 08:00:03 26 4
gpt4 key购买 nike

在我当前的项目中,我们将用户登录信息存储在 MongoDB 集合中。我们希望实现一种身份验证机制,根据存储在所述 MongoDB 中的信息检查请求中的凭据。有一个教程可以做到这一点with JPA + Postgres但没有关于以相同容量使用 MongoDB 的信息。我怀疑我需要为这种情况编写一个自定义 IdentityProvider。我尝试使用 JPA 身份提供程序作为基础,但看起来 security-jpa 源代码仅包含 an abstract identity provider ,而actual provider is generated automatically using black magic 。有没有人成功地将现有的 Quarkus 安全架构适应 MongoDB 或 security-jpa 未涵盖的其他任何内容?

最佳答案

经过一些研究,我能够让自定义的 IdentityProvider 工作。这是一个非常简单的演示(没有任何 MongoDB 逻辑):

@ApplicationScoped
public class DemoIdentityProvider implements IdentityProvider<UsernamePasswordAuthenticationRequest> {
private static final Map<String, String> CREDENTIALS = Map.of("bob", "password124", "alice", "hunter2");

@Override
public Class<UsernamePasswordAuthenticationRequest> getRequestType() {
return UsernamePasswordAuthenticationRequest.class;
}

@Override
public Uni<SecurityIdentity> authenticate(UsernamePasswordAuthenticationRequest request,
AuthenticationRequestContext authenticationRequestContext) {
if (new String(request.getPassword().getPassword()).equals(CREDENTIALS.get(request.getUsername()))) {
return Uni.createFrom().item(QuarkusSecurityIdentity.builder()
.setPrincipal(new QuarkusPrincipal(request.getUsername()))
.addCredential(request.getPassword())
.setAnonymous(false)
.addRole("admin")
.build());
}
throw new AuthenticationFailedException("password invalid or user not found");
}
}

请注意,为了访问 QuarkusSecurityIdentity,需要将 quarkus-security 扩展作为依赖项包含在 pom.xml 中:

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-security</artifactId>
</dependency>

此外,需要将 quarkus.http.auth.basic=true 添加到 application.properties 中,以便将身份提供程序与基本身份验证一起使用。

关于quarkus - 在 Quarkus 中使用自定义身份提供商,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74621459/

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