SysLoginController.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.ruoyi.web.controller.system;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import com.ruoyi.cc.domain.CcExtNum;
  5. import com.ruoyi.cc.domain.CcGateways;
  6. import com.ruoyi.cc.service.ICcExtNumService;
  7. import com.ruoyi.cc.service.ICcGatewaysService;
  8. import com.ruoyi.common.utils.CacheUtils;
  9. import org.apache.shiro.SecurityUtils;
  10. import org.apache.shiro.authc.AuthenticationException;
  11. import org.apache.shiro.authc.UsernamePasswordToken;
  12. import org.apache.shiro.subject.Subject;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.stereotype.Controller;
  16. import org.springframework.ui.ModelMap;
  17. import org.springframework.web.bind.annotation.GetMapping;
  18. import org.springframework.web.bind.annotation.PostMapping;
  19. import org.springframework.web.bind.annotation.ResponseBody;
  20. import com.ruoyi.common.core.controller.BaseController;
  21. import com.ruoyi.common.core.domain.AjaxResult;
  22. import com.ruoyi.common.core.text.Convert;
  23. import com.ruoyi.common.utils.ServletUtils;
  24. import com.ruoyi.common.utils.StringUtils;
  25. import com.ruoyi.framework.shiro.web.filter.embed.EmbedRequestUtils;
  26. import com.ruoyi.framework.web.service.ConfigService;
  27. import java.util.Arrays;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.stream.Collectors;
  32. /**
  33. * 登录验证
  34. *
  35. * @author ruoyi
  36. */
  37. @Controller
  38. public class SysLoginController extends BaseController
  39. {
  40. /**
  41. * 是否开启记住我功能
  42. */
  43. @Value("${shiro.rememberMe.enabled: false}")
  44. private boolean rememberMe;
  45. /**
  46. * 系统包版本
  47. */
  48. @Value("${sysconfig.sysVersion}")
  49. private String sysVersion;
  50. /**
  51. * 是否开启登陆时选择业务组
  52. */
  53. @Value("${sysconfig.show-dynamic-groupid: false}")
  54. private boolean showDynamicGroupid;
  55. @Autowired
  56. private ConfigService configService;
  57. @Autowired
  58. private ICcExtNumService ccExtNumService;
  59. @Autowired
  60. private ICcGatewaysService ccGatewaysService;
  61. @GetMapping("/login")
  62. public String login(HttpServletRequest request, HttpServletResponse response, ModelMap mmap)
  63. {
  64. // 如果是Ajax请求,返回Json字符串。
  65. if (ServletUtils.isAjaxRequest(request))
  66. {
  67. return ServletUtils.renderString(response, "{\"code\":\"1\",\"msg\":\"未登录或登录超时。请重新登录\"}");
  68. }
  69. // 是否开启记住我
  70. mmap.put("isRemembered", rememberMe);
  71. // 是否开启用户注册
  72. mmap.put("isAllowRegister", Convert.toBool(configService.getKey("sys.account.registerUser"), false));
  73. // 版本号
  74. mmap.put("sysVersion", sysVersion);
  75. // 是否开启登陆时选择业务组
  76. mmap.put("showDynamicGroupid", showDynamicGroupid);
  77. return "login";
  78. }
  79. @PostMapping("/login")
  80. @ResponseBody
  81. public AjaxResult ajaxLogin(String username, String password, String extNum, String groupId, String myGateway, Boolean rememberMe)
  82. {
  83. boolean embed = EmbedRequestUtils.isEmbed(ServletUtils.getRequest());
  84. // 校验分机号(HIS iframe 嵌入必须绑定分机;禁止登录成功后再被 main 踢回登录)
  85. CcExtNum ccExtNum = ccExtNumService.selectCcExtNumByUserCodeTwo(username);
  86. if (null == ccExtNum) {
  87. if (!embed && StringUtils.isNotEmpty(username) && username.startsWith("Runtian_")) {
  88. // 非嵌入:保留润天账号兼容
  89. } else {
  90. return AjaxResult.error("未绑定分机号,请联系系统管理员!");
  91. }
  92. } else {
  93. if (!ccExtNum.getExtNum().toString().equals(extNum)) {
  94. return AjaxResult.error("username="+username+"的分机号不正确!线上:"+ccExtNum.getExtNum()+",传入的分机号:" + extNum);
  95. }
  96. }
  97. CacheUtils.put("groupId-" + username, groupId);
  98. // cache myGateway for workbench dialing; only purposes 1/3 allowed
  99. if (StringUtils.isNotEmpty(myGateway)) {
  100. List<Long> gatewayIds = Arrays.stream(myGateway.split(","))
  101. .filter(StringUtils::isNotBlank)
  102. .map(String::trim)
  103. .map(Long::parseLong)
  104. .collect(Collectors.toList());
  105. CcGateways query = new CcGateways();
  106. query.setGatewayIds(gatewayIds);
  107. Map<String, Object> params = new HashMap<>();
  108. params.put("purposes", Arrays.asList(1, 3));
  109. query.setParams(params);
  110. List<CcGateways> allowed = ccGatewaysService.selectCcGatewaysList(query);
  111. if (allowed == null || allowed.isEmpty() || allowed.size() != gatewayIds.size()) {
  112. return AjaxResult.error("指定网关无效,仅可选择电话条/不限制用途的网关");
  113. }
  114. CacheUtils.put("myGateway-" + username, myGateway);
  115. } else {
  116. CacheUtils.remove("myGateway-" + username);
  117. }
  118. UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
  119. Subject subject = SecurityUtils.getSubject();
  120. try
  121. {
  122. subject.login(token);
  123. return success();
  124. }
  125. catch (AuthenticationException e)
  126. {
  127. String msg = "用户或密码错误";
  128. if (StringUtils.isNotEmpty(e.getMessage()))
  129. {
  130. msg = e.getMessage();
  131. }
  132. return error(msg);
  133. }
  134. }
  135. @GetMapping("/unauth")
  136. public String unauth()
  137. {
  138. return "error/unauth";
  139. }
  140. }