@Test public void loadUserByUsername() { UserDetails userDetails = userDetailsService.loadUserByUsername("sunriseydy"); try { String json = objectMapper.writeValueAsString(userDetails); log.warn(json); UserDetails jsonUserDetails = objectMapper.readValue(json, userDetails.getClass()); log.warn(String.valueOf(jsonUserDetails == null)); } catch (IOException e) { e.printStackTrace(); } }
@Override @Cacheable(cacheNames = "UserDetails", unless = "#username == null", cacheManager = "JDKCacheManager") public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { Assert.notNull(username, "用户名不能为空"); User user = userService.selectByUsername(username); if (user == null) { throw new UsernameNotFoundException("用户不存在"); } List<Role> roles = roleService.selectByUser(user); Assert.notEmpty(roles, "用户没有被分配角色"); List<Menu> menus = menuService.selectByRoles(roles); Assert.notEmpty(menus, "角色没有被分配菜单"); return UserUtils.convertToUserDetails(user, roles, menus); }
CustomUserDetailsServiceImpl implements UserDetailsService { @Override @Cacheable(cacheNames = "UserDetails", unless = "#username == null", cacheManager = "JDKCacheManager") public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { Assert.notNull(username, "用户名不能为空"); User user = userService.selectByUsername(username); if (user == null) { throw new UsernameNotFoundException("用户不存在"); } List<Role> roles = roleService.selectByUser(user); Assert.notEmpty(roles, "用户没有被分配角色"); List<Menu> menus = menuService.selectByRoles(roles); Assert.notEmpty(menus, "角色没有被分配菜单"); return UserUtils.convertToUserDetails(user, roles, menus); } }
CustomUserDetailsServiceImpl implements UserDetailsService { @Override @Cacheable(cacheNames = "UserDetails", unless = "#username == null", cacheManager = "JDKCacheManager") public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { Assert.notNull(username, "用户名不能为空"); User user = userService.selectByUsername(username); if (user == null) { throw new UsernameNotFoundException("用户不存在"); } List<Role> roles = roleService.selectByUser(user); Assert.notEmpty(roles, "用户没有被分配角色"); List<Menu> menus = menuService.selectByRoles(roles); Assert.notEmpty(menus, "角色没有被分配菜单"); return UserUtils.convertToUserDetails(user, roles, menus); } CustomUserDetailsServiceImpl(UserService userService, RoleService roleService, MenuService menuService); }
CustomUserDetailsServiceImpl implements UserDetailsService { @Override @Cacheable(cacheNames = "UserDetails", unless = "#username == null", cacheManager = "JDKCacheManager") public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { Assert.notNull(username, "用户名不能为空"); User user = userService.selectByUsername(username); if (user == null) { throw new UsernameNotFoundException("用户不存在"); } List<Role> roles = roleService.selectByUser(user); Assert.notEmpty(roles, "用户没有被分配角色"); List<Menu> menus = menuService.selectByRoles(roles); Assert.notEmpty(menus, "角色没有被分配菜单"); return UserUtils.convertToUserDetails(user, roles, menus); } CustomUserDetailsServiceImpl(UserService userService, RoleService roleService, MenuService menuService); @Override @Cacheable(cacheNames = "UserDetails", unless = "#username == null", cacheManager = "JDKCacheManager") UserDetails loadUserByUsername(String username); }
CustomUserDetailsServiceImpl implements UserDetailsService { @Override @Cacheable(cacheNames = "UserDetails", unless = "#username == null", cacheManager = "JDKCacheManager") public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { Assert.notNull(username, "用户名不能为空"); User user = userService.selectByUsername(username); if (user == null) { throw new UsernameNotFoundException("用户不存在"); } List<Role> roles = roleService.selectByUser(user); Assert.notEmpty(roles, "用户没有被分配角色"); List<Menu> menus = menuService.selectByRoles(roles); Assert.notEmpty(menus, "角色没有被分配菜单"); return UserUtils.convertToUserDetails(user, roles, menus); } CustomUserDetailsServiceImpl(UserService userService, RoleService roleService, MenuService menuService); @Override @Cacheable(cacheNames = "UserDetails", unless = "#username == null", cacheManager = "JDKCacheManager") UserDetails loadUserByUsername(String username);  }