From 2e5880f1a69c541d2f1cf56152630bc566ac41a0 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 10 Mar 2022 10:10:29 +0400 Subject: [PATCH] #3 -- add default admin --- .../java/ru/ulstu/SeminarApplication.java | 15 +++++++++ src/main/java/ru/ulstu/model/User.java | 6 ++++ src/main/java/ru/ulstu/user/UserService.java | 32 ++++++++++++++++++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/main/java/ru/ulstu/SeminarApplication.java b/src/main/java/ru/ulstu/SeminarApplication.java index fb6201f..04b8e36 100644 --- a/src/main/java/ru/ulstu/SeminarApplication.java +++ b/src/main/java/ru/ulstu/SeminarApplication.java @@ -2,10 +2,25 @@ package ru.ulstu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.event.EventListener; +import ru.ulstu.user.UserService; @SpringBootApplication public class SeminarApplication { + private final UserService userService; + + public SeminarApplication(UserService userService) { + this.userService = userService; + } + public static void main(String[] args) { SpringApplication.run(SeminarApplication.class, args); } + + @EventListener(ApplicationReadyEvent.class) + public void doSomethingAfterStartup() { + System.out.println("hello world, I have just started up"); + userService.initDefaultAdmin(); + } } diff --git a/src/main/java/ru/ulstu/model/User.java b/src/main/java/ru/ulstu/model/User.java index 7345316..5db0c57 100644 --- a/src/main/java/ru/ulstu/model/User.java +++ b/src/main/java/ru/ulstu/model/User.java @@ -39,6 +39,12 @@ public class User extends BaseEntity { roles = new HashSet<>(); } + public User(String login, String password, Set roles) { + this.login = login; + this.password = password; + this.roles = roles; + } + public String getLogin() { return login; } diff --git a/src/main/java/ru/ulstu/user/UserService.java b/src/main/java/ru/ulstu/user/UserService.java index 3bcb7bb..752f234 100644 --- a/src/main/java/ru/ulstu/user/UserService.java +++ b/src/main/java/ru/ulstu/user/UserService.java @@ -5,22 +5,32 @@ import org.slf4j.LoggerFactory; 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.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import ru.ulstu.model.User; +import ru.ulstu.model.UserRole; +import ru.ulstu.model.UserRoleConstants; import java.util.Collections; import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; @Service @Transactional public class UserService implements UserDetailsService { private final Logger log = LoggerFactory.getLogger(UserService.class); + private final PasswordEncoder passwordEncoder; private final UserRepository userRepository; + private final UserRoleRepository userRoleRepository; - public UserService(UserRepository userRepository) { + public UserService(PasswordEncoder passwordEncoder, + UserRepository userRepository, + UserRoleRepository userRoleRepository) { + this.passwordEncoder = passwordEncoder; this.userRepository = userRepository; + this.userRoleRepository = userRoleRepository; } public User getUserByLogin(String login) { @@ -39,4 +49,24 @@ public class UserService implements UserDetailsService { .map(role -> new SimpleGrantedAuthority(role.getName())) .collect(Collectors.toList())); } + + public User createUser(User user) { + if (getUserByLogin(user.getLogin()) != null) { + throw new RuntimeException(user.getLogin()); + } + //user.setRoles(Collections.singleton(new UserRole(UserRoleConstants.USER))); + user.setPassword(passwordEncoder.encode(user.getPassword())); + user = userRepository.save(user); + log.debug("Created Information for User: {}", user.getLogin()); + return user; + } + + public void initDefaultAdmin() { + String adminLogin = "admin"; + String adminPassword = "adminadmin"; + if (getUserByLogin(adminLogin) == null) { + UserRole adminRole = userRoleRepository.save(new UserRole(UserRoleConstants.ADMIN)); + createUser(new User(adminLogin, adminPassword, Set.of(adminRole))); + } + } }