package com.ym.mec.collectfee.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.MDC;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.ym.mec.collectfee.utils.WebUtil;

public class RequestInterceptor extends HandlerInterceptorAdapter {

	private static final String IP = "ip";

	private static final String USERNAME = "username";

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

		System.out.println("*************Request Interceptor************");
		
		String username = "";
		String userId = "";
		// 存储userId以及IP
		MDC.put(USERNAME, username + "@" + userId);
		MDC.put(IP, WebUtil.getRemoteIp(request));

		return true;
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		MDC.remove(USERNAME);
		MDC.remove(IP);
	}
}
