springboot 之拦截器

1、建立拦截器

2.实现拦截器(可实现权限拦截,操作日志)

2.1 权限(当然接口权限关系表 要事先实现)

2.2 日志

 

附加部分代码:

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {

  //获取项目动态绝对路径
  String path = request.getServletPath();

  if(!"/api/platform/location/insert".equalsIgnoreCase(path) && !path.contains("list")){
    //日志记录
    String param;
    //获得所有的参数名称
    Enumeration<String> em = request.getParameterNames();
    Map<String,Object> map = new HashMap<>();
    while (em.hasMoreElements()) {
    String name = em.nextElement();
    if(!"_".equals(name)){
      String value = request.getParameter(name);
      map.put(name, value);
    }

   }
  if(map.size() != 0){
    JSONObject json = JSONObject.fromObject(map);
    param = json.toString();
      }else{
    param= HttpHelper.getBodyString(request);
  }

  ManagerLoginLog log = new ManagerLoginLog();
  log.setCreateTime(utils.getNowTimeSecond());
  log.setManagerId(arr[0]);
  log.setIp(utils.getIPAddress(request));
  log.setMapper(path);
  log.setParam(param);
  managerLoginLogRepository.save(log);
}

 

}

 

posted @ 2019-08-26 16:50  文刀水告  阅读(163)  评论(0编辑  收藏  举报