package com.mortals.xhx.busiz.security.filter;

import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.busiz.security.SecurityUtils;
import com.mortals.xhx.busiz.security.DeviceTokenService;
import com.mortals.xhx.module.device.model.DeviceEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * token过滤器 验证token有效性
 *
 * @author ruoyi
 */
//@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
    @Autowired
    private DeviceTokenService deviceTokenService;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws ServletException, IOException {
        DeviceEntity device = deviceTokenService.getLoginDevice(request);
        if (StringUtils.isNotNull(device) && StringUtils.isNull(SecurityUtils.getAuthentication())) {
            deviceTokenService.verifyToken(device);
        }
        chain.doFilter(request, response);
    }
}
