use*_*911 9 hibernate jpa keycloak
到目前为止,我一直在使用 KeyCloak,并且能够成功地设置和运行客户门户示例。现在我需要在我的应用程序中实际使用它,我不完全确定 KeyCloak 是否是我正在寻找的正确的东西,但我相信我的需要只是一个常见的用例,希望 KeyCloak 是我所需要的正确软件寻找..
当用户访问我的网站时,他会注册并发布帖子。帖子和用户信息都存储在数据库中,用户和帖子之间的链接,即谁发了哪个帖子?所以我的数据库中有两个表:Post(id, post) 和 User(id,name),另一个表 UserPost(PostID, UserID) 用于存储链接信息。这在我自己的数据库中一切正常。
但是现在当KeyCloak开始使用时,用户首先在KeyCloak服务器中注册,用户信息存储在其自己的数据库中,这似乎与我的应用程序中的数据库(Post和User)无关。我不想在两台服务器中复制两个用户数据库,对吗?即使我可以容忍重复,KeyCloak数据库和我的应用程序数据库之间如何建立连接?我在我的应用程序中使用 JBoss、Hibernate/JPA。
也许我在如何将 KeyCloak 用户表与我自己的应用程序数据库连接的方式中遗漏了一些东西。有我可以阅读的教程或文档吗?
谢谢你。
更新:我的应用程序中的这个 User 表只存储一个 id,它来自 KeyCloak 用户注册信息,还有一个字段 'reputation' 将根据该用户的新帖子分配。用户的大多数其他属性将在 KeyCloak 的 USER_ENTITY 表中保持不变。现在,每当有新用户注册时,KeyCloak 都会在 USER_ENTITY 表中插入一条记录。不用担心。但同时,我需要根据 KeyCloak USER_ENTITY 中的用户 ID 向我的应用程序中的 User 表添加一条记录。问题是如何从注册html页面从Keycloak获取用户ID?
@Entity
public class User {
@Id
private Long id;
private int reputation = 0;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Post> posts = new ArrayList<>();
public User() {
}
public User(Long id, int reputation) {
this.id = id;
this.reputation = reputation;
}
public void setId(Long id) {
}
public Long getId() {
return id;
}
public void setReputation(int reputation) {
this.reputation = reputation;
}
public int getReputation() {
return reputation;
}
public List<Post> getPosts() {
return posts;
}
}
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private User user;
private String text;
public Post() {
}
public Long getId() {
return id;
}
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public void setText(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
Run Code Online (Sandbox Code Playgroud)
I'm in the process of a conversion almost exactly like this. I had users and roles in a home grown database and used Wildfly security via a custom UsernamePasswordLoginModule. I'm now moving to Keycloak.
I too had database referential integrity for users to other things. What I've done is to not remove the user table completely but to move all of the user attributes over to Keycloak. I maintain a user table with a very minimal amount of information and a primary key that is the Keycloak "user name" (a GUID). You can get that from getting the principal:
@Context
private SecurityContext sc;
...
String userId = sc.getUserPrincipal().getName();
Run Code Online (Sandbox Code Playgroud)
Now I have a key that I can use with JPA to get a user and tie them to anything they need to be tied to.
There will be a further step where I get more data from Keycloak about the user. Right now I have enough in the AccessToken:
KeycloakPrincipal<KeycloakSecurityContext> kcPrincipal = (KeycloakPrincipal<KeycloakSecurityContext>)(sc.getUserPrincipal());
AccessToken accessToken = kcPrincipal.getKeycloakSecurityContext().getToken();
String firstName = accessToken.getGivenName();
String lastName = accessToken.getFamilyName();
Run Code Online (Sandbox Code Playgroud)
but I will eventually have custom user attributes pushed over to the Keycloak side that I'll need to get access to.