package com.slodon.b2b2c.interceptor;


import com.alibaba.fastjson.JSON;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * Sso拦截器配置
 *
 * @author cyan
 */
@Slf4j
public class SsoInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("request URI = {}", request.getRequestURI());
        log.info("request QueryString = {}", request.getQueryString());

        String ssoHeaderVersion = request.getHeader("version");
        log.info("request sso Version = {}", ssoHeaderVersion);
        if (!StringUtil.isEmpty(ssoHeaderVersion)) {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            PrintWriter out = response.getWriter();
            JsonResult jsonResult = SldResponse.fail(100,"缺少请求头！");
            out.print(JSON.toJSON(jsonResult));
            return false;
        }
        return true;
    }

}
