авторизация #4

Merged
romanov73 merged 7 commits from 3-auth into master 2022-03-11 13:11:29 +04:00
3 changed files with 52 additions and 1 deletions
Showing only changes of commit 2e5880f1a6 - Show all commits

View File

@ -2,10 +2,25 @@ package ru.ulstu;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; 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 @SpringBootApplication
public class SeminarApplication { public class SeminarApplication {
private final UserService userService;
public SeminarApplication(UserService userService) {
this.userService = userService;
}
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SeminarApplication.class, args); SpringApplication.run(SeminarApplication.class, args);
} }
@EventListener(ApplicationReadyEvent.class)
public void doSomethingAfterStartup() {
System.out.println("hello world, I have just started up");
userService.initDefaultAdmin();
}
} }

View File

@ -39,6 +39,12 @@ public class User extends BaseEntity {
roles = new HashSet<>(); roles = new HashSet<>();
} }
public User(String login, String password, Set<UserRole> roles) {
this.login = login;
this.password = password;
this.roles = roles;
}
public String getLogin() { public String getLogin() {
return login; return login;
} }

View File

@ -5,22 +5,32 @@ import org.slf4j.LoggerFactory;
import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.model.User; import ru.ulstu.model.User;
import ru.ulstu.model.UserRole;
import ru.ulstu.model.UserRoleConstants;
import java.util.Collections; import java.util.Collections;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Service @Service
@Transactional @Transactional
public class UserService implements UserDetailsService { public class UserService implements UserDetailsService {
private final Logger log = LoggerFactory.getLogger(UserService.class); private final Logger log = LoggerFactory.getLogger(UserService.class);
private final PasswordEncoder passwordEncoder;
private final UserRepository userRepository; 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.userRepository = userRepository;
this.userRoleRepository = userRoleRepository;
} }
public User getUserByLogin(String login) { public User getUserByLogin(String login) {
@ -39,4 +49,24 @@ public class UserService implements UserDetailsService {
.map(role -> new SimpleGrantedAuthority(role.getName())) .map(role -> new SimpleGrantedAuthority(role.getName()))
.collect(Collectors.toList())); .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)));
}
}
} }