package com.ztmap.framework.interceptor;

import com.ztmap.common.utils.StringUtils;
import com.ztmap.framework.web.service.TokenService;
import com.ztmap.project.system.user.domain.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

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

/**
 * @ClassName AuthInterceptor
 * @Description TODO token 拦截认证
 * @Date 2020/8/22 16:57
 * @Version 1.0
 **/

@Component
public class AuthInterceptor implements HandlerInterceptor {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TokenService tokenService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LoginUser loginUser=null;
        try{
             loginUser = tokenService.getLoginUser(request);

        }catch(Exception e){
            response.sendRedirect("/api/login/noAuthority");
            return false;

        }
        if (StringUtils.isNotNull(loginUser))
        {
            tokenService.verifyToken(loginUser);
            return true;
        }
        response.sendRedirect("/api/login/noAuthority");

        return false;

    }

}
