I am building a jhipster application. and I am trying to get a list of objects based on the current logged in user. there are a few simple examples online such as jhipster b
You can get User from userService.getUserWithAuthorities().get();
You can get the current logged in user id from SecurityUtils. SecurityUtils.getCurrentUserLogin()
will give you the login of current logged in user. Use this login to fetch the user entity from db. (findOneByLogin
)
Currently the best answer is to use built-in UserService - the one provided by JHipster. You don't have to repeat calling SecurityUtils.getCurrentUserLogin()
twice (once yourself and once in UserService - check implementation). Usage would look like this:
final Optional<User> isUser = userService.getUserWithAuthorities();
if(!isUser.isPresent()) {
log.error("User is not logged in");
return new Shinything()
}
final User user = isUser.get();
// continue with the user