@Override public UserDetails mapRow(ResultSet rs, int rowNum) throws SQLException { String username = rs.getString(1); String password = rs.getString(2); boolean enabled = rs.getBoolean(3); return new User(username, password, enabled, true, true, true, AuthorityUtils.NO_AUTHORITIES); }
public MockUserDetailsService() { users.put("valid", new User("valid", "", true, true, true, true, auths)); users.put("locked", new User("locked", "", true, true, true, false, auths)); users.put("disabled", new User("disabled", "", false, true, true, true, auths)); users.put("credentialsExpired", new User("credentialsExpired", "", true, true, false, true, auths)); users.put("expired", new User("expired", "", true, false, true, true, auths)); }
public UserDetails loadUserByUsername(String username) { if ("peter".equals(username)) { return new User("peter", "opal", false, true, true, true, ROLES_12); } throw new UsernameNotFoundException("Could not find: " + username); } }
public UserDetails loadUserByUsername(String username) { if ("peter".equals(username)) { return new User("peter", "opal", true, false, true, true, ROLES_12); } throw new UsernameNotFoundException("Could not find: " + username); } }
public UserDetails loadUserByUsername(String ssoUserId) throws AuthenticationException { return new User(ssoUserId, "password", true, true, true, true, AuthorityUtils.createAuthorityList("ROLE_A", "ROLE_B")); } }
public InMemoryUserDetailsManager(Properties users) { Enumeration<?> names = users.propertyNames(); UserAttributeEditor editor = new UserAttributeEditor(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); editor.setAsText(users.getProperty(name)); UserAttribute attr = (UserAttribute) editor.getValue(); UserDetails user = new User(name, attr.getPassword(), attr.isEnabled(), true, true, true, attr.getAuthorities()); createUser(user); } }
@Test public void useIsSerializable() throws Exception { UserDetails user = new User("rod", "koala", false, true, true, true, ROLE_12); // Serialize to a byte array ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(user); out.close(); }
@Test(expected = IllegalArgumentException.class) public void constructorWhenEmptyKeyThenThrowsException() { new CasAuthenticationToken("", "user", "password", Collections.<GrantedAuthority>emptyList(), new User("user", "password", Collections.<GrantedAuthority>emptyList()), null); }
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { UserDetails user = users.get(username.toLowerCase()); if (user == null) { throw new UsernameNotFoundException(username); } return new User(user.getUsername(), user.getPassword(), user.isEnabled(), user.isAccountNonExpired(), user.isCredentialsNonExpired(), user.isAccountNonLocked(), user.getAuthorities()); }
private CasAuthenticationToken createCasAuthenticationToken() { User principal = new User("admin", "1234", Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"))); Collection<? extends GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")); Assertion assertion = new AssertionImpl(new AttributePrincipalImpl("assertName"), START_DATE, END_DATE, START_DATE, Collections.<String, Object>emptyMap()); return new CasAuthenticationToken(KEY, principal, principal.getPassword(), authorities, new User("admin", "1234", authorities), assertion); } }
@Test public void enabledFlagIsFalseForDisabledAccount() throws Exception { UserDetails user = new User("rod", "koala", false, true, true, true, ROLE_12); assertThat(user.isEnabled()).isFalse(); }
@Test public void testNullWithinGrantedAuthorityElementIsRejected() throws Exception { try { List<GrantedAuthority> auths = AuthorityUtils.createAuthorityList("ROLE_ONE"); auths.add(null); auths.add(new SimpleGrantedAuthority("ROLE_THREE")); new User(null, "koala", true, true, true, true, auths); fail("Should have thrown IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }
@Test public void authenticateWhenPasswordEncoderAndSuccessThenSuccess() { this.manager.setPasswordEncoder(this.passwordEncoder); when(this.passwordEncoder.matches(any(), any())).thenReturn(true); User user = new User(this.username, this.password, AuthorityUtils.createAuthorityList("ROLE_USER")); when(this.repository.findByUsername(user.getUsername())).thenReturn(Mono.just(user)); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( this.username, this.password); Authentication authentication = this.manager.authenticate(token).block(); assertThat(authentication).isEqualTo(authentication); }
@Test public void serializeUserWithoutAuthority() throws JsonProcessingException, JSONException { User user = new User("admin", "1234", Collections.<GrantedAuthority>emptyList()); String userJson = mapper.writeValueAsString(user); JSONAssert.assertEquals(userWithNoAuthoritiesJson(), userJson, true); }
@Transactional public void createOrUpdate(UserPO user) { String username = user.getUsername(); User userDetails = new User(username, encoder.encode(user.getPassword()), authorities); if (userDetailsManager.userExists(username)) { userDetailsManager.updateUser(userDetails); } else { userDetailsManager.createUser(userDetails); } UserPO managedUser = userRepository.findByUsername(username); managedUser.setEmail(user.getEmail()); userRepository.save(managedUser); }
@Test public void updateUserChangesDataCorrectlyAndClearsCache() { insertJoe(); User newJoe = new User("joe", "newpassword", false, true, true, true, AuthorityUtils.createAuthorityList(new String[] { "D", "F", "E" })); manager.updateUser(newJoe); UserDetails joe = manager.loadUserByUsername("joe"); assertThat(joe).isEqualTo(newJoe); assertThat(cache.getUserMap().containsKey("joe")).isFalse(); }
@Test public void resolveArgumentUserDetails() throws Exception { setAuthenticationPrincipal(new User("user", "password", AuthorityUtils.createAuthorityList("ROLE_USER"))); assertThat(resolver.resolveArgument(showUserAnnotationUserDetails(), null)) .isEqualTo(expectedPrincipal); }
@Test public void withUserDetailsWhenAllDisabled() throws Exception { User expected = new User("rob", "pass", false, false, false, false, ROLE_12); UserDetails actual = User.withUserDetails(expected).build(); assertThat(actual.getUsername()).isEqualTo(expected.getUsername()); assertThat(actual.getPassword()).isEqualTo(expected.getPassword()); assertThat(actual.getAuthorities()).isEqualTo(expected.getAuthorities()); assertThat(actual.isAccountNonExpired()).isEqualTo(expected.isAccountNonExpired()); assertThat(actual.isAccountNonLocked()).isEqualTo(expected.isAccountNonLocked()); assertThat(actual.isCredentialsNonExpired()).isEqualTo(expected.isCredentialsNonExpired()); assertThat(actual.isEnabled()).isEqualTo(expected.isEnabled()); }
@Test public void updateUserChangesDataCorrectlyAndClearsCacheWithLocking() { setUpAccLockingColumns(); insertJoe(); User newJoe = new User("joe", "newpassword", false, false, false, true, AuthorityUtils.createAuthorityList("D", "F", "E")); manager.updateUser(newJoe); UserDetails joe = manager.loadUserByUsername(newJoe.getUsername()); assertThat(joe).isEqualToComparingFieldByField(newJoe); assertThat(cache.getUserMap().containsKey(newJoe.getUsername())).isFalse(); }
@Test public void createUserInsertsCorrectDataWithLocking() { setUpAccLockingColumns(); UserDetails user = new User("joe", "pass", true, false, true, false, AuthorityUtils.createAuthorityList("A", "B")); manager.createUser(user); UserDetails user2 = manager.loadUserByUsername(user.getUsername()); assertThat(user2).isEqualToComparingFieldByField(user); }