InfoAsyncUtils.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.uas.sso.util;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.uas.sso.common.encrypt.MD5;
  5. import com.uas.sso.common.util.HttpUtil;
  6. import com.uas.sso.core.Const;
  7. import com.uas.sso.core.ICallable;
  8. import com.uas.sso.entity.App;
  9. import com.uas.sso.entity.UserView;
  10. import com.uas.sso.exception.AccountException;
  11. import java.util.ArrayList;
  12. import java.util.Collections;
  13. import java.util.List;
  14. import org.apache.log4j.Logger;
  15. import org.springframework.util.Assert;
  16. /**
  17. * Utils class is designed to sync user information with other apps
  18. * and handle user information.
  19. *
  20. * @author huxz
  21. */
  22. public class InfoAsyncUtils {
  23. public static final Logger logger = Logger.getLogger(InfoAsyncUtils.class);
  24. /**
  25. * 生成加密密码
  26. *
  27. * @param format 密码加密格式
  28. * @param password 明文密码
  29. * @param salt 盐值
  30. * @return 密文密码
  31. */
  32. public static String encryptePassword(String format, String password, String salt) {
  33. // 验证加密格式
  34. if (StringUtils.isEmpty(format)) {
  35. return password;
  36. }
  37. // 验证密码信息,长度超过32认为是已加密过的密文
  38. int minPwdLen = 4;
  39. int maxPwdLen = 32;
  40. if (StringUtils.isEmpty(password) || password.length() < minPwdLen
  41. || password.length() >= maxPwdLen) {
  42. throw new AccountException("invalid password");
  43. }
  44. // 加密格式: "$password{$salt}"
  45. String cipher = format.replace(Const.ENCRY_PARAM_PASSWORD, password);
  46. cipher = cipher.replace(Const.ENCRY_PARAM_SALT, salt == null ? "" : salt);
  47. return MD5.toMD5(cipher);
  48. }
  49. /**
  50. * 获取所有应用的有效用户信息更新接口.
  51. *
  52. * @param appList 应用列表
  53. * @return 用户更新接口链接列表
  54. */
  55. public static List<String> getUserBackUrlsFromApps(List<App> appList) {
  56. if (CollectionUtils.isEmpty(appList)) {
  57. return Collections.emptyList();
  58. }
  59. List<String> backUserUrls = new ArrayList<>();
  60. for (App app : appList) {
  61. if (app != null && StringUtils.isEmpty(app.getUserControl())
  62. && !StringUtils.isEmpty(app.getBackUserUrl())) {
  63. backUserUrls.add(app.getUid() + ":" + app.getBackUserUrl());
  64. }
  65. }
  66. return backUserUrls;
  67. }
  68. /**
  69. * 同步用户信息到各个应用
  70. *
  71. * @param backUserUrls 同步接口URL
  72. * @param userView 用户信息视图
  73. * @param msg 同步信息描述,用户区分同步类型
  74. */
  75. public static void syncUserInfo(final List<String> backUserUrls, final UserView userView,
  76. final String msg) {
  77. if (CollectionUtils.isEmpty(backUserUrls)) {
  78. logger.warn("用户更新接口URL列表为空列表");
  79. return;
  80. }
  81. Assert.notNull(userView, "用户信息不能为空");
  82. Assert.isTrue(!org.springframework.util.StringUtils.isEmpty(msg), "同步信息描述不能为空");
  83. ExecuteUtils.execute(new ICallable<Void, String>() {
  84. @Override
  85. public Void call(String url) {
  86. String[] split = url.split(":");
  87. String appId = split[0];
  88. try {
  89. url = split[1];
  90. JSONObject formData = JSON.parseObject(JSON.toJSONString(userView));
  91. HttpUtil.ResponseWrap res = HttpUtil.doPost(url, formData, 10000);
  92. if (!res.isSuccess()) {
  93. logger.error(String.format("%s:同步用户信息失败, %s, %s, %s", msg, appId,
  94. JSON.toJSONString(userView), res.getContent()));
  95. } else {
  96. logger.info(String.format("%s:同步用户信息成功, %s, %s", msg, appId,
  97. JSON.toJSONString(userView)));
  98. }
  99. } catch (Exception e) {
  100. logger.error(String.format("%s:同步用户信息失败, %s, %s, %s", msg, appId,
  101. JSON.toJSONString(userView), e.getMessage()));
  102. }
  103. return null;
  104. }
  105. }, backUserUrls);
  106. }
  107. }