소스 검색

feature:记录用户登出

xuexiaobo 6 년 전
부모
커밋
656d43820b

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
site/src/main/java/com/mooctest/crowd/site/controller/CommonController.java


+ 74 - 0
site/src/main/java/com/mooctest/crowd/site/util/OperationRecordUtil.java

@@ -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);
+    }
+}

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.