UserInfoService.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package org.maxkey.dao.service;
  2. import org.apache.mybatis.jpa.persistence.JpaBaseService;
  3. import org.maxkey.constants.ConstantsStatus;
  4. import org.maxkey.crypto.ReciprocalUtils;
  5. import org.maxkey.crypto.password.PasswordReciprocal;
  6. import org.maxkey.dao.persistence.UserInfoMapper;
  7. import org.maxkey.domain.ChangePassword;
  8. import org.maxkey.domain.UserInfo;
  9. import org.maxkey.identity.kafka.KafkaIdentityAction;
  10. import org.maxkey.identity.kafka.KafkaIdentityTopic;
  11. import org.maxkey.identity.kafka.KafkaProvisioningService;
  12. import org.maxkey.util.DateUtils;
  13. import org.maxkey.util.StringUtils;
  14. import org.maxkey.web.WebContext;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.security.crypto.password.PasswordEncoder;
  19. import org.springframework.stereotype.Service;
  20. /**
  21. * @author Crystal.Sea
  22. *
  23. */
  24. @Service
  25. public class UserInfoService extends JpaBaseService<UserInfo> {
  26. final static Logger _logger = LoggerFactory.getLogger(UserInfoService.class);
  27. @Autowired
  28. private PasswordEncoder passwordEncoder;
  29. @Autowired
  30. KafkaProvisioningService kafkaProvisioningService;
  31. public UserInfoService() {
  32. super(UserInfoMapper.class);
  33. }
  34. /* (non-Javadoc)
  35. * @see com.connsec.db.service.BaseService#getMapper()
  36. */
  37. @Override
  38. public UserInfoMapper getMapper() {
  39. // TODO Auto-generated method stub
  40. return (UserInfoMapper)super.getMapper();
  41. }
  42. public boolean insert(UserInfo userInfo) {
  43. userInfo = passwordEncoder(userInfo);
  44. if (super.insert(userInfo)) {
  45. kafkaProvisioningService.send(
  46. KafkaIdentityTopic.USERINFO_TOPIC, userInfo, KafkaIdentityAction.CREATE_ACTION);
  47. return true;
  48. }
  49. return false;
  50. }
  51. public boolean update(UserInfo userInfo) {
  52. if(super.update(userInfo)){
  53. kafkaProvisioningService.send(
  54. KafkaIdentityTopic.USERINFO_TOPIC, userInfo, KafkaIdentityAction.UPDATE_ACTION);
  55. return true;
  56. }
  57. return false;
  58. }
  59. public boolean delete(UserInfo userInfo) {
  60. if( super.delete(userInfo)){
  61. kafkaProvisioningService.send(
  62. KafkaIdentityTopic.USERINFO_TOPIC, userInfo, KafkaIdentityAction.DELETE_ACTION);
  63. return true;
  64. }
  65. return false;
  66. }
  67. public boolean updateProtectedApps(UserInfo userinfo) {
  68. try {
  69. if(WebContext.getUserInfo() != null) {
  70. userinfo.setModifiedBy(WebContext.getUserInfo().getId());
  71. }
  72. userinfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString());
  73. return getMapper().updateProtectedApps(userinfo) > 0;
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. }
  77. return false;
  78. }
  79. public UserInfo loadByUsername(String username) {
  80. return getMapper().loadByUsername(username);
  81. }
  82. public UserInfo loadByAppIdAndUsername(String appId,String username){
  83. try {
  84. UserInfo userinfo = new UserInfo();
  85. userinfo.setUsername(username);
  86. return getMapper().loadByAppIdAndUsername(userinfo) ;
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. }
  90. return null;
  91. }
  92. public void logisticDeleteAllByCid(String cid){
  93. try {
  94. getMapper().logisticDeleteAllByCid(cid);
  95. } catch(Exception e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. public UserInfo passwordEncoder(UserInfo userInfo) {
  100. String password = passwordEncoder.encode(PasswordReciprocal.getInstance().rawPassword(userInfo.getUsername(), userInfo.getPassword()));
  101. userInfo.setDecipherable(ReciprocalUtils.encode(PasswordReciprocal.getInstance().rawPassword(userInfo.getUsername(), userInfo.getPassword())));
  102. _logger.debug("decipherable : "+userInfo.getDecipherable());
  103. userInfo.setPassword(password);
  104. userInfo.setPasswordLastSetTime(DateUtils.getCurrentDateTimeAsString());
  105. userInfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString());
  106. return userInfo;
  107. }
  108. public boolean changePassword(UserInfo userInfo) {
  109. try {
  110. if(WebContext.getUserInfo() != null) {
  111. userInfo.setModifiedBy(WebContext.getUserInfo().getId());
  112. }
  113. userInfo = passwordEncoder(userInfo);
  114. if(getMapper().changePassword(userInfo) > 0){
  115. ChangePassword changePassword=new ChangePassword();
  116. changePassword.setId(userInfo.getId());
  117. changePassword.setUid(userInfo.getId());
  118. changePassword.setUsername(userInfo.getUsername());
  119. changePassword.setDecipherable(userInfo.getDecipherable());
  120. changePassword.setPassword(userInfo.getPassword());
  121. kafkaProvisioningService.send(
  122. KafkaIdentityTopic.PASSWORD_TOPIC, changePassword, KafkaIdentityAction.PASSWORD_ACTION);
  123. return true;
  124. }
  125. return false;
  126. } catch (Exception e) {
  127. e.printStackTrace();
  128. }
  129. return false;
  130. }
  131. public boolean changeAppLoginPassword(UserInfo userinfo) {
  132. try {
  133. if(WebContext.getUserInfo() != null) {
  134. userinfo.setModifiedBy(WebContext.getUserInfo().getId());
  135. }
  136. userinfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString());
  137. return getMapper().changeAppLoginPassword(userinfo) > 0;
  138. } catch (Exception e) {
  139. e.printStackTrace();
  140. }
  141. return false;
  142. }
  143. /**
  144. * 锁定用户:islock:1 用户解锁 2 用户锁定
  145. * @param userInfo
  146. */
  147. public void locked(UserInfo userInfo) {
  148. try {
  149. if(userInfo != null && StringUtils.isNotEmpty(userInfo.getId())) {
  150. userInfo.setIsLocked(ConstantsStatus.STOP);
  151. getMapper().locked(userInfo);
  152. }
  153. } catch(Exception e) {
  154. e.printStackTrace();
  155. }
  156. }
  157. /**
  158. * 用户登录成功后,重置错误密码次数和解锁用户
  159. * @param userInfo
  160. */
  161. public void unlock(UserInfo userInfo) {
  162. try {
  163. if(userInfo != null && StringUtils.isNotEmpty(userInfo.getId())) {
  164. userInfo.setIsLocked(ConstantsStatus.START);
  165. userInfo.setBadPasswordCount(0);
  166. getMapper().unlock(userInfo);
  167. }
  168. } catch(Exception e) {
  169. e.printStackTrace();
  170. }
  171. }
  172. /**
  173. * 更新错误密码次数
  174. * @param userInfo
  175. */
  176. public void updateBadPasswordCount(UserInfo userInfo) {
  177. try {
  178. if(userInfo != null && StringUtils.isNotEmpty(userInfo.getId())) {
  179. int updateBadPWDCount = userInfo.getBadPasswordCount() + 1;
  180. userInfo.setBadPasswordCount(updateBadPWDCount);
  181. getMapper().updateBadPWDCount(userInfo);
  182. }
  183. } catch(Exception e) {
  184. e.printStackTrace();
  185. }
  186. }
  187. public boolean changeSharedSecret(UserInfo userInfo){
  188. return getMapper().changeSharedSecret(userInfo)>0;
  189. }
  190. public boolean changePasswordQuestion(UserInfo userInfo){
  191. return getMapper().changePasswordQuestion(userInfo)>0;
  192. }
  193. public boolean changeAuthnType(UserInfo userInfo){
  194. return getMapper().changeAuthnType(userInfo)>0;
  195. }
  196. public boolean changeEmail(UserInfo userInfo){
  197. return getMapper().changeEmail(userInfo)>0;
  198. }
  199. public boolean changeMobile(UserInfo userInfo){
  200. return getMapper().changeMobile(userInfo)>0;
  201. }
  202. public UserInfo queryUserInfoByEmailMobile(String emailMobile) {
  203. return getMapper().queryUserInfoByEmailMobile(emailMobile);
  204. }
  205. public int updateProfile(UserInfo userInfo){
  206. return getMapper().updateProfile(userInfo);
  207. }
  208. }