package com.mca.sec;

import java.io.IOException;
import java.time.LocalDateTime;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mca.cmmn.service.LogService;
import com.mca.cmmn.vo.LogActions;
import com.mca.cmmn.vo.LogVO;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;

public class LogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler{

  @Resource(name="logService")
  private LogService logService;

	private String successUrl = "/";
	
	public void setSuccessUrl(String successUrl){
		this.successUrl = successUrl;
	}
	
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

    LogVO logVO = new LogVO();
    logVO.setLog(authentication.getName(), LogActions.LOGOUT.getValue(), null, LocalDateTime.now());
    logService.insertLog(logVO);

    setDefaultTargetUrl(successUrl);
    super.onLogoutSuccess(request, response, authentication);
  }

	
}
