public void save(User user) { user.setEnabled(true); BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); user.setPassword(encoder.encode(user.getPassword())); List<Role> roles = new ArrayList<Role>(); roles.add(roleRepository.findByName("ROLE_USER")); user.setRoles(roles); userRepository.save(user); }
@PostConstruct public void init() { if (roleRepository.findByName("ROLE_ADMIN") == null) { Role roleUser = new Role(); roleUser.setName("ROLE_USER"); roleRepository.save(roleUser); Role roleAdmin = new Role(); roleAdmin.setName("ROLE_ADMIN"); roleRepository.save(roleAdmin); User userAdmin = new User(); userAdmin.setEnabled(true); userAdmin.setName("admin"); BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); userAdmin.setPassword(encoder.encode("password")); List<Role> roles = new ArrayList<Role>(); roles.add(roleAdmin); roles.add(roleUser); userAdmin.setRoles(roles); userRepository.save(userAdmin); } }