package com.unis.crk.Interceptor;

import com.alibaba.fastjson.JSONObject;
import com.unis.crk.basic.util.TokenUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

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

@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return  true;
    /*    //跨域请求会首先发一个option请求，直接返回正常状态并通过拦截器
        if(request.getMethod().equals("OPTIONS")){
            response.setStatus(HttpServletResponse.SC_OK);
            return true;
        }
        response.setCharacterEncoding("utf-8");
        String token = request.getHeader("token");
        StringBuffer requestURL = request.getRequestURL();
        System.out.println(requestURL);
        if (token!=null){
            boolean result= TokenUtil.verify(token);
            if (result){
                System.out.println("通过拦截器");
                return true;
            }else {
                JSONObject json=new JSONObject();
                json.put("msg","token verify fail");
                json.put("code","550");
                response.getWriter().append(json.toString());
                System.out.println("认证失败，未通过拦截器");
            }
        }
        response.setContentType("application/json; charset=utf-8");
        try {
            JSONObject json=new JSONObject();
            json.put("msg","token verify fail");
            json.put("code","550");
            response.getWriter().append(json.toString());
            System.out.println("认证失败，未通过拦截器");
        } catch (Exception e) {
            return false;
        }
        *//**
         * 还可以在此处检验用户存不存在等操作
         *//*
        return false;*/
    }
}

