// for populating the menu based on logged in user role from db public void populateMenu() throws Exception { authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) authentication.getAuthorities(); roles = rolesService.getByName(authorities.iterator().next().getAuthority()); } else roles = null; if (NotEmpty.notEmpty(roles)) { Set<Menu> menuSet = roles.getMenuSet(); ArrayList<Menu> menuList = new ArrayList<Menu>(menuSet); userSession.setMenuList(menuList); } }
// for getting the current logged in user from spring SecurityContextHolder public void setCurrentUser() throws Exception { authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) userSession.setUser(userService.getUserByEmail(authentication.getName().toString())); else System.out.println("Authentication is null"); }