Make all new users users by default

master
esensar 2018-01-17 22:12:22 +01:00
parent a0db1a1631
commit 7df33fd942
3 changed files with 17 additions and 1 deletions

View File

@ -28,7 +28,7 @@ dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.cloud:spring-cloud-starter-eureka')
testCompile('org.springframework.cloud:spring-cloud-starter-eureka-server')
testCompile('rg.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {

View File

@ -2,14 +2,17 @@ package ba.steleks.security;
import ba.steleks.AutowireHelper;
import ba.steleks.model.User;
import ba.steleks.repository.UserRolesJpaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import javax.persistence.PostPersist;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import java.util.HashSet;
/**
* Created by ensar on 30/05/17.
@ -21,6 +24,9 @@ public class UserPasswordEntityListener {
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private UserRolesJpaRepository userRolesJpaRepository;
@PrePersist
@PreUpdate
public void onUserUpdate(User user) {
@ -30,4 +36,13 @@ public class UserPasswordEntityListener {
}
}
@PostPersist
public void postUserCreated(User user) {
AutowireHelper.autowire(this, userRolesJpaRepository);
if (user.getUserRoles() == null) {
user.setUserRoles(new HashSet<>());
}
user.getUserRoles().add(userRolesJpaRepository.findOne(1L));
}
}

View File

@ -39,6 +39,7 @@ public class UserRoleFactory {
.stream()
// get role name
.map(UserRole::getRoleName)
.map(role -> "ROLE_" + role)
// create authority
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());