package com.ningdatech.kqapi.security.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ningdatech.kqapi.common.constant.BizConst;
import lombok.AllArgsConstructor;
import org.apache.http.HttpStatus;
import org.springframework.security.web.session.InvalidSessionStrategy;
import org.springframework.stereotype.Component;

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

/**
 * <p>
 * DefaultInvalidSessionStrategy
 * </p>
 *
 * @author WendyYang
 * @since 13:52 2023/3/2
 */
@Component
@AllArgsConstructor
public class DefaultInvalidSessionStrategy implements InvalidSessionStrategy {

    private final ObjectMapper objectMapper;

    @Override
    public void onInvalidSessionDetected(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType(BizConst.JSON_UTF8);
        response.setStatus(HttpStatus.SC_UNAUTHORIZED);
        response.getWriter().write(objectMapper.writeValueAsString(BizConst.SESSION_EXPIRE));
    }

}
