/** * Turns the users password, granted authorities and enabled state into a property file value * * @param user */ String serializeUser(User user) { StringBuffer sb = new StringBuffer(); sb.append(user.getPassword()); sb.append(","); for (GrantedAuthority ga : user.getAuthorities()) { sb.append(ga.getAuthority()); sb.append(","); } sb.append(user.isEnabled() ? "enabled" : "disabled"); return sb.toString(); } }
@Test public void deserializeUserWithNullPasswordNoAuthorityTest() throws Exception { String userJsonWithoutPasswordString = removeNode(userWithNoAuthoritiesJson(), mapper, "password"); User user = mapper.readValue(userJsonWithoutPasswordString, User.class); assertThat(user).isNotNull(); assertThat(user.getUsername()).isEqualTo("admin"); assertThat(user.getPassword()).isNull(); assertThat(user.getAuthorities()).isEmpty(); assertThat(user.isEnabled()).isEqualTo(true); }
@Test public void deserializeRememberMeAuthenticationTokenWithUserTest() throws IOException { RememberMeAuthenticationToken token = mapper .readValue(String.format(REMEMBERME_AUTH_JSON, "\"password\""), RememberMeAuthenticationToken.class); assertThat(token).isNotNull(); assertThat(token.getPrincipal()).isNotNull().isInstanceOf(User.class); assertThat(((User) token.getPrincipal()).getUsername()).isEqualTo("admin"); assertThat(((User) token.getPrincipal()).getPassword()).isEqualTo("1234"); assertThat(((User) token.getPrincipal()).getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER")); assertThat(token.getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER")); assertThat(((User) token.getPrincipal()).isEnabled()).isEqualTo(true); } }
@Test public void withUserDetailsWhenAllEnabled() throws Exception { User expected = new User("rob", "pass", true, true, true, true, 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 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()); }
@Override public boolean isEnabled() { return super.isEnabled(); }
@Override public boolean isEnabled() { return super.isEnabled(); }
@Override public void write(Kryo kryo, Output output, User user) { output.writeString(user.getPassword()); output.writeString(user.getUsername()); final Collection<GrantedAuthority> authorities = user.getAuthorities(); output.writeInt(authorities.size(), true); for (final GrantedAuthority item : authorities) { _kryo.writeClassAndObject(output, item); } output.writeBoolean(user.isAccountNonExpired()); output.writeBoolean(user.isAccountNonLocked()); output.writeBoolean(user.isCredentialsNonExpired()); output.writeBoolean(user.isEnabled()); }
@Override public boolean isEnabled() { return wrappedUser.isEnabled(); }
protected UserDetailsService userDetailsService() { return new UserDetailsService() { @Override UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User u = crmService.findUserByUsername(username); return new org.springframework.security.core.userdetails.User( u.getUsername(), u.getPassword(), u.isEnabled(), u.isEnabled(), u.isEnabled(), u.isEnabled(), AuthorityUtils.createAuthorityList("USER", "write")); } } }
public Authentication authenticate(Authentication authentication) throws AuthenticationException { User user = null; try { //use a rest service to find the user. //Spring security provides user login name in authentication.getPrincipal() user = userRestService.loadUserByUsername(authentication.getPrincipal().toString()); } catch (Exception e) { log.error("Error loading user, not found: " + e.getMessage(), e); } if (user == null) { throw new UsernameNotFoundException(String.format("Invalid credentials", authentication.getPrincipal())); } else if (!user.isEnabled()) { throw new UsernameNotFoundException(String.format("Not found enabled user for username ", user.getUsername())); } //check user password stored in authentication.getCredentials() against stored password hash if (StringUtils.isBlank(authentication.getCredentials().toString()) || !passwordEncoder.isPasswordValid(user.getPasswordHash(), authentication.getCredentials().toString()) { throw new BadCredentialsException("Invalid credentials"); } //doLogin makes whatever is necesary when login is made (put info in session, load other data etc..) return doLogin(user); }
if (!user.isEnabled()) { throw new BadCredentialsException(CUENTA_IS_NOT_ENABLED);