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); } }
public void update(User user) { userRepository.saveAndFlush(user); }
public User findLoggedInUser(String name) { // TODO Auto-generated method stub return userRepository.findByName(name); }