gpt4 book ai didi

java - JBoss Picketlink - 自定义用户模型身份验证失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:49:31 26 4
gpt4 key购买 nike

我一直在关注 JBoss Picketlink 2.5.x documentation found here试图创建自定义 User 模型来对我的应用程序进行身份验证。我的应用程序是部署在 JBoss EAP 6.2 上的典型 JavaEE 6 应用程序。我已经能够成功创建自定义 User 模型类,并且能够毫无问题地保留用户。我看到我的自定义属性按预期反射(reflect)在数据库中。我的自定义 User 模型如下所示:

public class ExtendedUser extends User {

private static final long serialVersionUID = -9132382669181464122L;

@AttributeProperty
private String uniqueIdentifier = null;

...

}

...创建新用户帐户的代码如下所示:

ExtendedUser u = new ExtendedUser(username);
u.setFirstName(firstName);
u.setLastName(lastName);
u.setEmail(emailAddress);
u.setUniqueIdentifier(UUID.randomUUID().toString());

idm.add(u);
idm.updateCredential(u, new Password(password));

此外,我有一个用户帐户的自定义实体模型,如下所示:

@Entity(name = "UserAccount")
@Table(name = "tbl_user_account", uniqueConstraints = {
@UniqueConstraint(columnNames = { "email" }) })
@IdentityManaged({ ExtendedUser.class, Agent.class })
public class UserAccountBean extends BaseAccountIdentityBean implements
IdentifiedDomainEntity {

private static final long serialVersionUID = -537779599507513418L;

<snip... irrelevant fields...>

@Column(length = ValidationConstants.UUID_LEN, unique = true)
@Unique
@Length(max = ValidationConstants.UUID_LEN)
@Index(name = "idx_user_account_uniqueid")
@AttributeValue(name = "uniqueIdentifier")
// @NotEmpty
private String uniqueIdentifier = null;


...

我看到 uniqueIdentifier 属性按预期填充到数据库表中。但是,每当我尝试使用以这种方式创建的用户进行身份验证时,身份验证都会失败。每次。我还需要做些什么来使用我的自定义 User 对象吗?创建我的 IdentityIdentityManager 实例时是否必须在某处指定它?我的身份验证代码比较简单,如下所示:

@Inject
private DefaultLoginCredentials loginCredentials = null;

...

loginCredentials.setUserId(loginName);
loginCredentials.setPassword(password);

AuthenticationResult result = identity.login();
if (logger.isDebugEnabled()) {
logger.debug("authenticate(String, String, String) - AuthenticationResult result=" + result); //$NON-NLS-1$
}

正如我所提到的,这段代码每次都会失败。我已经对密码进行了完整性检查,我知道它们是正确的。我没有看到任何异常或错误写入日志,所以我不确定问题可能是什么。如果我使用 Picketlink 库提供的标准 User 对象,即使我对后端数据库使用相同的实体,代码也能正常工作。它仅在使用 ExtendedUser 时失败,所以我确信我遗漏了一些东西。有什么建议吗?

更新:Picketlink 2.6.0 似乎添加了更多的日志记录。我已经升级,但仍然失败,但现在我看到以下错误消息:

14:41:23,133 DEBUG [org.picketlink.idm.credential] (http-localhost/127.0.0.1:9080-3) Starting validation for credentials [class org.picketlink.idm.credential.UsernamePasswordCredentials][org.picketlink.idm.credential.UsernamePasswordCredentials@56370e07] using identity store [org.picketlink.idm.jpa.internal.JPAIdentityStore@22ef0c6c] and credential handler [org.picketlink.idm.credential.handler.PasswordCredentialHandler@387a19c9].
14:41:23,134 DEBUG [org.picketlink.idm.credential] (http-localhost/127.0.0.1:9080-3) PLIDM001003: Trying to find account [user6_4] using default account type [class org.picketlink.idm.model.basic.User] with property [loginName].
14:41:23,136 DEBUG [org.picketlink.idm.credential] (http-localhost/127.0.0.1:9080-3) PLIDM001003: Trying to find account [user6_4] using default account type [class org.picketlink.idm.model.basic.Agent] with property [loginName].
14:41:23,138 DEBUG [org.picketlink.idm.credential] (http-localhost/127.0.0.1:9080-3) Account NOT FOUND for credentials [class org.picketlink.idm.credential.UsernamePasswordCredentials][org.picketlink.idm.credential.UsernamePasswordCredentials@56370e07].

很明显,Picketlink 正在尝试使用标准的 User 对象进行身份验证,而不是我的自定义对象。也就是说,我如何告诉 Picketlink 使用我的自定义 ExtendedUser 对象进行身份验证?

最佳答案

我刚经历了和你一样的头痛。我也一直在尝试添加自己的用户帐户。我能够让它工作的唯一原因是您提到 PicketLink 正在尝试查找用户和代理类型的帐户!这意味着您必须实现自己的 PasswordCredentialHandler。不用担心!你可以只复制this classthis class到你的项目。在 AbstractCredentialHandler 的“configureDefaultSupportedAccountTypes”方法中,您将看到以下内容:

   if (!this.defaultAccountTypes.contains(User.class)) {
this.defaultAccountTypes.add(User.class);
}

if (!this.defaultAccountTypes.contains(Agent.class)) {
this.defaultAccountTypes.add(Agent.class);
}

只需像这样向其中添加您的自定义帐户类:

if (!this.defaultAccountTypes.contains(CustomUser.class)) {
this.defaultAccountTypes.add(CustomUser.class);

}

您要做的最后一件事是将 PasswordCredentialHandler 添加到 IDMConfigurtion:

private void initConfig() {
IdentityConfigurationBuilder builder = new IdentityConfigurationBuilder();

builder
.named("default")
.stores()
.jpa()
.mappedEntity(
AccountTypeEntity.class,
RoleTypeEntity.class,
GroupTypeEntity.class,
IdentityTypeEntity.class,
RelationshipTypeEntity.class,
RelationshipIdentityTypeEntity.class,
PartitionTypeEntity.class,
PasswordCredentialTypeEntity.class,
AttributeTypeEntity.class,
CustomUserEntity.class
)
.supportGlobalRelationship(Relationship.class)
.addContextInitializer(this.contextInitializer)
.addCredentialHandler(PasswordCredentialHandler.class)
.supportAllFeatures();
identityConfig = builder.build();

希望对您有所帮助!

关于java - JBoss Picketlink - 自定义用户模型身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21267142/

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