getting the current logged in user in JHipster

后端 未结 3 899
情话喂你
情话喂你 2020-12-07 00:16

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

相关标签:
3条回答
  • 2020-12-07 00:30

    You can get User from userService.getUserWithAuthorities().get();

    0 讨论(0)
  • 2020-12-07 00:31

    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)

    0 讨论(0)
  • 2020-12-07 00:48

    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
    
    0 讨论(0)
提交回复
热议问题