UserInfoService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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.ImportResultVO;
  23. import org.maxkey.domain.UserInfo;
  24. import org.maxkey.identity.kafka.KafkaIdentityAction;
  25. import org.maxkey.identity.kafka.KafkaIdentityTopic;
  26. import org.maxkey.identity.kafka.KafkaProvisioningService;
  27. import org.maxkey.persistence.db.PasswordPolicyValidator;
  28. import org.maxkey.persistence.mapper.UserInfoMapper;
  29. import org.maxkey.util.DateUtils;
  30. import org.maxkey.util.StringUtils;
  31. import org.maxkey.web.WebContext;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import org.springframework.jdbc.core.JdbcTemplate;
  36. import org.springframework.security.crypto.password.PasswordEncoder;
  37. import org.springframework.stereotype.Service;
  38. import org.springframework.web.multipart.MultipartFile;
  39. /**
  40. * @author Crystal.Sea
  41. *
  42. */
  43. @Service
  44. public class UserInfoService extends JpaBaseService<UserInfo> {
  45. final static Logger _logger = LoggerFactory.getLogger(UserInfoService.class);
  46. final static String UPDATE_GRIDLIST_SQL = "UPDATE MXK_USERINFO SET GRIDLIST = ? WHERE ID = ?";
  47. @Autowired
  48. private PasswordEncoder passwordEncoder;
  49. @Autowired
  50. PasswordPolicyValidator passwordPolicyValidator;
  51. @Autowired
  52. KafkaProvisioningService kafkaProvisioningService;
  53. @Autowired
  54. protected JdbcTemplate jdbcTemplate;
  55. public UserInfoService() {
  56. super(UserInfoMapper.class);
  57. }
  58. /* (non-Javadoc)
  59. * @see com.connsec.db.service.BaseService#getMapper()
  60. */
  61. @Override
  62. public UserInfoMapper getMapper() {
  63. // TODO Auto-generated method stub
  64. return (UserInfoMapper)super.getMapper();
  65. }
  66. public boolean insert(UserInfo userInfo) {
  67. userInfo = passwordEncoder(userInfo);
  68. if (super.insert(userInfo)) {
  69. kafkaProvisioningService.send(
  70. KafkaIdentityTopic.USERINFO_TOPIC,
  71. userInfo,
  72. KafkaIdentityAction.CREATE_ACTION);
  73. return true;
  74. }
  75. return false;
  76. }
  77. public boolean update(UserInfo userInfo) {
  78. userInfo = passwordEncoder(userInfo);
  79. if (super.update(userInfo)) {
  80. kafkaProvisioningService.send(
  81. KafkaIdentityTopic.USERINFO_TOPIC,
  82. userInfo,
  83. KafkaIdentityAction.UPDATE_ACTION);
  84. changePasswordProvisioning(userInfo);
  85. return true;
  86. }
  87. return false;
  88. }
  89. public ImportResultVO importing(MultipartFile file,Integer type){
  90. // 校验当前文件格式是不是excel文件
  91. // 解析excel文件中数据
  92. // 判断当前类型 0忽略 1覆盖 2终止
  93. // 返回导入结果
  94. return new ImportResultVO();
  95. }
  96. public boolean delete(UserInfo userInfo) {
  97. if( super.delete(userInfo)){
  98. kafkaProvisioningService.send(
  99. KafkaIdentityTopic.USERINFO_TOPIC,
  100. userInfo,
  101. KafkaIdentityAction.DELETE_ACTION);
  102. return true;
  103. }
  104. return false;
  105. }
  106. public boolean updateGridList(String gridList) {
  107. try {
  108. if (gridList != null && !gridList.equals("")) {
  109. int intGridList = Integer.parseInt(gridList);
  110. jdbcTemplate.update(UPDATE_GRIDLIST_SQL, intGridList,
  111. WebContext.getUserInfo().getId());
  112. WebContext.getUserInfo().setGridList(intGridList);
  113. }
  114. }catch(Exception e) {
  115. e.printStackTrace();
  116. return false;
  117. }
  118. return true;
  119. }
  120. public boolean updateProtectedApps(UserInfo userinfo) {
  121. try {
  122. if(WebContext.getUserInfo() != null) {
  123. userinfo.setModifiedBy(WebContext.getUserInfo().getId());
  124. }
  125. userinfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString());
  126. return getMapper().updateProtectedApps(userinfo) > 0;
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. }
  130. return false;
  131. }
  132. public UserInfo loadByUsername(String username) {
  133. return getMapper().loadByUsername(username);
  134. }
  135. public UserInfo loadByAppIdAndUsername(String appId,String username){
  136. try {
  137. UserInfo userinfo = new UserInfo();
  138. userinfo.setUsername(username);
  139. return getMapper().loadByAppIdAndUsername(userinfo) ;
  140. } catch (Exception e) {
  141. e.printStackTrace();
  142. }
  143. return null;
  144. }
  145. public void logisticDeleteAllByCid(String cid){
  146. try {
  147. getMapper().logisticDeleteAllByCid(cid);
  148. } catch(Exception e) {
  149. e.printStackTrace();
  150. }
  151. }
  152. public UserInfo passwordEncoder(UserInfo userInfo) {
  153. //密码不为空,则需要进行加密处理
  154. if(userInfo.getPassword()!=null && !userInfo.getPassword().equals("")) {
  155. String password = passwordEncoder.encode(userInfo.getPassword());
  156. userInfo.setDecipherable(ReciprocalUtils.encode(PasswordReciprocal.getInstance().rawPassword(userInfo.getUsername(), userInfo.getPassword())));
  157. _logger.debug("decipherable : "+userInfo.getDecipherable());
  158. userInfo.setPassword(password);
  159. userInfo.setPasswordLastSetTime(DateUtils.getCurrentDateTimeAsString());
  160. userInfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString());
  161. }
  162. return userInfo;
  163. }
  164. public boolean changePassword(String oldPassword,
  165. String newPassword,
  166. String confirmPassword) {
  167. try {
  168. WebContext.setAttribute(PasswordPolicyValidator.PASSWORD_POLICY_VALIDATE_RESULT, "");
  169. UserInfo userInfo = WebContext.getUserInfo();
  170. UserInfo changeUserInfo = new UserInfo();
  171. changeUserInfo.setUsername(userInfo.getUsername());
  172. changeUserInfo.setPassword(newPassword);
  173. changeUserInfo.setId(userInfo.getId());
  174. changeUserInfo.setDecipherable(userInfo.getDecipherable());
  175. if(newPassword.equals(confirmPassword)){
  176. if(oldPassword==null ||
  177. passwordEncoder.matches(oldPassword, userInfo.getPassword())){
  178. if(changePassword(changeUserInfo) ){
  179. userInfo.setPassword(changeUserInfo.getPassword());
  180. userInfo.setDecipherable(changeUserInfo.getDecipherable());
  181. return true;
  182. }
  183. return false;
  184. }else {
  185. if(oldPassword!=null &&
  186. passwordEncoder.matches(newPassword, userInfo.getPassword())) {
  187. WebContext.setAttribute(PasswordPolicyValidator.PASSWORD_POLICY_VALIDATE_RESULT,
  188. WebContext.getI18nValue("PasswordPolicy.OLD_PASSWORD_MATCH"));
  189. }else {
  190. WebContext.setAttribute(PasswordPolicyValidator.PASSWORD_POLICY_VALIDATE_RESULT,
  191. WebContext.getI18nValue("PasswordPolicy.OLD_PASSWORD_NOT_MATCH"));
  192. }
  193. }
  194. }else {
  195. WebContext.setAttribute(PasswordPolicyValidator.PASSWORD_POLICY_VALIDATE_RESULT,
  196. WebContext.getI18nValue("PasswordPolicy.CONFIRMPASSWORD_NOT_MATCH"));
  197. }
  198. } catch (Exception e) {
  199. e.printStackTrace();
  200. }
  201. return false;
  202. }
  203. public boolean changePassword(UserInfo changeUserInfo) {
  204. try {
  205. _logger.debug("decipherable old : " + changeUserInfo.getDecipherable());
  206. _logger.debug("decipherable new : " + ReciprocalUtils.encode(PasswordReciprocal.getInstance()
  207. .rawPassword(changeUserInfo.getUsername(), changeUserInfo.getPassword())));
  208. if (passwordPolicyValidator.validator(changeUserInfo) == false) {
  209. return false;
  210. }
  211. if (WebContext.getUserInfo() != null) {
  212. changeUserInfo.setModifiedBy(WebContext.getUserInfo().getId());
  213. }
  214. changeUserInfo = passwordEncoder(changeUserInfo);
  215. if (getMapper().changePassword(changeUserInfo) > 0) {
  216. changePasswordProvisioning(changeUserInfo);
  217. return true;
  218. }
  219. return false;
  220. } catch (Exception e) {
  221. e.printStackTrace();
  222. }
  223. return false;
  224. }
  225. public String randomPassword() {
  226. return passwordPolicyValidator.generateRandomPassword();
  227. }
  228. public void changePasswordProvisioning(UserInfo userInfo) {
  229. if(userInfo.getPassword()!=null && !userInfo.getPassword().equals("")) {
  230. ChangePassword changePassword=new ChangePassword();
  231. changePassword.setId(userInfo.getId());
  232. changePassword.setUid(userInfo.getId());
  233. changePassword.setUsername(userInfo.getUsername());
  234. changePassword.setDecipherable(userInfo.getDecipherable());
  235. changePassword.setPassword(userInfo.getPassword());
  236. kafkaProvisioningService.send(
  237. KafkaIdentityTopic.PASSWORD_TOPIC,
  238. changePassword,
  239. KafkaIdentityAction.PASSWORD_ACTION);
  240. }
  241. }
  242. public boolean changeAppLoginPassword(UserInfo userinfo) {
  243. try {
  244. if(WebContext.getUserInfo() != null) {
  245. userinfo.setModifiedBy(WebContext.getUserInfo().getId());
  246. }
  247. userinfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString());
  248. return getMapper().changeAppLoginPassword(userinfo) > 0;
  249. } catch (Exception e) {
  250. e.printStackTrace();
  251. }
  252. return false;
  253. }
  254. /**
  255. * 锁定用户:islock:1 用户解锁 2 用户锁定
  256. * @param userInfo
  257. */
  258. public void locked(UserInfo userInfo) {
  259. try {
  260. if(userInfo != null && StringUtils.isNotEmpty(userInfo.getId())) {
  261. userInfo.setIsLocked(ConstantsStatus.STOP);
  262. getMapper().locked(userInfo);
  263. }
  264. } catch(Exception e) {
  265. e.printStackTrace();
  266. }
  267. }
  268. /**
  269. * 用户登录成功后,重置错误密码次数和解锁用户
  270. * @param userInfo
  271. */
  272. public void unlock(UserInfo userInfo) {
  273. try {
  274. if(userInfo != null && StringUtils.isNotEmpty(userInfo.getId())) {
  275. userInfo.setIsLocked(ConstantsStatus.START);
  276. userInfo.setBadPasswordCount(0);
  277. getMapper().unlock(userInfo);
  278. }
  279. } catch(Exception e) {
  280. e.printStackTrace();
  281. }
  282. }
  283. /**
  284. * 更新错误密码次数
  285. * @param userInfo
  286. */
  287. public void updateBadPasswordCount(UserInfo userInfo) {
  288. try {
  289. if(userInfo != null && StringUtils.isNotEmpty(userInfo.getId())) {
  290. int updateBadPWDCount = userInfo.getBadPasswordCount() + 1;
  291. userInfo.setBadPasswordCount(updateBadPWDCount);
  292. getMapper().updateBadPWDCount(userInfo);
  293. }
  294. } catch(Exception e) {
  295. e.printStackTrace();
  296. }
  297. }
  298. public boolean changeSharedSecret(UserInfo userInfo){
  299. return getMapper().changeSharedSecret(userInfo)>0;
  300. }
  301. public boolean changePasswordQuestion(UserInfo userInfo){
  302. return getMapper().changePasswordQuestion(userInfo)>0;
  303. }
  304. public boolean changeAuthnType(UserInfo userInfo){
  305. return getMapper().changeAuthnType(userInfo)>0;
  306. }
  307. public boolean changeEmail(UserInfo userInfo){
  308. return getMapper().changeEmail(userInfo)>0;
  309. }
  310. public boolean changeMobile(UserInfo userInfo){
  311. return getMapper().changeMobile(userInfo)>0;
  312. }
  313. public UserInfo queryUserInfoByEmailMobile(String emailMobile) {
  314. return getMapper().queryUserInfoByEmailMobile(emailMobile);
  315. }
  316. public int updateProfile(UserInfo userInfo){
  317. return getMapper().updateProfile(userInfo);
  318. }
  319. public void setPasswordPolicyValidator(PasswordPolicyValidator passwordPolicyValidator) {
  320. this.passwordPolicyValidator = passwordPolicyValidator;
  321. }
  322. }