

package com.nantian.sc.cloud.auth.handler;

import com.nantian.sc.cloud.admin.api.entity.SysLog;
import com.nantian.sc.cloud.admin.api.feign.RemoteLogService;
import com.nantian.sc.cloud.common.core.constant.SecurityConstants;
import com.nantian.sc.cloud.common.log.util.SysLogUtils;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.handler.AbstractAuthenticationSuccessEventHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @date 2018/10/8
 */
@Slf4j
@Component
public class ScAuthenticationSuccessEventHandler extends AbstractAuthenticationSuccessEventHandler {

	@Autowired
	private RemoteLogService remoteLogService;

	/**
	 * 处理登录成功方法
	 * <p>
	 * 获取到登录的authentication 对象
	 *
	 * @param authentication 登录对象
	 * @param request        请求
	 * @param response       返回
	 */
	@Override
	public void handle(Authentication authentication, HttpServletRequest request, HttpServletResponse response) {
		ScUser scUser = (ScUser)authentication.getPrincipal();
		SysLog logVo = SysLogUtils.getSysLog();
		logVo.setTitle("用户登录成功");
		logVo.setCreateBy(scUser.getUsername());
		remoteLogService.saveLog(logVo, SecurityConstants.FROM_IN);
		log.info("用户：{} 登录成功", scUser);
	}
}
