Revize d411f143
Přidáno uživatelem Vojtěch Danišík před asi 4 roky(ů)
src/main/java/vldc/aswi/service/UserManagerImpl.java | ||
---|---|---|
5 | 5 |
import org.springframework.context.event.ContextRefreshedEvent; |
6 | 6 |
import org.springframework.context.event.EventListener; |
7 | 7 |
import org.springframework.core.annotation.Order; |
8 |
import org.springframework.security.core.userdetails.UserDetails; |
|
9 |
import org.springframework.security.core.userdetails.UserDetailsService; |
|
10 |
import org.springframework.security.core.userdetails.UsernameNotFoundException; |
|
11 |
import org.springframework.security.crypto.password.PasswordEncoder; |
|
8 | 12 |
import org.springframework.stereotype.Service; |
9 | 13 |
import vldc.aswi.dao.RoleRepository; |
10 | 14 |
import vldc.aswi.dao.UserRepository; |
11 | 15 |
import vldc.aswi.domain.User; |
16 |
import vldc.aswi.model.login.WebCredentials; |
|
12 | 17 |
|
13 | 18 |
import javax.transaction.Transactional; |
14 | 19 |
import java.util.LinkedList; |
... | ... | |
19 | 24 |
*/ |
20 | 25 |
@Service |
21 | 26 |
@Slf4j |
22 |
public class UserManagerImpl implements UserManager { |
|
27 |
public class UserManagerImpl implements UserManager, UserDetailsService { |
|
28 |
|
|
29 |
@Autowired |
|
30 |
private PasswordEncoder encoder; |
|
23 | 31 |
|
24 | 32 |
/** Autowired user repository. */ |
25 | 33 |
@Autowired |
... | ... | |
32 | 40 |
/** |
33 | 41 |
* Initialization setup for user manager. Check if user repository contains records and if not, initialize default values. |
34 | 42 |
*/ |
35 |
/* |
|
36 | 43 |
@EventListener(classes = { |
37 | 44 |
ContextRefreshedEvent.class |
38 | 45 |
}) |
... | ... | |
40 | 47 |
@Transactional |
41 | 48 |
public void setup() { |
42 | 49 |
if (this.userRepository.count() == 0) { |
43 |
log.info("No user present, creating 'admin', 'referentka', 'prorektor'."); |
|
44 |
Long idAdmin = this.addUser("admin", "admin"); |
|
45 |
Long idReferentka = this.addUser("referentka", "referentka"); |
|
46 |
Long idProrektor = this.addUser("prorektor", "prorektor"); |
|
47 |
|
|
48 |
User admin = this.userRepository.getById(idAdmin); |
|
49 |
User referentka = this.userRepository.getById(idReferentka); |
|
50 |
User prorektor = this.userRepository.getById(idProrektor); |
|
51 |
|
|
52 |
//admin.setRole(); |
|
50 |
log.info("No user present"); |
|
51 |
/* |
|
52 |
this.addUser("administrator", "demo", (long) 1); |
|
53 |
this.addUser("fakulta", "demo", (long) 2); |
|
54 |
this.addUser("referentka", "demo", (long) 3); |
|
55 |
this.addUser("prorektor", "demo", (long) 4); |
|
56 |
*/ |
|
53 | 57 |
} |
54 | 58 |
} |
59 |
|
|
60 |
/** |
|
61 |
* Load user details by his username. |
|
62 |
* @param username Username of user. |
|
63 |
* @return UserDetails of user. |
|
64 |
* @throws UsernameNotFoundException Exception. |
|
55 | 65 |
*/ |
66 |
@Override |
|
67 |
@Transactional |
|
68 |
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { |
|
69 |
User user = this.userRepository.findByUsername(username); |
|
70 |
if (user == null) { |
|
71 |
throw new UsernameNotFoundException("Invalid username!"); |
|
72 |
} |
|
73 |
|
|
74 |
WebCredentials credentials = new WebCredentials(user.getUsername(), user.getPassword()); |
|
75 |
credentials.addRole(user.getRole().getName()); |
|
76 |
|
|
77 |
return credentials; |
|
78 |
} |
|
56 | 79 |
|
57 | 80 |
/** |
58 | 81 |
* Get all users from database. |
... | ... | |
72 | 95 |
* @return ID of newly created user. |
73 | 96 |
*/ |
74 | 97 |
@Override |
75 |
public Long addUser(String username, String password) { |
|
76 |
User user = new User(username, password); |
|
98 |
public Long addUser(String username, String password, Long roleId) { |
|
99 |
String hashed = this.encoder.encode(password); |
|
100 |
User user = new User(username, hashed); |
|
101 |
user.setRole(this.roleRepository.getById(roleId)); |
|
77 | 102 |
this.userRepository.save(user); |
78 | 103 |
return user.getId(); |
79 | 104 |
} |
Také k dispozici: Unified diff
re #8014 Login into app + session for user.