JsonSessionInterceptor.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package com.steerinfo.util;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.springframework.web.servlet.ModelAndView;
  5. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
  6. import com.hnshituo.basic.spring.web.interceptor.SessionTimeOutException;
  7. import com.hnshituo.basic.util.DataEncoder;
  8. public class JsonSessionInterceptor extends HandlerInterceptorAdapter {
  9. /**
  10. * 在业务处理器处理请求之前被调用 如果返回false 从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链
  11. * 如果返回true 执行下一个拦截器,直到所有的拦截器都执行完毕 再执行被拦截的Controller 然后进入拦截器链,
  12. * 从最后一个拦截器往回执行所有的postHandle() 接着再从最后一个拦截器往回执行所有的afterCompletion()
  13. */
  14. @Override
  15. public boolean preHandle(HttpServletRequest request,
  16. HttpServletResponse response, Object handler) throws Exception {
  17. String userId = (String) request.getSession().getAttribute("userId");
  18. String requestUri = request.getRequestURI();
  19. String BasePath=requestUri.substring(requestUri.lastIndexOf("/")+1);
  20. String clinetIp=request.getRemoteAddr(); //客户端ip
  21. String IpEncode = DataEncoder.shaEncode(clinetIp); //密文用来跟config.proprity文件中的信息匹配
  22. /*
  23. Properties ps = PropertiesUtils.getProperties();
  24. Set<Map.Entry<Object, Object>> entrySet = ps.entrySet();//返回的属性键值对实体
  25. for (Map.Entry<Object, Object> entry : entrySet) {
  26. if(!IpEncode.equals(IpEncode)){
  27. throw new Exception("当前客户端未配置计量点信息");
  28. }
  29. //system.out.println(entry.getKey() + "=" + entry.getValue());
  30. }
  31. //*/
  32. //监听移动端,WinForm端不考虑session过期
  33. if((BasePath.contains("App") || BasePath.contains("Web")) && userId==null){
  34. throw new SessionTimeOutException();
  35. }
  36. return true;
  37. }
  38. /**
  39. * 在业务处理器处理请求执行完成后,生成视图之前执行的动作 可在modelAndView中加入数据,比如当前时间
  40. */
  41. @Override
  42. public void postHandle(HttpServletRequest request,
  43. HttpServletResponse response, Object handler,
  44. ModelAndView modelAndView) throws Exception {
  45. }
  46. /**
  47. * 在DispatcherServlet完全处理完请求后被调用,可用于清理资源等
  48. *
  49. * 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion()
  50. */
  51. @Override
  52. public void afterCompletion(HttpServletRequest request,
  53. HttpServletResponse response, Object handler, Exception ex)
  54. throws Exception {
  55. }
  56. }