I am developing a web site.
frontend (SPA / vue.js) <-> REST backend (Spring Boot 3.2.1, Java 21)
I chose the keycloak IDP for the authorization server so as not to write a custom implementation of registration and authorization from scratch. This is my first time working with keycloak.
I use the spring-addons software and implemented the BFF design based on the example.
frontend <-> Spring Gateway (Client ouath2) <-> Spring REST Backend (oauth2 resource server)
I'm facing a problem, I don't understand how to bind user from keycloak to my Hibernate entity.
Simple example:
@Entity(name = "post")
public class Post {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Access(AccessType.PROPERTY)
private Integer id;
// another fields
@Column(name = "content")
private String content;
@Column(name = "author_id", insertable = false, updatable = false)
private Integer authorId; // how to ??? Keycloak user id is GUID...
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", referencedColumnName = "id")
private KeycloakUser author; // how to ??? KeycloakUser - example
// constructors / getters / setters omitted
}
The question is how do I assign their authors to my entities so that later using JPA I can extract them on request, for example findById ?
I found a solution in the fact that I need to maintain a separate database of users of my application, two implementation options:
A filter that checks the existence of the current authorized user and if he is not registered in the application DB, then register him;
SPI, an event system that could be used to intercept a new user registration event and add it to the application's user database.
So far, both options look doubtful to me personally, I would like to learn about the best practices for solving such a problem, if possible with examples.