package com.supplier.util;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.supplier.jwt.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class JWTIntercept implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = DesDecry.decrypt(request.getHeader("token"));
        Map<String, Object> map = new HashMap<>();
        log.info("输入的token为：{{}}", token);
        try {
            JWTUtil.verify(token);
            return true;
        } catch (SignatureVerificationException e) {
            map.put("errMsg", "无效签名！");
            e.printStackTrace();
        } catch (TokenExpiredException e) {
            map.put("errMsg", "token过期！");
            e.printStackTrace();
        } catch (AlgorithmMismatchException e) {
            map.put("errMsg", "token算法不一致");
            e.printStackTrace();
        } catch (Exception e) {
            map.put("errMsg", "token无效!");
            e.printStackTrace();
        }
        map.put("msg", "510");
        // 将map转为json，然后将map的json数据返回给前端
        String jsonStr = new ObjectMapper().writeValueAsString(map);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(jsonStr);
        return false;
    }
}
