UserInfoService.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.maxkey.persistence.service;
  17. import org.apache.mybatis.jpa.persistence.JpaBaseService;
  18. import org.maxkey.constants.ConstantsStatus;
  19. import org.maxkey.crypto.ReciprocalUtils;
  20. import org.maxkey.crypto.password.PasswordReciprocal;
  21. import org.maxkey.domain.ChangePassword;
  22. import org.maxkey.domain.UserInfo;
  23. import org.maxkey.identity.kafka.KafkaIdentityAction;
  24. import org.maxkey.identity.kafka.KafkaIdentityTopic;
  25. import org.maxkey.identity.kafka.KafkaProvisioningService;
  26. import org.maxkey.persistence.db.PasswordPolicyValidator;
  27. import org.maxkey.persistence.mapper.UserInfoMapper;
  28. import org.maxkey.util.DateUtils;
  29. import org.maxkey.util.StringUtils;
  30. import org.maxkey.web.WebContext;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.security.crypto.password.PasswordEncoder;
  35. import org.springframework.stereotype.Service;
  36. /**
  37. * @author Crystal.Sea
  38. *
  39. */
  40. @Service
  41. public class UserInfoService extends JpaBaseService<UserInfo> {
  42. final static Logger _logger = LoggerFactory.getLogger(UserInfoService.class);
  43. @Autowired
  44. private PasswordEncoder passwordEncoder;
  45. @Autowired
  46. PasswordPolicyValidator passwordPolicyValidator;
  47. @Autowired
  48. KafkaProvisioningService kafkaProvisioningService;
  49. public UserInfoService() {
  50. super(UserInfoMapper.class);
  51. }
  52. /* (non-Javadoc)
  53. * @see com.connsec.db.service.BaseService#getMapper()
  54. */
  55. @Override
  56. public UserInfoMapper getMapper() {
  57. // TODO Auto-generated method stub
  58. return (UserInfoMapper)super.getMapper();
  59. }
  60. public boolean insert(UserInfo userInfo) {
  61. userInfo = passwordEncoder(userInfo);
  62. if (super.insert(userInfo)) {
  63. kafkaProvisioningService.send(
  64. KafkaIdentityTopic.USERINFO_TOPIC,
  65. userInfo,
  66. KafkaIdentityAction.CREATE_ACTION);
  67. return true;
  68. }
  69. return false;
  70. }
  71. public boolean update(UserInfo userInfo) {
  72. userInfo = passwordEncoder(userInfo);
  73. if (super.update(userInfo)) {
  74. kafkaProvisioningService.send(
  75. KafkaIdentityTopic.USERINFO_TOPIC,
  76. userInfo,
  77. KafkaIdentityAction.UPDATE_ACTION);
  78. changePasswordProvisioning(userInfo);
  79. return true;
  80. }
  81. return false;
  82. }
  83. public boolean delete(UserInfo userInfo) {
  84. if( super.delete(userInfo)){
  85. kafkaProvisioningService.send(
  86. KafkaIdentityTopic.USERINFO_TOPIC,
  87. userInfo,
  88. KafkaIdentityAction.DELETE_ACTION);
  89. return true;
  90. }
  91. return false;
  92. }
  93. public boolean updateProtectedApps(UserInfo userinfo) {
  94. try {
  95. if(WebContext.getUserInfo() != null) {
  96. userinfo.setModifiedBy(WebContext.getUserInfo().getId());
  97. }
  98. userinfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString());
  99. return getMapper().updateProtectedApps(userinfo) > 0;
  100. } catch (Exception e) {
  101. e.printStackTrace();
  102. }
  103. return false;
  104. }
  105. public UserInfo loadByUsername(String username) {
  106. return getMapper().loadByUsername(username);
  107. }
  108. public UserInfo loadByAppIdAndUsername(String appId,String username){
  109. try {
  110. UserInfo userinfo = new UserInfo();
  111. userinfo.setUsername(username);
  112. return getMapper().loadByAppIdAndUsername(userinfo) ;
  113. } catch (Exception e) {
  114. e.printStackTrace();
  115. }
  116. return null;
  117. }
  118. public void logisticDeleteAllByCid(String cid){
  119. try {
  120. getMapper().logisticDeleteAllByCid(cid);
  121. } catch(Exception e) {
  122. e.printStackTrace();
  123. }
  124. }
  125. public UserInfo passwordEncoder(UserInfo userInfo) {
  126. //密码不为空,则需要进行加密处理
  127. if(userInfo.getPassword()!=null && !userInfo.getPassword().equals("")) {
  128. String password = passwordEncoder.encode(userInfo.getPassword());
  129. userInfo.setDecipherable(ReciprocalUtils.encode(PasswordReciprocal.getInstance().rawPassword(userInfo.getUsername(), userInfo.getPassword())));
  130. _logger.debug("decipherable : "+userInfo.getDecipherable());
  131. userInfo.setPassword(password);
  132. userInfo.setPasswordLastSetTime(DateUtils.getCurrentDateTimeAsString());
  133. userInfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString());
  134. }
  135. return userInfo;
  136. }
  137. public boolean changePassword(UserInfo userInfo) {
  138. try {
  139. passwordPolicyValidator.validator(userInfo);
  140. if(WebContext.getUserInfo() != null) {
  141. userInfo.setModifiedBy(WebContext.getUserInfo().getId());
  142. }
  143. userInfo = passwordEncoder(userInfo);
  144. if(getMapper().changePassword(userInfo) > 0){
  145. changePasswordProvisioning(userInfo);
  146. return true;
  147. }
  148. return false;
  149. } catch (Exception e) {
  150. e.printStackTrace();
  151. }
  152. return false;
  153. }
  154. public void changePasswordProvisioning(UserInfo userInfo) {
  155. if(userInfo.getPassword()!=null && !userInfo.getPassword().equals("")) {
  156. ChangePassword changePassword=new ChangePassword();
  157. changePassword.setId(userInfo.getId());
  158. changePassword.setUid(userInfo.getId());
  159. changePassword.setUsername(userInfo.getUsername());
  160. changePassword.setDecipherable(userInfo.getDecipherable());
  161. changePassword.setPassword(userInfo.getPassword());
  162. kafkaProvisioningService.send(
  163. KafkaIdentityTopic.PASSWORD_TOPIC,
  164. changePassword,
  165. KafkaIdentityAction.PASSWORD_ACTION);
  166. }
  167. }
  168. public boolean changeAppLoginPassword(UserInfo userinfo) {
  169. try {
  170. if(WebContext.getUserInfo() != null) {
  171. userinfo.setModifiedBy(WebContext.getUserInfo().getId());
  172. }
  173. userinfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString());
  174. return getMapper().changeAppLoginPassword(userinfo) > 0;
  175. } catch (Exception e) {
  176. e.printStackTrace();
  177. }
  178. return false;
  179. }
  180. /**
  181. * 锁定用户:islock:1 用户解锁 2 用户锁定
  182. * @param userInfo
  183. */
  184. public void locked(UserInfo userInfo) {
  185. try {
  186. if(userInfo != null && StringUtils.isNotEmpty(userInfo.getId())) {
  187. userInfo.setIsLocked(ConstantsStatus.STOP);
  188. getMapper().locked(userInfo);
  189. }
  190. } catch(Exception e) {
  191. e.printStackTrace();
  192. }
  193. }
  194. /**
  195. * 用户登录成功后,重置错误密码次数和解锁用户
  196. * @param userInfo
  197. */
  198. public void unlock(UserInfo userInfo) {
  199. try {
  200. if(userInfo != null && StringUtils.isNotEmpty(userInfo.getId())) {
  201. userInfo.setIsLocked(ConstantsStatus.START);
  202. userInfo.setBadPasswordCount(0);
  203. getMapper().unlock(userInfo);
  204. }
  205. } catch(Exception e) {
  206. e.printStackTrace();
  207. }
  208. }
  209. /**
  210. * 更新错误密码次数
  211. * @param userInfo
  212. */
  213. public void updateBadPasswordCount(UserInfo userInfo) {
  214. try {
  215. if(userInfo != null && StringUtils.isNotEmpty(userInfo.getId())) {
  216. int updateBadPWDCount = userInfo.getBadPasswordCount() + 1;
  217. userInfo.setBadPasswordCount(updateBadPWDCount);
  218. getMapper().updateBadPWDCount(userInfo);
  219. }
  220. } catch(Exception e) {
  221. e.printStackTrace();
  222. }
  223. }
  224. public boolean changeSharedSecret(UserInfo userInfo){
  225. return getMapper().changeSharedSecret(userInfo)>0;
  226. }
  227. public boolean changePasswordQuestion(UserInfo userInfo){
  228. return getMapper().changePasswordQuestion(userInfo)>0;
  229. }
  230. public boolean changeAuthnType(UserInfo userInfo){
  231. return getMapper().changeAuthnType(userInfo)>0;
  232. }
  233. public boolean changeEmail(UserInfo userInfo){
  234. return getMapper().changeEmail(userInfo)>0;
  235. }
  236. public boolean changeMobile(UserInfo userInfo){
  237. return getMapper().changeMobile(userInfo)>0;
  238. }
  239. public UserInfo queryUserInfoByEmailMobile(String emailMobile) {
  240. return getMapper().queryUserInfoByEmailMobile(emailMobile);
  241. }
  242. public int updateProfile(UserInfo userInfo){
  243. return getMapper().updateProfile(userInfo);
  244. }
  245. public void setPasswordPolicyValidator(PasswordPolicyValidator passwordPolicyValidator) {
  246. this.passwordPolicyValidator = passwordPolicyValidator;
  247. }
  248. }