spring security get user id from DB

前端 未结 4 2058
南旧
南旧 2021-02-06 16:10

I am using spring security for authentication and successfully able to get User object (org.springframework.security.core.userdetails.User) anywhere I

4条回答
  •  礼貌的吻别
    2021-02-06 16:29

    @RequestMapping(value="/getLogedUserid")
    public Long getUserByUsername(HttpServletRequest httpServletRequest) {      
        HttpSession httpSession = httpServletRequest.getSession();
        SecurityContext securityContext = (SecurityContext)
                httpSession.getAttribute("SPRING_SECURITY_CONTEXT");
        String username = securityContext.getAuthentication().getName();
        return userMetier.getUserByUsername(username);
    }
    

    here we have the definition of the function getting the id by the username

        @Override
    public Long getUserByUsername(String Username) {
        User user = userRepository.getUserByUsername(Username);
    
        return user.getIdUser();
    }
    

提交回复
热议问题