0

I've multiple different type users in my application, each type has it own role (and obviously authorities)

I want to use spring data rest but this functionnality must be possible for the ADMIN_ROLE user only.

I've then created 2 Repositories for each entity, one for the admin and the other for the rest of the users:

@RepositoryRestResource(exported = false)
public interface MenuRepository extends JpaRepository<Menu, Integer> {

}

// -------------------------------------------------------------------

@RepositoryRestResource
@PreAuthorize("hasRole('ROLE_ADMIN')")
public @interface AdminRestRepository {
}

// -------------------------------------------------------------------

@AdminRestRepository
public interface AdminMenuRestRepository extends JpaRepository<Menu, Integer> {
}

The problem is that once i start the application, sometimes the repository is exported and some other time it's not... any clue ?

Mssm
  • 717
  • 11
  • 29

1 Answers1

0

I found the explanation to this issue.

It is not possible to have 2 repositories for the same entity in a single spring app, since repositories are indentified by the entity class (for example PersonRepository is indentified by Person class) using a map.

Source can be found here: Multiple Repositories for the Same Entity in Spring Data Rest

As a work arround, it's possible to make another entity and map it to the same Database Table and then make another Repository for it (for example i would have PersonRepository and Person2Repository where Person and Person2 maps to the same table)

I'll finally use Spring url based security and secure each rest endpoint by it url .

Hope this helps

Mssm
  • 717
  • 11
  • 29