我创建了一个MongoDbUserStorageProvider
将用户存储委托给 MongoDB:
public class MongoDbUserStorageProvider implements
UserStorageProvider,
UserLookupProvider,
CredentialInputValidator,
CredentialInputUpdater,
UserQueryProvider,
UserRegistrationProvider {
...
}
public final class MongoUserAdapter extends AbstractUserAdapter {
...
@Override
public void setFirstName(String firstName) {
this.userDocument.put("firstName", firstName);
}
...
}
我遇到的问题是:当我通过 Keycloak 控制台创建用户时,只有用户名存储在数据库中(不是电子邮件、名字等):
如您所见,该UserRegistrationProvider.addUser(RealmModel realm, String username)
操作仅接收用户名:
@Override
public UserModel addUser(RealmModel realm, String username) {
final MongoCollection<Document> users = database.getCollection("users");
Document doc = new Document("username", username);
users.insertOne(doc);
Optional<Document> insertedUser = loadUserDocument(new HashMap<String, String>() {{
put("username", username);
}});
return buildUserModel(realm, insertedUser.get());
}
在org.keycloak.services.resources.admin.UsersResource
该方法中public Response createUser(final UserRepresentation rep)
遵循以下逻辑:
try {
UserModel user = session.users().addUser(realm, rep.getUsername()); //1)
Set<String> emptySet = Collections.emptySet();
UserResource.updateUserFromRep(user, rep, emptySet, realm, session, false); //2)
RepresentationToModel.createCredentials(rep, session, realm, user, true);
adminEvent.operation(OperationType.CREATE).resourcePath(session.getContext().getUri(), user.getId()).representation(rep).success();
if (session.getTransactionManager().isActive()) {
session.getTransactionManager().commit();
}
return Response.created(session.getContext().getUri().getAbsolutePathBuilder().path(user.getId()).build()).build();
} catch (ModelDuplicateException e) {
if (session.getTransactionManager().isActive()) {
session.getTransactionManager().setRollbackOnly();
}
return ErrorResponse.exists("User exists with same username or email");
} catch (ModelException me){
if (session.getTransactionManager().isActive()) {
session.getTransactionManager().setRollbackOnly();
}
logger.warn("Could not create user", me);
return ErrorResponse.exists("Could not create user");
}
}
1)它调用我的 addUser 实现(仅使用用户名存储新用户)
MongoUserAdapter
2) 它用其余字段填充用户对象 ( )
但是我看不到如何使填充的用户对象持久存在。