@Test public void shouldReturnUserDetailsWhenUserFoundInDB() { Role role = new Role(); role.setRole("HELLO"); User userFromDB = new User(); userFromDB.setOrg(new Org("MyOrg")); userFromDB.setTenant(new Tenant("MyTenant")); userFromDB.setUsername("MyUsername"); userFromDB.setPassword("MyPass"); userFromDB.setDisplayName("MyName"); userFromDB.setRoles(Collections.singleton(role)); userFromDB.setEnabled(true); when(userRepositoryMock.find(anyString(), anyString(), anyString())).thenReturn(userFromDB); String providedUsername = "MyOrg" + String.valueOf(Character.LINE_SEPARATOR) + "MyTenant" + String.valueOf(Character.LINE_SEPARATOR) + "MyUsername"; UserDetails userDetails = userDetailsService.loadUserByUsername(providedUsername); assertEquals("MyOrg" + String.valueOf(Character.LINE_SEPARATOR) + "MyTenant" + String.valueOf(Character.LINE_SEPARATOR) + "MyUsername", userDetails.getUsername()); assertEquals("MyPass", userDetails.getPassword()); assertEquals("[ROLE_HELLO]", userDetails.getAuthorities().toString()); assertTrue(userDetails.isEnabled()); assertTrue(userDetails.isAccountNonExpired()); assertTrue(userDetails.isAccountNonLocked()); assertTrue(userDetails.isCredentialsNonExpired()); }
@Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { String[] orgTenantUsername = username.split(String.valueOf(Character.LINE_SEPARATOR)); if (orgTenantUsername == null || orgTenantUsername.length != 3) { throw new UsernameNotFoundException("Org, Tenant and Username must be provided."); } User user = userRepository.find(orgTenantUsername[0], orgTenantUsername[1], orgTenantUsername[2]); if (user == null) { throw new UsernameNotFoundException(String.format("Username[%s] not found for Org[%s] Tenant[%s].", orgTenantUsername[2], orgTenantUsername[0], orgTenantUsername[1])); } List<GrantedAuthority> authorities = user.getRoles().stream() .map(authority -> new SimpleGrantedAuthority("ROLE_" + authority.getRole())) .collect(Collectors.toList()); String authPrinciple = user.getOrg().getOrg() + String.valueOf(Character.LINE_SEPARATOR) + user.getTenant().getTenant() + String.valueOf(Character.LINE_SEPARATOR) + user.getUsername(); return new org.springframework.security.core.userdetails.User(authPrinciple, user.getPassword(), user.isEnabled(), true, true, true, authorities); }
MultiTenantUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { String[] orgTenantUsername = username.split(String.valueOf(Character.LINE_SEPARATOR)); if (orgTenantUsername == null || orgTenantUsername.length != 3) { throw new UsernameNotFoundException("Org, Tenant and Username must be provided."); } User user = userRepository.find(orgTenantUsername[0], orgTenantUsername[1], orgTenantUsername[2]); if (user == null) { throw new UsernameNotFoundException(String.format("Username[%s] not found for Org[%s] Tenant[%s].", orgTenantUsername[2], orgTenantUsername[0], orgTenantUsername[1])); } List<GrantedAuthority> authorities = user.getRoles().stream() .map(authority -> new SimpleGrantedAuthority("ROLE_" + authority.getRole())) .collect(Collectors.toList()); String authPrinciple = user.getOrg().getOrg() + String.valueOf(Character.LINE_SEPARATOR) + user.getTenant().getTenant() + String.valueOf(Character.LINE_SEPARATOR) + user.getUsername(); return new org.springframework.security.core.userdetails.User(authPrinciple, user.getPassword(), user.isEnabled(), true, true, true, authorities); } }
MultiTenantUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { String[] orgTenantUsername = username.split(String.valueOf(Character.LINE_SEPARATOR)); if (orgTenantUsername == null || orgTenantUsername.length != 3) { throw new UsernameNotFoundException("Org, Tenant and Username must be provided."); } User user = userRepository.find(orgTenantUsername[0], orgTenantUsername[1], orgTenantUsername[2]); if (user == null) { throw new UsernameNotFoundException(String.format("Username[%s] not found for Org[%s] Tenant[%s].", orgTenantUsername[2], orgTenantUsername[0], orgTenantUsername[1])); } List<GrantedAuthority> authorities = user.getRoles().stream() .map(authority -> new SimpleGrantedAuthority("ROLE_" + authority.getRole())) .collect(Collectors.toList()); String authPrinciple = user.getOrg().getOrg() + String.valueOf(Character.LINE_SEPARATOR) + user.getTenant().getTenant() + String.valueOf(Character.LINE_SEPARATOR) + user.getUsername(); return new org.springframework.security.core.userdetails.User(authPrinciple, user.getPassword(), user.isEnabled(), true, true, true, authorities); }  }
MultiTenantUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { String[] orgTenantUsername = username.split(String.valueOf(Character.LINE_SEPARATOR)); if (orgTenantUsername == null || orgTenantUsername.length != 3) { throw new UsernameNotFoundException("Org, Tenant and Username must be provided."); } User user = userRepository.find(orgTenantUsername[0], orgTenantUsername[1], orgTenantUsername[2]); if (user == null) { throw new UsernameNotFoundException(String.format("Username[%s] not found for Org[%s] Tenant[%s].", orgTenantUsername[2], orgTenantUsername[0], orgTenantUsername[1])); } List<GrantedAuthority> authorities = user.getRoles().stream() .map(authority -> new SimpleGrantedAuthority("ROLE_" + authority.getRole())) .collect(Collectors.toList()); String authPrinciple = user.getOrg().getOrg() + String.valueOf(Character.LINE_SEPARATOR) + user.getTenant().getTenant() + String.valueOf(Character.LINE_SEPARATOR) + user.getUsername(); return new org.springframework.security.core.userdetails.User(authPrinciple, user.getPassword(), user.isEnabled(), true, true, true, authorities); }  @Override UserDetails loadUserByUsername(String username); }
MultiTenantUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { String[] orgTenantUsername = username.split(String.valueOf(Character.LINE_SEPARATOR)); if (orgTenantUsername == null || orgTenantUsername.length != 3) { throw new UsernameNotFoundException("Org, Tenant and Username must be provided."); } User user = userRepository.find(orgTenantUsername[0], orgTenantUsername[1], orgTenantUsername[2]); if (user == null) { throw new UsernameNotFoundException(String.format("Username[%s] not found for Org[%s] Tenant[%s].", orgTenantUsername[2], orgTenantUsername[0], orgTenantUsername[1])); } List<GrantedAuthority> authorities = user.getRoles().stream() .map(authority -> new SimpleGrantedAuthority("ROLE_" + authority.getRole())) .collect(Collectors.toList()); String authPrinciple = user.getOrg().getOrg() + String.valueOf(Character.LINE_SEPARATOR) + user.getTenant().getTenant() + String.valueOf(Character.LINE_SEPARATOR) + user.getUsername(); return new org.springframework.security.core.userdetails.User(authPrinciple, user.getPassword(), user.isEnabled(), true, true, true, authorities); }  @Override UserDetails loadUserByUsername(String username);  }