package cc.mrbird.febs.auth.handler;

import com.ruoyi.common.core.utils.FebsUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.stereotype.Component;

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

import static com.ruoyi.common.core.web.domain.AjaxResult.DATA_TAG;
import static com.ruoyi.common.core.web.domain.AjaxResult.MSG_TAG;

/**
 * @author MrBird
 */
@Slf4j
@Component
public class FebsWebLoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    private final RequestCache requestCache = new HttpSessionRequestCache();

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
        SavedRequest savedRequest = requestCache.getRequest(request, response);
        HttpSession session = request.getSession(false);
        if (session != null) {
            Object attribute = session.getAttribute("SPRING_SECURITY_SAVED_REQUEST");
            log.info("跳转到登录页的地址为: {}", attribute);
        }
        if (FebsUtil.isAjaxRequest(request)) {
            AjaxResult error = new AjaxResult();
            if (savedRequest == null) {
                error = AjaxResult.error("请通过授权码模式跳转到该页面");
                FebsUtil.makeFailureResponse(response, error.get(MSG_TAG));
                return;
            }
            error.put(DATA_TAG, savedRequest.getRedirectUrl());
            FebsUtil.makeSuccessResponse(response, error.get(DATA_TAG));
        } else {
            if (savedRequest == null) {
                super.onAuthenticationSuccess(request, response, authentication);
                return;
            }
            clearAuthenticationAttributes(request);
            getRedirectStrategy().sendRedirect(request, response, savedRequest.getRedirectUrl());
        }
    }
}
