/**  
 * @Title: TokenFilter.java
 * @Package: com.fuzamei.filter
 * @Description: TODO 
 * @author: Ma Amin
 * @date: 2017-10-31 下午12:02:38
 */
package com.fuzamei.interceptor;


import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.alibaba.fastjson.JSONObject;

/** 
 * @file_name: TokenFilter.java
 * @Description: TODO token拦截，ture--放行。false---过滤
 * @author: Ma Amin
 * @date: 2017-10-31 下午12:02:38 
 * @version 1.0
 */
public class TokenInterceptor extends HandlerInterceptorAdapter{/*
	@Autowired
	private AccountService accountService;
	 (non-Javadoc)
	 * @see org.springframework.web.servlet.handler.HandlerInterceptorAdapter#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
	 
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		// TODO Auto-generated method stub
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		if(request.getHeader("Authorization") == null){
			json.put("code", 12431);
			json.put("success", false);
			json.put("message", "Authorization为空");
			json.put("data", null);
			out = response.getWriter();
			out.append(json.toString());
			return false;
		}
		String tokenAndUserId = request.getHeader("Authorization").toString();
		String token = tokenAndUserId.split("&")[0].toString().replace("Bearer ", "");//token
		int accountId = Integer.parseInt(tokenAndUserId.split("&")[1]);	//账户id
		if(token == null){
			json.put("code", 12431);
			json.put("success", false);
			json.put("message", "token为空");
			json.put("data", null);
			out = response.getWriter();
			out.append(json.toString());
			return false;
		}
		boolean flag = accountService.verificationToken(accountId, token);
		if(flag){
			json.put("code", 200);
			json.put("success", true);
			json.put("message", "token验证通过");
			json.put("data", null);
			out = response.getWriter();
			out.append(json.toString());
			return true;
		}
		json.put("code", 12431);
		json.put("success", false);
		json.put("message", "token验证失败");
		json.put("data", null);
		out = response.getWriter();
		out.append(json.toString());
		return false;
	}
*/}
