LoginInterceptor.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.ssj.sys.interceptor;
  2. import com.ssj.bean.sys.role.domain.Admin;
  3. import com.ssj.bean.weixin.problem.domain.RequestMethodLog;
  4. import com.ssj.bean.weixin.push.domain.SendTemplateShort;
  5. import com.ssj.framework.basic.utils.DateHelper;
  6. import com.ssj.framework.basic.utils.StringUtils;
  7. import com.ssj.framework.core.util.PropertiesUtil;
  8. import com.ssj.framework.core.util.WebUtil;
  9. import com.ssj.service.weixin.problem.service.RequestMethodLogService;
  10. import com.ssj.service.weixin.push.service.PushTemplateService;
  11. import com.ssj.service.weixin.sales.service.LibStaffingService;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.web.method.HandlerMethod;
  15. import org.springframework.web.servlet.ModelAndView;
  16. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import java.util.*;
  20. @Component
  21. public class LoginInterceptor extends HandlerInterceptorAdapter{
  22. @Autowired
  23. private RequestMethodLogService requestMethodLogService;
  24. @Autowired
  25. private LibStaffingService libStaffingService;
  26. @Autowired
  27. private PushTemplateService pushTemplateService;
  28. private static Set<String> sysURLList = new HashSet<String>();
  29. private static Set<String> notCheckURLList = new HashSet<String>();
  30. static {
  31. //notCheckURLList.add("/sys/login/index.html");
  32. //notCheckURLList.add("/sys/login/index");
  33. notCheckURLList.add("/sys/login/login");
  34. notCheckURLList.add("/sys/lib/getAllSchoolMap");
  35. sysURLList.add("/sys/sales/updatePrice");
  36. sysURLList.add("/sys/sales/libStaffingDelete");
  37. sysURLList.add("/sys/sales/libStaffingSave");
  38. sysURLList.add("/sys/sales/libStaffingZBSave");
  39. }
  40. // Controller 前执行
  41. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  42. response.setCharacterEncoding("UTF8"); // 设置输出编码
  43. response.setContentType("text/html;charset=UTF-8");
  44. Admin admin= (Admin) request.getSession().getAttribute("admin");
  45. String url = request.getRequestURI().replaceAll(request.getContextPath(), "");
  46. if (sysURLList.contains(url)) {
  47. HandlerMethod method = (HandlerMethod) handler;
  48. RequestMethodLog methodLog = new RequestMethodLog();
  49. if(admin !=null){
  50. methodLog.setAccountName(admin.getNickName());
  51. methodLog.setMethodUrl(url);
  52. methodLog.setMethodName(method.getMethod().getName());
  53. methodLog.setIpAddress(WebUtil.getRemoteAddr(request));
  54. // methodLog.setOrgCode(admin.getOrgCode());
  55. methodLog.setCreateTime(new Date());
  56. if("/sys/sales/updatePrice".equals(request.getRequestURI().replaceAll(request.getContextPath(), "")))
  57. {
  58. String sortName = (String) request.getParameter("sortName");
  59. String libId = (String) request.getParameter("libId");
  60. String oldPrice = (String) request.getParameter("oldPrice");
  61. String aliases = (String) request.getParameter("aliases");
  62. String price = (String) request.getParameter("price");
  63. methodLog.setSubstance(sortName+"("+oldPrice+"元/张)"+"更改为"+aliases+"("+price+"元/张)");
  64. List<String> userList= libStaffingService.findByCDSDAndOpenId(libId, "s_cd","s_sd");
  65. String template_id_short="OPENTM407378723";
  66. for (String openId : userList) {
  67. Map<String,String> data =new HashMap<String, String>();
  68. data.put("first", "馆长,你好。你有一则后台管理系统的操作通知,请知晓。\n");
  69. data.put("keyword1",sortName+"("+oldPrice+"元/张)"+"更改为"+aliases+"("+price+"元/张)");
  70. data.put("keyword2",DateHelper.format(new Date(),"yyyy年MM月dd日 HH:mm "));
  71. data.put("remark","\n如非您本人操作,请尽快查明原因。");
  72. SendTemplateShort sendTemplateShort=new SendTemplateShort(openId, template_id_short, null, data);
  73. pushTemplateService.savePushWxTemplate(sendTemplateShort);
  74. }
  75. }else if ("/sys/sales/libStaffingDelete".equals(request.getRequestURI().replaceAll(request.getContextPath(), "")))
  76. {
  77. String name = (String) request.getParameter("name");
  78. String libId = (String) request.getParameter("libId");
  79. String type = (String) request.getParameter("type");
  80. String userName ="其他";
  81. if("SSJ-SSJ-SD".equals(type)){
  82. userName ="总部SD";
  83. }else if("SSJ-SSJ-SR".equals(type)){
  84. userName="总部SR";
  85. }else if("s_cd".equals(type)){
  86. userName="CD";
  87. }else if("s_cc".equals(type)){
  88. userName="CC";
  89. }else if("s_cda".equals(type)){
  90. userName="CDA";
  91. }else if("s_sd".equals(type)){
  92. userName="总部SD人员";
  93. }
  94. methodLog.setSubstance("删除"+name+"为"+userName+"的销售人员权限");
  95. if( type !=null && type.contains("s_")){
  96. List<String> userList= libStaffingService.findByCDSDAndOpenId(libId, "s_cd","s_sd");
  97. String template_id_short="OPENTM407378723";
  98. for (String openId : userList) {
  99. Map<String,String> data =new HashMap<String, String>();
  100. data.put("first", "馆长,你好。你有一则后台管理系统的操作通知,请知晓。\n");
  101. data.put("keyword1","删除"+name+"为"+userName+"的销售人员权限");
  102. data.put("keyword2",DateHelper.format(new Date(),"yyyy年MM月dd日 HH:mm "));
  103. data.put("remark","\n如非您本人操作,请尽快查明原因。");
  104. SendTemplateShort sendTemplateShort=new SendTemplateShort(openId, template_id_short, null, data);
  105. pushTemplateService.savePushWxTemplate(sendTemplateShort);
  106. }
  107. }
  108. }else if ("/sys/sales/libStaffingSave".equals(url)||"/sys/sales/libStaffingZBSave".equals(url)){
  109. String[] names = request.getParameterValues("names[]");
  110. String name = Arrays.toString(names);
  111. String type = (String) request.getParameter("type");
  112. String userName ="其他";
  113. if("SSJ-SSJ-SD".equals(type)){
  114. userName ="总部SD";
  115. }else if("SSJ-SSJ-SR".equals(type)){
  116. userName="总部SR";
  117. }else if("s_cd".equals(type)){
  118. userName="CD";
  119. }else if("s_cc".equals(type)){
  120. userName="CC";
  121. }else if("s_cda".equals(type)){
  122. userName="CDA";
  123. }else if("s_sd".equals(type)){
  124. userName="总部SD人员";
  125. }
  126. if(type !=null && type.contains("s_")){
  127. String libId = (String) request.getParameter("libId");
  128. List<String> userList= libStaffingService.findByCDSDAndOpenId(libId, "s_cd","s_sd");
  129. String template_id_short="OPENTM407378723";
  130. for (String openId : userList) {
  131. Map<String,String> data =new HashMap<String, String>();
  132. data.put("first", "馆长,你好。你有一则后台管理系统的操作通知,请知晓。\n");
  133. data.put("keyword1","新增"+name+"为"+userName+"的销售人员权限");
  134. data.put("keyword2",DateHelper.format(new Date(),"yyyy年MM月dd日 HH:mm "));
  135. data.put("remark","\n如非您本人操作,请尽快查明原因。");
  136. SendTemplateShort sendTemplateShort=new SendTemplateShort(openId, template_id_short, null, data);
  137. pushTemplateService.savePushWxTemplate(sendTemplateShort);
  138. }
  139. }
  140. methodLog.setSubstance("新增"+name+"为"+userName+"的销售人员权限");
  141. }
  142. requestMethodLogService.save(methodLog);
  143. }
  144. }
  145. if (checkRequestURIIntNotFilterList(request)) { return true; }
  146. if (admin==null) {
  147. response.sendRedirect(request.getContextPath() + "/login.html");
  148. return false;
  149. }
  150. return true;
  151. /* //不检查的url
  152. java.util.Collection<String> collection = commonService.getAttributes(uri);
  153. if (!StringUtil.isEmpty(collection)) {
  154. if (!StringUtil.isEmpty(admin) && !StringUtil.isEmpty(admin.getAuthorities())) {
  155. Collection<String> authorities = admin.getAuthorities();
  156. for (String coll : collection) {
  157. if (authorities.contains(coll)) return true;
  158. }
  159. }
  160. }
  161. return false;*/
  162. }
  163. //运行Controller后运行
  164. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  165. if (modelAndView != null) {
  166. request.setCharacterEncoding("UTF-8");
  167. initModelAndView(request, modelAndView);
  168. }
  169. }
  170. public static void initModelAndView(HttpServletRequest request, ModelAndView modelAndView) throws Exception {
  171. modelAndView.addObject("sysUrl", "https://sys.sharingschool.com");
  172. //modelAndView.addObject("sysUrl", "http://127.0.0.1:8080/");
  173. modelAndView.addObject("imgUrl", PropertiesUtil.getValue("STATIC_FILE_URL"));
  174. }
  175. /**
  176. * 检查不过滤的url
  177. * @param request
  178. * @return
  179. */
  180. private boolean checkRequestURIIntNotFilterList(HttpServletRequest request) {
  181. String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());
  182. if (uri.startsWith("/resources/") || notCheckURLList.contains(uri) || isAjaxRequest(request)) {
  183. return true;
  184. } else {
  185. return false;
  186. }
  187. }
  188. public static boolean isAjaxRequest(HttpServletRequest request) {
  189. String ajaxHeader = request.getHeader("x-requested-with");
  190. if (StringUtils.isEmpty(ajaxHeader) && !"XMLHttpRequest".equalsIgnoreCase(ajaxHeader)) {
  191. return false;
  192. } else {
  193. return true;
  194. }
  195. }
  196. }