package com.org.system.config.security;

import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.ContentType;
import com.alibaba.fastjson.JSONObject;
import com.org.system.config.async.AsyncFactory;
import com.org.system.config.async.AsyncManager;
import com.org.system.entity.LoginUser;
import com.org.system.service.TokenService;
import com.org.utils.AjaxResult;
import com.org.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;

/** 自定义退出处理类 返回成功 **/
@Configuration
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler
{
    @Autowired
    private TokenService tokenService;

    /**
     * 退出处理
     * 
     * @return
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException {
        LoginUser loginUser = tokenService.getLoginUser(request);

        if (null != loginUser) {
            String userName = loginUser.getUsername();

            // 删除用户缓存记录
            tokenService.delLoginUser(loginUser.getToken());

            // 记录用户退出日志
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGOUT, "退出成功"));
        }
        ServletUtil.write(response, JSONObject.toJSONString(AjaxResult.success("退出成功")), ContentType.JSON.toString(Charset.defaultCharset()));
    }
}
