|
|
@@ -6,7 +6,12 @@ import org.apache.commons.codec.digest.DigestUtils;
|
|
|
import org.gitlab4j.api.GitLabApiException;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.DependsOn;
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
+import org.springframework.security.core.userdetails.UserDetails;
|
|
|
+import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
+import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.Assert;
|
|
|
@@ -29,20 +34,25 @@ import seecoder.devcloud.web.service.user.UserService;
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
-public class UserServiceImpl implements UserService {
|
|
|
+public class UserServiceImpl implements UserService, UserDetailsService {
|
|
|
private static final Sort PAGE_SORT = Sort.by(Sort.Direction.ASC, "id");
|
|
|
|
|
|
private final UserMapper userMapper;
|
|
|
private final MailService mailService;
|
|
|
private final GitlabApi gitlabApi;
|
|
|
private final JenkinsApi jenkinsApi;
|
|
|
+ /**
|
|
|
+ * spring security提供的加密
|
|
|
+ */
|
|
|
+ private final PasswordEncoder passwordEncoder;
|
|
|
|
|
|
@Autowired
|
|
|
- public UserServiceImpl(UserMapper userMapper, MailService mailService, GitlabApi gitlabApi, JenkinsApi jenkinsApi) {
|
|
|
+ public UserServiceImpl(UserMapper userMapper, MailService mailService, GitlabApi gitlabApi, JenkinsApi jenkinsApi, PasswordEncoder passwordEncoder) {
|
|
|
this.userMapper = userMapper;
|
|
|
this.mailService = mailService;
|
|
|
this.gitlabApi = gitlabApi;
|
|
|
this.jenkinsApi = jenkinsApi;
|
|
|
+ this.passwordEncoder = passwordEncoder;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -79,7 +89,7 @@ public class UserServiceImpl implements UserService {
|
|
|
user.setUsername(username);
|
|
|
user.setEmail(email);
|
|
|
//在gitlab注册后,直接加密
|
|
|
- user.setPassword(DigestUtils.sha256Hex(realPassword));
|
|
|
+ user.setPassword(passwordEncoder.encode(realPassword));
|
|
|
user.setRole(role);
|
|
|
userMapper.insert(user, "created_at");
|
|
|
final UserVO userVO = new UserVO(user);
|
|
|
@@ -125,13 +135,13 @@ public class UserServiceImpl implements UserService {
|
|
|
public void changePassword(ChangePasswordVO form) throws ServiceException {
|
|
|
User user = userMapper.findById(form.getUserId());
|
|
|
|
|
|
- if (!user.getPassword().equals(DigestUtils.sha256Hex(form.getOrigin()))) {
|
|
|
+ if (!passwordEncoder.matches(form.getOrigin(),user.getPassword())) {
|
|
|
throw new InvalidRequestException("原密码验证失败,如忘记密码请联系管理员重置");
|
|
|
}
|
|
|
- changePassword(user, form.getPassword(), false);
|
|
|
+ changePassword(user, form.getPassword());
|
|
|
}
|
|
|
|
|
|
- private void changePassword(User user, String password, boolean reset) throws ServiceException {
|
|
|
+ private void changePassword(User user, String password) throws ServiceException {
|
|
|
try {
|
|
|
/**
|
|
|
* todo 修改密码容易导致gitlab与devcloud不一致
|
|
|
@@ -146,10 +156,18 @@ public class UserServiceImpl implements UserService {
|
|
|
log.error(e.getMessage(), e);
|
|
|
throw new ServiceException("修改GitLab用户密码时发生异常[" + e.getMessage() + "]");
|
|
|
}
|
|
|
- user.setPassword(DigestUtils.sha256Hex(password));
|
|
|
+ user.setPassword(passwordEncoder.encode(password));
|
|
|
userMapper.updateById(user);
|
|
|
- if (reset) {
|
|
|
- mailService.send( "您的新密码为"+password,"密码变更提醒 | SEECODER",user.getEmail());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
|
|
+ UserDetails u = userMapper.findByUsername(username);
|
|
|
+ if (u == null){
|
|
|
+ throw new UsernameNotFoundException("此账号不存在");
|
|
|
}
|
|
|
+ return u;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|