13693261870
2022-09-20 a666b5f9741ef9b21f547d3b2141752a0383c70c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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);
 
    }
 
}