Is it possible to expose a spring data rest generated API to manage the same users used with spring security for authentication and access control ?
Consider the entity:
@Entity
public class User implements UserDetails {
....
}
Which is used with spring security:
@Service
public class RepositoryUserDetailsService implements UserDetailsService{
private final UnsecuredUserRepository users;
@Autowired
public RepositoryUserDetailsService(UnsecuredUserRepository users) {
this.users = users;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User one = users.findOne(username);
if (one == null) {
throw new UsernameNotFoundException("No such user");
}
return one;
}
}
It uses the following spring data repository:
public interface UnsecuredUserRepository extends CrudRepository<User, String> {
}
I now want to add an admin API to manage users. Spring data rest can do this for me, and I can use spring security to secure it.
@PreAuthorize("hasRole('ROLE_USER_MANAGER')")
public interface UserRepository extends CrudRepository<User, String>, UserSignUpExtension {
@Override
@PreAuthorize("hasRole('ROLE_USER_MANAGER') or #userName == authentication?.name")
void delete(@Param("userName") String userName);
}
The problem is that one can't have multiple repositories for the same entities with spring data rest, using the secured repo creates a chicken egg problem, and prevents me from having startup code that creates default users (since the security checks are already enforced).