EmsAuthServiceImpl.java 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.persagy.proxy.common.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.persagy.dmp.auth.client.EmsSaasWebClient;
  6. import com.persagy.dmp.auth.domain.Group;
  7. import com.persagy.dmp.auth.domain.Project;
  8. import com.persagy.dmp.auth.domain.ResultReturn;
  9. import com.persagy.dmp.auth.service.impl.AbstractAuthServiceImpl;
  10. import com.persagy.dmp.common.constant.CommonConstant;
  11. import com.persagy.dmp.common.constant.ResponseCode;
  12. import com.persagy.dmp.common.context.AppContext;
  13. import com.persagy.dmp.common.exception.BusinessException;
  14. import com.persagy.dmp.common.helper.SpringHelper;
  15. import com.persagy.proxy.adm.constant.AdmCommonConstant;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Set;
  22. import java.util.stream.Collectors;
  23. /**
  24. * 运维平台2.0 鉴权实现类
  25. * 从head中获取上下文信息,并校验集团、项目是否有效
  26. * @author Charlie Yu
  27. * @date 2021-11-02
  28. */
  29. public class EmsAuthServiceImpl extends AbstractAuthServiceImpl {
  30. @Autowired
  31. private EmsSaasWebClient emsSaasWebClient;
  32. @Override
  33. public void loginSuccess(HttpServletRequest request, HttpServletResponse response) {
  34. super.loginSuccess(request, response);
  35. loadContextByRequest(request);
  36. if (StrUtil.isBlank(AppContext.getContext().getGroupCode())
  37. && StrUtil.isBlank(AppContext.getContext().getProjectId())){
  38. // 两者都为空时不做校验
  39. return;
  40. }
  41. ResultReturn<Group> queryResult = emsSaasWebClient.queryGroupProjectList(new JSONObject());
  42. if (!CommonConstant.QUERY_SUCCESS.equals(queryResult.getResult())){
  43. throw new BusinessException(queryResult.getResultMsg());
  44. }
  45. List<Group> content = queryResult.getContent();
  46. if (CollUtil.isEmpty(content)){
  47. throw new BusinessException(ResponseCode.A0402.getDesc(), ResponseCode.A0402.getDesc());
  48. }
  49. Map<String, List<Project>> groupMap = content.stream()
  50. .collect(Collectors.toMap(Group::getGroupCode, Group::getProjects, (k1, k2) -> k1));
  51. // 1.没有集团编码时根据项目匹配集团
  52. if (StrUtil.isBlank(AppContext.getContext().getGroupCode())){
  53. // 如果集团编码为空则通过项目id查找
  54. Set<Map.Entry<String, List<Project>>> entries = groupMap.entrySet();
  55. flag:
  56. for (Map.Entry<String, List<Project>> entry : entries) {
  57. if (CollUtil.isEmpty(entry.getValue())){
  58. continue;
  59. }
  60. for (Project project : entry.getValue()) {
  61. if (AppContext.getContext().getProjectId().equals(project.getProjectId())){
  62. AppContext.getContext().setGroupCode(entry.getKey());
  63. break flag;
  64. }
  65. }
  66. }
  67. }
  68. if (StrUtil.isBlank(AppContext.getContext().getGroupCode())){
  69. // 无集团编码时,默认为配置的集团编码
  70. AppContext.getContext().setGroupCode(SpringHelper.getString(AdmCommonConstant.MIDDLEWARE_GROUP_CODE));
  71. }
  72. // 2.验证集团是否有效
  73. if (StrUtil.isBlank(AppContext.getContext().getGroupCode())
  74. || !groupMap.containsKey(AppContext.getContext().getGroupCode())){
  75. throw new BusinessException(ResponseCode.A0402.getCode(), ResponseCode.A0402.getDesc());
  76. }
  77. }
  78. @Override
  79. protected void loadContextByRequest(HttpServletRequest request) {
  80. AppContext context = AppContext.getContext();
  81. context.setGroupCode(request.getHeader("groupCode"));
  82. context.setProjectId(request.getHeader("projectId"));
  83. context.setAppId(request.getHeader("appId"));
  84. String userId = request.getHeader("userId");
  85. // 无用户时,默认为默认系统用户
  86. if(StrUtil.isBlank(userId)) {
  87. context.setAccountId(CommonConstant.DEFAULT_ID);
  88. }
  89. }
  90. }