UserServiceImpl.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2019 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. * >>
  17. *
  18. */
  19. package edp.davinci.service.impl;
  20. import com.alibaba.druid.util.StringUtils;
  21. import com.alibaba.fastjson.JSONObject;
  22. import com.jayway.jsonpath.JsonPath;
  23. import edp.core.consts.Consts;
  24. import edp.core.enums.HttpCodeEnum;
  25. import edp.core.enums.MailContentTypeEnum;
  26. import edp.core.exception.NotFoundException;
  27. import edp.core.exception.ServerException;
  28. import edp.core.model.MailContent;
  29. import edp.core.utils.*;
  30. import edp.davinci.core.common.Constants;
  31. import edp.davinci.core.common.ErrorMsg;
  32. import edp.davinci.core.common.ResultMap;
  33. import edp.davinci.core.enums.CheckEntityEnum;
  34. import edp.davinci.core.enums.LockType;
  35. import edp.davinci.core.enums.UserDistinctType;
  36. import edp.davinci.core.enums.UserOrgRoleEnum;
  37. import edp.davinci.dao.OrganizationMapper;
  38. import edp.davinci.dao.RelUserOrganizationMapper;
  39. import edp.davinci.dao.UserMapper;
  40. import edp.davinci.dto.organizationDto.OrganizationInfo;
  41. import edp.davinci.dto.userDto.*;
  42. import edp.davinci.model.LdapPerson;
  43. import edp.davinci.model.Organization;
  44. import edp.davinci.model.RelUserOrganization;
  45. import edp.davinci.model.User;
  46. import edp.davinci.service.LdapService;
  47. import edp.davinci.service.UserService;
  48. import lombok.extern.slf4j.Slf4j;
  49. import org.mindrot.jbcrypt.BCrypt;
  50. import org.springframework.beans.BeanUtils;
  51. import org.springframework.beans.factory.annotation.Autowired;
  52. import org.springframework.core.env.Environment;
  53. import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
  54. import org.springframework.security.oauth2.core.user.OAuth2User;
  55. import org.springframework.stereotype.Service;
  56. import org.springframework.transaction.annotation.Transactional;
  57. import org.springframework.web.multipart.MultipartFile;
  58. import javax.servlet.http.HttpServletRequest;
  59. import java.util.*;
  60. import java.util.regex.Matcher;
  61. @Slf4j
  62. @Service("userService")
  63. public class UserServiceImpl extends BaseEntityService implements UserService {
  64. @Autowired
  65. private UserMapper userMapper;
  66. @Autowired
  67. private OrganizationMapper organizationMapper;
  68. @Autowired
  69. private RelUserOrganizationMapper relUserOrganizationMapper;
  70. @Autowired
  71. private TokenUtils tokenUtils;
  72. @Autowired
  73. private MailUtils mailUtils;
  74. @Autowired
  75. private FileUtils fileUtils;
  76. @Autowired
  77. private ServerUtils serverUtils;
  78. @Autowired
  79. private LdapService ldapService;
  80. @Autowired
  81. private Environment environment;
  82. private static final CheckEntityEnum entity = CheckEntityEnum.USER;
  83. private static final Long TOKEN_TIMEOUT_MILLIS = 10 * 60 * 1000L;
  84. /**
  85. * 用户是否存在
  86. *
  87. * @param name
  88. * @param scopeId
  89. * @return
  90. */
  91. @Override
  92. public boolean isExist(String name, Long id, Long scopeId) {
  93. Long userId = userMapper.getIdByName(name);
  94. if (null != id && null != userId) {
  95. return !id.equals(userId);
  96. }
  97. return null != userId && userId.longValue() > 0L;
  98. }
  99. /**
  100. * 用户注册接口
  101. *
  102. * @param userRegist
  103. * @return
  104. */
  105. @Override
  106. @Transactional
  107. public User regist(UserRegist userRegist) throws ServerException {
  108. String username = userRegist.getUsername();
  109. //用户名是否已经注册
  110. if (isExist(username, null, null)) {
  111. log.info("The username({}) has been registered", username);
  112. throw new ServerException("The username:" + username + " has been registered");
  113. }
  114. String email = userRegist.getEmail();
  115. //邮箱是否已经注册
  116. // if (isExist(email, null, null)) {
  117. // log.info("The email({}) has been registered", email);
  118. // throw new ServerException("The email:" + email + " has been registered");
  119. // }
  120. BaseLock usernameLock = getLock(entity, username, null);
  121. if (usernameLock != null && !usernameLock.getLock()) {
  122. alertNameTaken(entity, username);
  123. }
  124. BaseLock emailLock = null;
  125. if (!username.toLowerCase().equals(email.toLowerCase())) {
  126. emailLock = getLock(entity, email, null);
  127. }
  128. if (emailLock != null && !emailLock.getLock()) {
  129. alertNameTaken(entity, email);
  130. }
  131. try {
  132. User user = new User();
  133. //密码加密
  134. userRegist.setPassword(BCrypt.hashpw(userRegist.getPassword(), BCrypt.gensalt()));
  135. BeanUtils.copyProperties(userRegist, user);
  136. //添加用户
  137. if (userMapper.insert(user) <= 0) {
  138. log.info("Regist fail, userRegist:{}", userRegist.toString());
  139. throw new ServerException("Regist fail, unspecified error");
  140. }
  141. //添加成功,发送激活邮件
  142. // sendMail(user.getEmail(), user);
  143. return user;
  144. } finally {
  145. releaseLock(usernameLock);
  146. releaseLock(emailLock);
  147. }
  148. }
  149. @Override
  150. public User externalRegist(OAuth2AuthenticationToken oauthAuthToken) throws ServerException {
  151. OAuth2User oauthUser = oauthAuthToken.getPrincipal();
  152. User user = getByUsername(oauthUser.getName());
  153. if (user != null) {
  154. return user;
  155. }
  156. user = new User();
  157. String emailMapping = environment.getProperty(String.format("spring.security.oauth2.client.provider.%s.userMapping.email", oauthAuthToken.getAuthorizedClientRegistrationId()));
  158. String nameMapping = environment.getProperty(String.format("spring.security.oauth2.client.provider.%s.userMapping.name", oauthAuthToken.getAuthorizedClientRegistrationId()));
  159. String avatarMapping = environment.getProperty(String.format("spring.security.oauth2.client.provider.%s.userMapping.avatar", oauthAuthToken.getAuthorizedClientRegistrationId()));
  160. JSONObject jsonObj = new JSONObject(oauthUser.getAttributes());
  161. user.setName(JsonPath.read(jsonObj, nameMapping));
  162. user.setUsername(oauthUser.getName());
  163. user.setPassword("OAuth2");
  164. user.setEmail(JsonPath.read(jsonObj, emailMapping));
  165. user.setAvatar(JsonPath.read(jsonObj, avatarMapping));
  166. int insert = userMapper.insert(user);
  167. if (insert > 0) {
  168. return user;
  169. } else {
  170. log.info("Regist fail, username:{}", oauthUser.getName());
  171. throw new ServerException("Regist fail, unspecified error");
  172. }
  173. }
  174. protected void alertNameTaken(CheckEntityEnum entity, String name) throws ServerException {
  175. log.warn("The {} username or email {} has been registered", entity.getSource(), name);
  176. throw new ServerException("The " + entity.getSource() + " username or email has been registered");
  177. }
  178. /**
  179. * 根据用户名获取用户
  180. *
  181. * @param username
  182. * @return
  183. */
  184. @Override
  185. public User getByUsername(String username) {
  186. return userMapper.selectByUsername(username);
  187. }
  188. /**
  189. * 用户登录
  190. *
  191. * @param userLogin
  192. * @return
  193. */
  194. @Override
  195. public User userLogin(UserLogin userLogin) throws ServerException {
  196. String username = userLogin.getUsername();
  197. String password = userLogin.getPassword();
  198. User user = getByUsername(username);
  199. if (user != null) {
  200. // 校验密码
  201. boolean checkpw = false;
  202. try {
  203. checkpw = BCrypt.checkpw(password, user.getPassword());
  204. } catch (Exception e) {
  205. }
  206. if (checkpw) {
  207. return user;
  208. }
  209. if (ldapLogin(username, password)) {
  210. return user;
  211. }
  212. log.info("Username({}) password is wrong", username);
  213. throw new ServerException("Username or password is wrong");
  214. }
  215. user = ldapAutoRegist(username, password);
  216. if (user == null) {
  217. throw new ServerException("Username or password is wrong");
  218. }
  219. return user;
  220. }
  221. private boolean ldapLogin(String username, String password) {
  222. if (!ldapService.existLdapServer()) {
  223. return false;
  224. }
  225. LdapPerson ldapPerson = ldapService.findByUsername(username, password);
  226. if (null == ldapPerson) {
  227. return false;
  228. }
  229. return true;
  230. }
  231. private User ldapAutoRegist(String username, String password) {
  232. if (!ldapService.existLdapServer()) {
  233. return null;
  234. }
  235. LdapPerson ldapPerson = ldapService.findByUsername(username, password);
  236. if (null == ldapPerson) {
  237. throw new ServerException("Username or password is wrong");
  238. }
  239. String email = ldapPerson.getEmail();
  240. if (userMapper.existEmail(ldapPerson.getEmail())) {
  241. log.info("Ldap auto regist fail, the email {} has been registered", email);
  242. throw new ServerException("Ldap auto regist fail: the email " + email + " has been registered");
  243. }
  244. if (userMapper.existUsername(ldapPerson.getSAMAccountName())) {
  245. ldapPerson.setSAMAccountName(email);
  246. }
  247. return ldapService.registPerson(ldapPerson);
  248. }
  249. /**
  250. * 查询用户
  251. *
  252. * @param keyword
  253. * @param user
  254. * @param orgId
  255. * @param includeSelf
  256. * @return
  257. */
  258. @Override
  259. public List<UserBaseInfo> getUsersByKeyword(String keyword, User user, Long orgId, Boolean includeSelf) {
  260. List<UserBaseInfo> users = userMapper.getUsersByKeyword(keyword, orgId);
  261. if (includeSelf) {
  262. return users;
  263. }
  264. Iterator<UserBaseInfo> iterator = users.iterator();
  265. while (iterator.hasNext()) {
  266. UserBaseInfo userBaseInfo = iterator.next();
  267. if (userBaseInfo.getId().equals(user.getId())) {
  268. iterator.remove();
  269. }
  270. }
  271. return users;
  272. }
  273. /**
  274. * 更新用户
  275. *
  276. * @param user
  277. * @return
  278. */
  279. @Override
  280. @Transactional
  281. public boolean updateUser(User user) throws ServerException {
  282. if (userMapper.updateBaseInfo(user) <= 0) {
  283. log.info("Update user fail, username:{}", user.getUsername());
  284. throw new ServerException("Update user fail");
  285. }
  286. return true;
  287. }
  288. @Override
  289. @Transactional
  290. public ResultMap activateUserNoLogin(String token, HttpServletRequest request) {
  291. ResultMap resultMap = new ResultMap(tokenUtils);
  292. token = AESUtils.decrypt(token, null);
  293. String username = tokenUtils.getUsername(token);
  294. if (null == username) {
  295. return resultMap.fail().message("The activate toke is invalid");
  296. }
  297. User user = getByUsername(username);
  298. if (null == user) {
  299. return resultMap.fail().message("The activate toke is invalid");
  300. }
  301. // 已经激活,不需要再次激活
  302. if (user.getActive()) {
  303. return resultMap.fail().message("The current user is activated and doesn't need to be reactivated");
  304. }
  305. BaseLock lock = LockFactory.getLock("ACTIVATE" + Consts.AT_SYMBOL + username.toUpperCase(), 5, LockType.REDIS);
  306. if (lock != null && !lock.getLock()) {
  307. return resultMap.fail().message("The current user is activating");
  308. }
  309. try {
  310. // 验证激活token
  311. if (tokenUtils.validateToken(token, user)) {
  312. user.setActive(true);
  313. user.setUpdateTime(new Date());
  314. userMapper.activeUser(user);
  315. String orgName = user.getUsername() + "'s Organization";
  316. // 激活成功,创建默认Organization
  317. Organization organization = new Organization(orgName, null, user.getId());
  318. organizationMapper.insert(organization);
  319. // 关联用户和组织,创建人是组织的owner
  320. RelUserOrganization relUserOrganization = new RelUserOrganization(organization.getId(), user.getId(),
  321. UserOrgRoleEnum.OWNER.getRole());
  322. relUserOrganization.createdBy(user.getId());
  323. relUserOrganizationMapper.insert(relUserOrganization);
  324. UserLoginResult userLoginResult = new UserLoginResult();
  325. BeanUtils.copyProperties(user, userLoginResult);
  326. return resultMap.success(tokenUtils.generateToken(user)).payload(userLoginResult);
  327. }
  328. return resultMap.fail().message("The activate toke is invalid");
  329. } finally {
  330. releaseLock(lock);
  331. }
  332. }
  333. /**
  334. * 发送邮件
  335. *
  336. * @param email
  337. * @param user
  338. * @return
  339. */
  340. @Override
  341. public boolean sendMail(String email, User user) throws ServerException {
  342. //校验邮箱
  343. if (!email.equals(user.getEmail())) {
  344. throw new ServerException("The current email address is not match user email address");
  345. }
  346. Map<String, Object> content = new HashMap<String, Object>();
  347. content.put("username", user.getUsername());
  348. content.put("host", serverUtils.getHost());
  349. content.put("token", AESUtils.encrypt(tokenUtils.generateContinuousToken(user), null));
  350. MailContent mailContent = MailContent.MailContentBuilder.builder()
  351. .withSubject(Constants.USER_ACTIVATE_EMAIL_SUBJECT)
  352. .withTo(user.getEmail())
  353. .withMainContent(MailContentTypeEnum.TEMPLATE)
  354. .withTemplate(Constants.USER_ACTIVATE_EMAIL_TEMPLATE)
  355. .withTemplateContent(content)
  356. .build();
  357. mailUtils.sendMail(mailContent, null);
  358. return true;
  359. }
  360. /**
  361. * 修改用户密码
  362. *
  363. * @param user
  364. * @param oldPassword
  365. * @param password
  366. * @param request
  367. * @return
  368. */
  369. @Override
  370. @Transactional
  371. public ResultMap changeUserPassword(User user, String oldPassword, String password, HttpServletRequest request) {
  372. ResultMap resultMap = new ResultMap(tokenUtils);
  373. //校验原密码
  374. if (!BCrypt.checkpw(oldPassword, user.getPassword())) {
  375. return resultMap.failAndRefreshToken(request).message("Incorrect original password");
  376. }
  377. //设置新密码
  378. user.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
  379. user.setUpdateTime(new Date());
  380. if (userMapper.changePassword(user) > 0) {
  381. return resultMap.success().message("Successful password modification");
  382. }
  383. return resultMap.failAndRefreshToken(request);
  384. }
  385. /**
  386. * 上传头像
  387. *
  388. * @param user
  389. * @param file
  390. * @param request
  391. * @return
  392. */
  393. @Override
  394. @Transactional
  395. public ResultMap uploadAvatar(User user, MultipartFile file, HttpServletRequest request) {
  396. ResultMap resultMap = new ResultMap(tokenUtils);
  397. //校验文件是否图片
  398. if (!fileUtils.isImage(file)) {
  399. return resultMap.failAndRefreshToken(request).message("File format error");
  400. }
  401. //上传文件
  402. String fileName = user.getUsername() + "_" + UUID.randomUUID();
  403. String avatar = null;
  404. try {
  405. avatar = fileUtils.upload(file, Constants.USER_AVATAR_PATH, fileName);
  406. if (StringUtils.isEmpty(avatar)) {
  407. return resultMap.failAndRefreshToken(request).message("User avatar upload error");
  408. }
  409. } catch (Exception e) {
  410. log.error("User avatar upload error, username:{}", user.getUsername(), e);
  411. return resultMap.failAndRefreshToken(request).message("User avatar upload error");
  412. }
  413. //删除原头像
  414. if (!StringUtils.isEmpty(user.getAvatar())) {
  415. fileUtils.remove(user.getAvatar());
  416. }
  417. //修改用户头像
  418. user.setAvatar(avatar);
  419. user.setUpdateTime(new Date());
  420. if (userMapper.updateAvatar(user) > 0) {
  421. Map<String, String> map = new HashMap<>();
  422. map.put("avatar", avatar);
  423. return resultMap.successAndRefreshToken(request).payload(map);
  424. }
  425. return resultMap.failAndRefreshToken(request).message("Server error, user avatar update fail");
  426. }
  427. /**
  428. * 查询用户信息
  429. *
  430. * @param id
  431. * @param user
  432. * @param request
  433. * @return
  434. */
  435. @Override
  436. public ResultMap getUserProfile(Long id, User user, HttpServletRequest request) {
  437. ResultMap resultMap = new ResultMap(tokenUtils);
  438. User tempUser = userMapper.getById(id);
  439. if (null == tempUser) {
  440. return resultMap.failAndRefreshToken(request).message("User not found");
  441. }
  442. UserProfile userProfile = new UserProfile();
  443. BeanUtils.copyProperties(tempUser, userProfile);
  444. if (id.equals(user.getId())) {
  445. List<OrganizationInfo> organizationInfos = organizationMapper.getOrganizationByUser(user.getId());
  446. userProfile.setOrganizations(organizationInfos);
  447. return resultMap.successAndRefreshToken(request).payload(userProfile);
  448. }
  449. Long[] userIds = {user.getId(), id};
  450. List<OrganizationInfo> jointlyOrganization = organizationMapper.getJointlyOrganization(Arrays.asList(userIds), id);
  451. if (!CollectionUtils.isEmpty(jointlyOrganization)) {
  452. BeanUtils.copyProperties(tempUser, userProfile);
  453. userProfile.setOrganizations(jointlyOrganization);
  454. return resultMap.successAndRefreshToken(request).payload(userProfile);
  455. }
  456. return resultMap.failAndRefreshToken(request, HttpCodeEnum.UNAUTHORIZED).message("You have not permission to view the user's information because you don't have any organizations that join together");
  457. }
  458. @Override
  459. public ResultMap getUserProfileFromToken(String token) {
  460. String username = tokenUtils.getUsername(Constants.TOKEN_PREFIX + Constants.SPACE + token);
  461. User user = getByUsername(username);
  462. if (null == user) {
  463. return new ResultMap().fail(HttpCodeEnum.FORBIDDEN.getCode()).message(ErrorMsg.ERR_MSG_PERMISSION);
  464. }
  465. if (!tokenUtils.validateToken(token, user)) {
  466. return new ResultMap().fail(HttpCodeEnum.FORBIDDEN.getCode()).message(ErrorMsg.ERR_MSG_PERMISSION);
  467. }
  468. UserProfile userProfile = new UserProfile();
  469. BeanUtils.copyProperties(user, userProfile);
  470. List<OrganizationInfo> organizationInfos = organizationMapper.getOrganizationByUser(user.getId());
  471. userProfile.setOrganizations(organizationInfos);
  472. return new ResultMap().success(tokenUtils.generateToken(user)).payload(userProfile);
  473. }
  474. @Override
  475. public String forgetPassword(UserDistinctType userDistinctType, UserDistinctTicket ticket) {
  476. User user = null;
  477. switch (userDistinctType) {
  478. case EMAIL:
  479. String email = ticket.getTicket();
  480. if (StringUtils.isEmpty(email)) {
  481. throw new ServerException("Email cannot be empty!");
  482. }
  483. Matcher matcher = Constants.PATTERN_EMAIL_FORMAT.matcher(email);
  484. if (!matcher.find()) {
  485. throw new ServerException("Invalid email format!");
  486. }
  487. user = userMapper.selectByUsername(email);
  488. if (user == null) {
  489. throw new ServerException("The current email is not registered in Davinci");
  490. }
  491. break;
  492. case USERNAME:
  493. String username = ticket.getTicket();
  494. if (StringUtils.isEmpty(username)) {
  495. throw new ServerException("Username cannot be EMPTY!");
  496. }
  497. user = userMapper.selectByUsername(username);
  498. if (user == null) {
  499. throw new ServerException("The current username is not registered in Davinci");
  500. }
  501. break;
  502. default:
  503. throw new NotFoundException("Unknown request uri");
  504. }
  505. String checkCode = TokenUtils.randomPassword();
  506. user.setPassword(checkCode);
  507. String checkToken = tokenUtils.generateToken(user, TOKEN_TIMEOUT_MILLIS);
  508. Map<String, Object> content = new HashMap<>(3);
  509. content.put("ticket", ticket.getTicket());
  510. content.put("checkCode", checkCode);
  511. MailContent mailContent = MailContent.MailContentBuilder.builder()
  512. .withSubject(Constants.USER_REST_PASSWORD_EMAIL_SUBJECT)
  513. .withTo(user.getEmail())
  514. .withMainContent(MailContentTypeEnum.TEMPLATE)
  515. .withTemplate(Constants.USER_REST_PASSWORD_EMAIL_TEMPLATE)
  516. .withTemplateContent(content)
  517. .build();
  518. mailUtils.sendMail(mailContent, null);
  519. return StringZipUtil.compress(checkToken);
  520. }
  521. @Override
  522. @Transactional
  523. public boolean resetPassword(UserDistinctType userDistinctType, String token, UserDistinctTicket ticket) {
  524. User user = null;
  525. switch (userDistinctType) {
  526. case EMAIL:
  527. String email = ticket.getTicket();
  528. if (StringUtils.isEmpty(email)) {
  529. throw new ServerException("Email cannot be EMPTY!");
  530. }
  531. Matcher matcher = Constants.PATTERN_EMAIL_FORMAT.matcher(email);
  532. if (!matcher.find()) {
  533. throw new ServerException("Invalid email format!");
  534. }
  535. user = userMapper.selectByUsername(email);
  536. if (user == null) {
  537. throw new ServerException("The current email is not registered in Davinci");
  538. }
  539. break;
  540. case USERNAME:
  541. String username = ticket.getTicket();
  542. if (StringUtils.isEmpty(username)) {
  543. throw new ServerException("Username cannot be EMPTY!");
  544. }
  545. user = userMapper.selectByUsername(username);
  546. if (user == null) {
  547. throw new ServerException("The current username is not registered in Davinci");
  548. }
  549. break;
  550. default:
  551. throw new NotFoundException("Unknown request uri");
  552. }
  553. if (StringUtils.isEmpty(ticket.getCheckCode())) {
  554. throw new ServerException("Check code cannot be Empty");
  555. }
  556. if (StringUtils.isEmpty(ticket.getPassword())) {
  557. throw new ServerException("Password cannot be Empty");
  558. }
  559. String decompress = StringZipUtil.decompress(token);
  560. user.setPassword(ticket.getCheckCode());
  561. if (!tokenUtils.validateToken(decompress, user)) {
  562. throw new ServerException("Invalid check code, check code is wrong or has expired");
  563. }
  564. user.setPassword(BCrypt.hashpw(ticket.getPassword(), BCrypt.gensalt()));
  565. return userMapper.changePassword(user) > 0;
  566. }
  567. }