package com.ningdatech.kqapi.security.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ningdatech.kqapi.common.constant.BizConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * DefaultExpiredSessionStrategy
 * </p>
 *
 * @author WendyYang
 * @since 11:33 2024/4/20
 */
@Slf4j
@Component
public class DefaultExpiredSessionStrategy implements SessionInformationExpiredStrategy {

    private final ObjectMapper objectMapper;

    public DefaultExpiredSessionStrategy(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent sessionInformationExpiredEvent) throws IOException {
        HttpServletResponse response = sessionInformationExpiredEvent.getResponse();
        response.setContentType(BizConst.JSON_UTF8);
        response.getWriter().write(objectMapper.writeValueAsString(BizConst.SESSION_EXPIRE));
    }

}
