package com.simuwang.shiro.core.jwt;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.simuwang.base.common.util.ServletUtils;
import com.simuwang.shiro.core.ShiroUser;
import com.simuwang.shiro.core.adapter.LoginAuthAdapter;
import com.simuwang.shiro.utils.UserUtils;
import com.smppw.common.pojo.ResultVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author wangzaijun
 * @date 2024/9/9 16:33
 * @description 自定义的jwt token过滤器
 */
@Component
public class JwtFilter extends AccessControlFilter {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final JwtContext jwtContext;
    private final LoginAuthAdapter loginAuthAdapter;

    public JwtFilter(JwtContext jwtContext, LoginAuthAdapter loginAuthAdapter) {
        this.jwtContext = jwtContext;
        this.loginAuthAdapter = loginAuthAdapter;
    }

    /**
     * isAccessAllowed()判断是否携带了有效的JwtToken
     * onAccessDenied()是没有携带JwtToken的时候进行账号密码登录，登录成功允许访问，登录失败拒绝访问
     */
    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) {
        /*
         * 1. 返回true，shiro就直接允许访问url
         * 2. 返回false，shiro才会根据onAccessDenied的方法的返回值决定是否允许访问url
         *  这里先让它始终返回false来使用onAccessDenied()方法
         */
        return false;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        // 跟前端约定将jwtToken放在请求的Header的Authorization中，Authorization: token
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String requestURI = request.getRequestURI();
        String token = request.getHeader(JwtContext.HEADER);
        if (StrUtil.isBlank(token)) {
            this.onLoginFail(servletResponse, requestURI, "请先登录后操作");
            return false;
        }
        Claims claims;
        try {
            claims = this.jwtContext.getClaimsByToken(token);
        } catch (ExpiredJwtException e) {
            this.onLoginFail(servletResponse, requestURI, "登录已过期，请重新登录");
            return false;
        }
        String username = claims.getSubject();
        String requestIp = ServletUtils.getIpAddr(request);
        String validToken = this.jwtContext.getUserCache(username, requestIp);
        if (!token.equals(validToken)) {
            this.onLoginFail(servletResponse, requestURI, "token非法");
            return false;
        }
        // 设置登录用户信息
        ShiroUser shiroUser = this.loginAuthAdapter.findUserByUsername(username);
        UserUtils.setPrincipal(shiroUser);
        return true;
    }

    //登录失败要执行的方法
    private void onLoginFail(ServletResponse response, String url, String msg) throws IOException {
        this.logger.warn("接口{} 认证或授权错误：{}", url, msg);
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
        httpServletResponse.getWriter().print(JSONUtil.toJsonStr(ResultVo.fail(401, msg)));
    }
}
