Repository navigation
Expand file tree
/
Copy pathUserService.java
More file actions
108 lines (82 loc) · 3.66 KB
/
Copy pathUserService.java
File metadata and controls
108 lines (82 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.raysmond.blog.services;
import java.util.Collections;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import com.raysmond.blog.Constants;
import com.raysmond.blog.models.User;
import com.raysmond.blog.repositories.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.*;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.*;
import org.springframework.security.crypto.password.PasswordEncoder;
public class UserService implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Inject
private PasswordEncoder passwordEncoder;
private static final Logger logger = LoggerFactory.getLogger(UserService.class);
@PostConstruct
protected void initialize() {
getSuperUser();
}
public User createUser(User user){
user.setPassword(passwordEncoder.encode(user.getPassword()));
return userRepository.save(user);
}
public User getSuperUser(){
User user = userRepository.findByEmail(Constants.DEFAULT_ADMIN_EMAIL);
if ( user == null) {
user = createUser(new User(Constants.DEFAULT_ADMIN_EMAIL, Constants.DEFAULT_ADMIN_PASSWORD, User.ROLE_ADMIN));
}
return user;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByEmail(username);
if (user == null) {
throw new UsernameNotFoundException("user not found");
}
return createSpringUser(user);
}
public User currentUser(){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if(auth == null || auth instanceof AnonymousAuthenticationToken){
return null;
}
String email = ((org.springframework.security.core.userdetails.User) auth.getPrincipal()).getUsername();
return userRepository.findByEmail(email);
}
public boolean changePassword(User user, String password, String newPassword){
if (password == null || newPassword == null || password.isEmpty() || newPassword.isEmpty())
return false;
logger.info("" + passwordEncoder.matches(password, user.getPassword()));
boolean match = passwordEncoder.matches(password, user.getPassword());
if (!match)
return false;
user.setPassword(passwordEncoder.encode(newPassword));
userRepository.save(user);
logger.info("User @"+user.getEmail() + " changed password.");
return true;
}
public void signin(User user) {
SecurityContextHolder.getContext().setAuthentication(authenticate(user));
}
private Authentication authenticate(User user) {
return new UsernamePasswordAuthenticationToken(createSpringUser(user), null, Collections.singleton(createAuthority(user)));
}
private org.springframework.security.core.userdetails.User createSpringUser(User user) {
return new org.springframework.security.core.userdetails.User(
user.getEmail(),
user.getPassword(),
Collections.singleton(createAuthority(user)));
}
private GrantedAuthority createAuthority(User user) {
return new SimpleGrantedAuthority(user.getRole());
}
}