work on registration

This commit is contained in:
eugenp 2015-01-09 01:50:37 +02:00
parent 55f1a9b1bf
commit 7b40f74b96
3 changed files with 52 additions and 50 deletions

View File

@ -4,5 +4,5 @@ import org.baeldung.persistence.model.Privilege;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PrivilegeRepository extends JpaRepository<Privilege, Long> {
public Privilege findByName(String name);
Privilege findByName(String name);
}

View File

@ -4,9 +4,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.baeldung.persistence.dao.RoleRepository;
import org.baeldung.persistence.dao.UserRepository;
import org.baeldung.persistence.model.Privilege;
@ -17,6 +14,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -32,45 +32,45 @@ public class MyUserDetailsService implements UserDetailsService {
private MessageSource messages;
@Autowired
private RoleRepository roleRepository;
public MyUserDetailsService() {
public MyUserDetailsService() {
super();
}
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
boolean enabled = true;
boolean accountNonExpired = true;
boolean credentialsNonExpired = true;
boolean accountNonLocked = true;
// API
@Override
public UserDetails loadUserByUsername(final String email) throws UsernameNotFoundException {
try {
User user = userRepository.findByEmail(email);
final User user = userRepository.findByEmail(email);
if (user == null) {
return new org.springframework.security.core.userdetails.User(" ", " ", enabled, true, true, true, getAuthorities(roleRepository.findByName("ROLE_USER")));
return new org.springframework.security.core.userdetails.User(" ", " ", true, true, true, true, getAuthorities(roleRepository.findByName("ROLE_USER")));
}
return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), user.isEnabled(), accountNonExpired, credentialsNonExpired, accountNonLocked, getAuthorities(user.getRole()));
} catch (Exception e) {
return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), user.isEnabled(), true, true, true, getAuthorities(user.getRole()));
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
private Collection<? extends GrantedAuthority> getAuthorities(Role roleName) {
List<GrantedAuthority> authList = getGrantedAuthorities(getPrivileges(roleName));
return authList;
// UTIL
private final Collection<? extends GrantedAuthority> getAuthorities(final Role roleName) {
return getGrantedAuthorities(getPrivileges(roleName));
}
public List<String> getPrivileges(Role role) {
List<String> privileges = new ArrayList<String>();
Collection<Privilege> collection = role.getPrivileges();
for (Privilege item : collection) {
private final List<String> getPrivileges(final Role role) {
final List<String> privileges = new ArrayList<String>();
final Collection<Privilege> collection = role.getPrivileges();
for (final Privilege item : collection) {
privileges.add(item.getName());
}
return privileges;
}
private static List<GrantedAuthority> getGrantedAuthorities(List<String> privileges) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for (String privilege : privileges) {
private final List<GrantedAuthority> getGrantedAuthorities(final List<String> privileges) {
final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for (final String privilege : privileges) {
authorities.add(new SimpleGrantedAuthority(privilege));
}
return authorities;

View File

@ -13,46 +13,48 @@ import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
public class InitialDataLoader implements ApplicationListener<ContextRefreshedEvent>{
public class InitialDataLoader implements ApplicationListener<ContextRefreshedEvent> {
boolean alreadyExist = false;
@Autowired
private RoleRepository roleRepository;
@Autowired
private PrivilegeRepository privilegeRepository;
@Override
@Transactional
public void onApplicationEvent(ContextRefreshedEvent event) {
if(alreadyExist)
public void onApplicationEvent(final ContextRefreshedEvent event) {
if (alreadyExist)
return;
if(roleRepository.findAll().size() > 0 || privilegeRepository.findAll().size() > 0)
if (roleRepository.count() > 0 || privilegeRepository.count() > 0)
return;
//== create initial roles
Role admin = new Role("ROLE_ADMIN");
Role user = new Role("ROLE_USER");
//== create initial privileges
Privilege readPrivilege = new Privilege("READ_PRIVILEGE");
Privilege writePrivilege = new Privilege("WRITE_PRIVILEGE");
//== link roles and privileges
Privilege[] adminPrivileges = {readPrivilege, writePrivilege};
admin.setPrivileges(Arrays.asList(adminPrivileges));
Privilege[] userPrivileges = {readPrivilege};
user.setPrivileges(Arrays.asList(userPrivileges));
//== save to database
// == create initial privileges
final Privilege readPrivilege = new Privilege("READ_PRIVILEGE");
final Privilege writePrivilege = new Privilege("WRITE_PRIVILEGE");
privilegeRepository.save(readPrivilege);
privilegeRepository.save(writePrivilege);
// == create initial roles
final Role admin = new Role("ROLE_ADMIN");
final Role user = new Role("ROLE_USER");
// == link roles and privileges
admin.setPrivileges(Arrays.asList(readPrivilege, writePrivilege));
user.setPrivileges(Arrays.asList(readPrivilege));
roleRepository.save(admin);
roleRepository.save(user);
alreadyExist = true;
}
private final void createPrivilegeIfNotFound(final Privilege privilege) {
if (privilegeRepository.findByName(privilege.getName()) != null) {
privilegeRepository.save(privilege);
}
}
}