Using Spring JPA Repositorys and hibernate entity filter with keycloak

426 Views Asked by At

I want to develop a Spring Boot Application using Keycloak-authentication.

I defined an entity 'House' with a String 'ownerID' which is the users keycloak-principal-id. Now i want to add an hibernate filter to my entity-class which filters all houses and compare the keycloak-principal-id with the saved ownerId.

My House-Entity with Filter:

@Entity
@Table(name = "house_houses")
@FilterDef(name = "keycloakFilter", parameters = {
    @ParamDef(name = "sessionID", type = "string")
})
@Filter(name = "keycloakFilter", condition = "sessionID=ownerId")
public class House {

@Id
@Column(name = "house_id", updatable = false, nullable = false)
private String id;

@Column(name = "house_owner", nullable = false)
private String ownerId;

...
}

My HouseRepository:

public interface HouseRepository extends CrudRepository<House, String> {}

No i want to get all houses for the requesting user if i call houseRepository.findAll(), but no houses of other users.

Does everyone knows a way to do so?

What i don't want to use (if possible) is @Query-annotation on my repository methods.

Greetings from Germany!

0

There are 0 best solutions below