package com.enterprise.manage.core.secure.process;

import com.enterprise.manage.common.tools.servlet.ServletUtil;
import com.enterprise.manage.common.web.response.Result;
import com.enterprise.manage.core.secure.domain.SecurityConstant;
import com.enterprise.manage.modules.system.service.ITokenService;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

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

/**
 * 自定义 spring security 用户登出处理类
 *
 * @author PeiZhi Guo
 * 2022-4-13 15:57
 */
@Component
public class SecurityLogoutSuccessHandler implements LogoutSuccessHandler {

    @Resource
    private ITokenService sysTokenService;

    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                Authentication authentication) throws IOException, ServletException {
        String token = httpServletRequest.getHeader(SecurityConstant.TOKEN_KEY);
        sysTokenService.deleteCacheToken(token);

        ServletUtil.jsonWrite(Result.success("退出成功"));
    }
}
