|
@@ -0,0 +1,74 @@
|
|
|
|
|
+package com.mooctest.crowd.site.util;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.mooctest.crowd.site.anticorruption.impl.data.UserInfo;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.http.*;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
+
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
+import java.sql.Timestamp;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author: Diors.Po
|
|
|
|
|
+ * @Email: 171256175@qq.com
|
|
|
|
|
+ * @date 2019-09-15 23:05
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Component
|
|
|
|
|
+public class OperationRecordUtil {
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private RestTemplate restTemplate;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${user.service.baseUrl}")
|
|
|
|
|
+ private String userServiceUrl;
|
|
|
|
|
+
|
|
|
|
|
+ public void recordLogoutAction(HttpServletRequest request){
|
|
|
|
|
+ Long userId = Long.parseLong((String)request.getSession().getAttribute("userId"));
|
|
|
|
|
+ String ip = getRequestIp(request);
|
|
|
|
|
+ HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
|
|
+ httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
|
|
+ params.put("ip", ip);
|
|
|
|
|
+ params.put("userId", userId);
|
|
|
|
|
+ params.put("operation", "Logout");
|
|
|
|
|
+ params.put("resource", "crowd-service");
|
|
|
|
|
+ HttpEntity<String> entity = new HttpEntity<>(params.toString() , httpHeaders);
|
|
|
|
|
+ restTemplate.exchange(userServiceUrl + "/api/operation_record", HttpMethod.POST, entity, Object.class);
|
|
|
|
|
+ Timestamp current = new Timestamp(System.currentTimeMillis());
|
|
|
|
|
+ log.info(String.format("IP:[%s] - User[%d] logout at [%s]", ip, userId, current.toString()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String getRequestIp(HttpServletRequest request){
|
|
|
|
|
+ String ip = request.getHeader("x-forwarded-for");
|
|
|
|
|
+ if (isNotValidIP(ip)) {
|
|
|
|
|
+ ip = request.getHeader("Proxy-Client-IP");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (isNotValidIP(ip)) {
|
|
|
|
|
+ ip = request.getHeader("WL-Proxy-Client-IP");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (isNotValidIP(ip)) {
|
|
|
|
|
+ ip = request.getRemoteAddr();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (isNotValidIP(ip)) {
|
|
|
|
|
+ ip = request.getHeader("http_client_ip");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (isNotValidIP(ip)) {
|
|
|
|
|
+ ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (ip.equals("0:0:0:0:0:0:0:1")) {
|
|
|
|
|
+ ip = "127.0.0.1";
|
|
|
|
|
+ }
|
|
|
|
|
+ if (ip.split(",").length > 1) {
|
|
|
|
|
+ ip = ip.split(",")[0];
|
|
|
|
|
+ }
|
|
|
|
|
+ return ip;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean isNotValidIP(String ip) {
|
|
|
|
|
+ return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|