@Test public void loadByUsername_regular_success() { Mockito.when(userInfoRepository.getByUsername(regularUsername)).thenReturn(userInfoRegular); UserDetails user = service.loadUserByUsername(regularUsername); ArrayList<GrantedAuthority> userAuthorities = Lists.newArrayList(user.getAuthorities()); assertThat(userAuthorities, not(hasItem(DefaultUserInfoUserDetailsService.ROLE_ADMIN))); assertThat(userAuthorities, hasItem(DefaultUserInfoUserDetailsService.ROLE_USER)); assertEquals(user.getUsername(), regularSub); }
@Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { UserInfo userInfo = repository.getByUsername(username); if (userInfo != null) { String password = "password"; List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); authorities.add(ROLE_USER); if (admins != null && admins.contains(username)) { authorities.add(ROLE_ADMIN); } User user = new User(userInfo.getSub(), password, authorities); return user; } else { throw new UsernameNotFoundException("Could not find username: " + username); } }
DefaultUserInfoUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { UserInfo userInfo = repository.getByUsername(username); if (userInfo != null) { String password = "password"; List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); authorities.add(ROLE_USER); if (admins != null && admins.contains(username)) { authorities.add(ROLE_ADMIN); } User user = new User(userInfo.getSub(), password, authorities); return user; } else { throw new UsernameNotFoundException("Could not find username: " + username); } } }
DefaultUserInfoUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { UserInfo userInfo = repository.getByUsername(username); if (userInfo != null) { String password = "password"; List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); authorities.add(ROLE_USER); if (admins != null && admins.contains(username)) { authorities.add(ROLE_ADMIN); } User user = new User(userInfo.getSub(), password, authorities); return user; } else { throw new UsernameNotFoundException("Could not find username: " + username); } }  }
DefaultUserInfoUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { UserInfo userInfo = repository.getByUsername(username); if (userInfo != null) { String password = "password"; List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); authorities.add(ROLE_USER); if (admins != null && admins.contains(username)) { authorities.add(ROLE_ADMIN); } User user = new User(userInfo.getSub(), password, authorities); return user; } else { throw new UsernameNotFoundException("Could not find username: " + username); } }  @Override UserDetails loadUserByUsername(String username); List<String> getAdmins(); void setAdmins(List<String> admins); }
DefaultUserInfoUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { UserInfo userInfo = repository.getByUsername(username); if (userInfo != null) { String password = "password"; List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); authorities.add(ROLE_USER); if (admins != null && admins.contains(username)) { authorities.add(ROLE_ADMIN); } User user = new User(userInfo.getSub(), password, authorities); return user; } else { throw new UsernameNotFoundException("Could not find username: " + username); } }  @Override UserDetails loadUserByUsername(String username); List<String> getAdmins(); void setAdmins(List<String> admins); static final GrantedAuthority ROLE_USER; static final GrantedAuthority ROLE_ADMIN; }