

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.handler.AbstractAuthenticationFailureEvenHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

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

/**
 *
 * @date 2018/10/8
 */
@Slf4j
@Component
public class ScAuthenticationFailureEvenHandler extends AbstractAuthenticationFailureEvenHandler {

	@Autowired
	private RemoteLogService remoteLogService;

	/**
	 * 处理登录失败方法
	 * <p>
	 *
	 * @param authenticationException 登录的authentication 对象
	 * @param authentication          登录的authenticationException 对象
	 * @param request                 请求
	 * @param response                响应
	 */
	@Override
	public void handle(AuthenticationException authenticationException, Authentication authentication, HttpServletRequest request, HttpServletResponse response) {
		String userName = (String) authentication.getPrincipal();
		SysLog logVo = SysLogUtils.getSysLog();
		logVo.setTitle("用户登录失败");
		logVo.setCreateBy(userName);
		logVo.setException(authenticationException.getLocalizedMessage());
		remoteLogService.saveLog(logVo, SecurityConstants.FROM_IN);
		log.info("用户：{} 登录失败，异常：{}", userName, authenticationException.getLocalizedMessage());
	}
}
