package org.apereo.cas.web.landtool.log.listener;
|
|
import java.util.Map;
|
|
import org.apache.commons.lang3.StringUtils;
|
import org.apereo.cas.authentication.principal.Service;
|
import org.apereo.cas.ticket.TicketGrantingTicket;
|
import org.apereo.cas.web.landtool.log.service.LogService;
|
import org.apereo.cas.web.landtool.rabbitmq.RadarRabbitMQTemplate;
|
import org.apereo.cas.web.landtool.rabbitmq.config.RabbitMQProperties;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.context.event.EventListener;
|
import org.springframework.scheduling.annotation.Async;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
/**
|
* @author Tanbin
|
* @date 2018-12-12
|
*/
|
public class LogListener {
|
private static final Logger LOGGER = LoggerFactory.getLogger(LogListener.class);
|
|
@Autowired
|
private LogService logService;
|
@Autowired
|
private RadarRabbitMQTemplate rabbitMQTemplate;
|
@Autowired
|
private RabbitMQProperties rabbitConfig;
|
|
public LogListener() {
|
|
}
|
|
/**
|
* 发送用户登陆i消息
|
* @param event
|
*/
|
@EventListener
|
@Async
|
public void onTgtCreated(org.apereo.cas.support.events.CasTicketGrantingTicketCreatedEvent event) {
|
TicketGrantingTicket ticketGrantingTicket = event.getTicketGrantingTicket();
|
String userId = ticketGrantingTicket.getAuthentication().getPrincipal().getId();
|
String tgtId = ticketGrantingTicket.getId();
|
String clientIp = (String) ticketGrantingTicket.getAuthentication().getAttributes().get("clientIp");
|
String browserInfo = (String) ticketGrantingTicket.getAuthentication().getAttributes().get("browserInfo");
|
String serviceUrl = (String) ticketGrantingTicket.getAuthentication().getAttributes().get("serviceUrl");
|
if(!StringUtils.isNotBlank(browserInfo)) {
|
browserInfo = "未知";
|
}
|
if(!StringUtils.isNotBlank(serviceUrl)) {
|
serviceUrl = "ssologin";
|
}
|
LOGGER.debug("#####LOGIN:[{}],[{}],[{}],[{}], [{}]", userId, clientIp, browserInfo, tgtId , serviceUrl);
|
System.out.println("onTgtCreated#####VALIDATE:[{}],[{}],[{}],[{}],[{}]"+ userId+"-"+ clientIp+"-"+tgtId+"-"+ serviceUrl);
|
logService.addLog(userId, clientIp, 1, 0, browserInfo, serviceUrl);
|
if(rabbitConfig.isEnabled()){
|
JSONObject json = new JSONObject();
|
json.put("userId", userId);
|
json.put("clientIp", clientIp);
|
json.put("status", "登入");
|
json.put("browserInfo", browserInfo);
|
// json.put("serviceUrl", serviceUrl);
|
rabbitMQTemplate.send(rabbitConfig.getRmqRoutingKeyPrefixCas()+"login", json.toJSONString());
|
}
|
}
|
|
/**
|
* 发送用户登出消息
|
* @param event
|
*/
|
@EventListener
|
@Async
|
public void onTgtDestroyed(org.apereo.cas.support.events.CasTicketGrantingTicketDestroyedEvent event) {
|
TicketGrantingTicket ticketGrantingTicket = event.getTicketGrantingTicket();
|
String userId = ticketGrantingTicket.getAuthentication().getPrincipal().getId();
|
String tgtId = ticketGrantingTicket.getId();
|
String clientIp = (String) ticketGrantingTicket.getAuthentication().getAttributes().get("clientIp");
|
String browserInfo = (String) ticketGrantingTicket.getAuthentication().getAttributes().get("browserInfo");
|
if(!StringUtils.isNotBlank(browserInfo)) {
|
browserInfo = "未知";
|
}
|
LOGGER.debug("#####LOGOUT:[{}],[{}],[{}]", userId, clientIp, tgtId);
|
System.out.println("onTgtDestroyed#####LOGOUT:[{}],[{}],[{}]"+userId+"-"+clientIp+"-"+tgtId);
|
if(rabbitConfig.isEnabled()){
|
logService.addLog(userId, clientIp, 2, 0, browserInfo, "ssologout");
|
JSONObject json = new JSONObject();
|
json.put("userId", userId);
|
json.put("clientIp", clientIp);
|
json.put("status", "登出");
|
json.put("browserInfo", browserInfo);
|
//json.put("serviceUrl", "ssologout");
|
rabbitMQTemplate.send(rabbitConfig.getRmqRoutingKeyPrefixCas()+"logout", json.toJSONString());
|
}
|
}
|
|
/**
|
* 登录登出日志添加
|
* @param event
|
*/
|
@EventListener
|
@Async
|
public void onStValidated(org.apereo.cas.support.events.CasServiceTicketValidatedEvent event) {
|
TicketGrantingTicket ticketGrantingTicket = event.getServiceTicket().getGrantingTicket();
|
String userId = ticketGrantingTicket.getAuthentication().getPrincipal().getId();
|
String tgtId = ticketGrantingTicket.getId();
|
String clientIp = (String) ticketGrantingTicket.getAuthentication().getAttributes().get("clientIp");
|
String stId = event.getServiceTicket().getId();
|
String appUrl = "";
|
String browserInfo = (String) ticketGrantingTicket.getAuthentication().getAttributes().get("browserInfo");
|
if(!StringUtils.isNotBlank(browserInfo)) {
|
browserInfo = "未知";
|
}
|
Map<String, Service> services = ticketGrantingTicket.getServices();
|
Service service = services.get(stId);
|
if(service != null) {
|
appUrl = service.getId();
|
}
|
LOGGER.debug("#####VALIDATE:[{}],[{}],[{}],[{}],[{}]", userId, clientIp, tgtId, stId, appUrl);
|
System.out.println("onStValidated#####VALIDATE:[{}],[{}],[{}],[{}],[{}]"+ userId+"-"+ clientIp+"-"+tgtId+"-"+stId+"-"+ appUrl);
|
logService.addLog(userId, clientIp, 3, 0, browserInfo, appUrl);
|
|
}
|
|
}
|