Refactor MyUserDetailsService

This commit is contained in:
Grzegorz Piwowarek 2016-10-01 09:58:51 +02:00
parent c8ff59ffe2
commit 62fb8b257b
1 changed files with 9 additions and 31 deletions

View File

@ -1,11 +1,5 @@
package org.baeldung.service; package org.baeldung.service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.baeldung.security.SecurityRole; import org.baeldung.security.SecurityRole;
@ -17,9 +11,9 @@ import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** import java.util.*;
* User Details Service - hard coded to two users for the example. import java.util.stream.Collectors;
*/
@Service @Service
public class MyUserDetailsService implements UserDetailsService { public class MyUserDetailsService implements UserDetailsService {
@ -28,24 +22,19 @@ public class MyUserDetailsService implements UserDetailsService {
private final Map<String, User> availableUsers = new HashMap<String, User>(); private final Map<String, User> availableUsers = new HashMap<String, User>();
public MyUserDetailsService() { public MyUserDetailsService() {
populateDemoUsers(); populateDemoUsers();
} }
//
@Override @Override
public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException { public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
logger.info("Load user by username " + username); logger.info("Load user by username " + username);
final UserDetails user = availableUsers.get(username); final UserDetails user = availableUsers.get(username);
if (user == null) { if (user == null) {
throw new UsernameNotFoundException("Username not found"); throw new UsernameNotFoundException(username);
} else {
return availableUsers.get(username);
} }
return user;
} }
/** /**
@ -59,24 +48,13 @@ public class MyUserDetailsService implements UserDetailsService {
availableUsers.put("admin", createUser("admin", "password", Arrays.asList(SecurityRole.ROLE_ADMIN))); availableUsers.put("admin", createUser("admin", "password", Arrays.asList(SecurityRole.ROLE_ADMIN)));
} }
/**
* Create a demo User.
*
* @param username
* Username
* @param password
* Password
* @param roles
* Role names user is assigned to
* @return User
*/
private User createUser(final String username, final String password, final List<SecurityRole> roles) { private User createUser(final String username, final String password, final List<SecurityRole> roles) {
logger.info("Create user " + username); logger.info("Create user " + username);
final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); final List<GrantedAuthority> authorities = roles.stream()
for (final SecurityRole role : roles) { .map(role -> new SimpleGrantedAuthority(role.toString()))
authorities.add(new SimpleGrantedAuthority(role.toString())); .collect(Collectors.toList());
}
return new User(username, password, true, true, true, true, authorities); return new User(username, password, true, true, true, true, authorities);
} }
} }