hcst_sunzhen 5 år sedan
förälder
incheckning
7fcc2a45c9

+ 97 - 0
src/main/java/cn/com/ctop/okr/interceptors/BrowseLogHandelInterceptor.java

@@ -0,0 +1,97 @@
+package cn.com.ctop.okr.interceptors;
+
+import cn.com.ctop.okr.utils.DateUtils;
+import cn.com.ctop.okr.utils.JwtUtil;
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Component;
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Created by JQ.bi on 2020.5.8
+ */
+@Component
+@Slf4j
+public class BrowseLogHandelInterceptor extends HandlerInterceptorAdapter {
+
+    private static final String REQUEST_METHOD = "GET_POST_PUT_DELETE_OPTIONS";
+
+    private final static ExecutorService executorServicePool;
+
+    //加载一次
+    static {
+        int threadCount = Runtime.getRuntime().availableProcessors();
+        ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
+        builder.setNameFormat("BrowseLogHandlerFactory");
+        builder.setDaemon(true);
+        builder.setUncaughtExceptionHandler((t, e) -> {
+        });//忽略异常
+        builder.setPriority(Thread.MIN_PRIORITY);
+        executorServicePool = new ThreadPoolExecutor(threadCount, threadCount,
+                0L, TimeUnit.MILLISECONDS,
+                new LinkedBlockingQueue<Runnable>(100000), builder.build(), new ThreadPoolExecutor.DiscardPolicy());
+    }
+
+
+    @Override
+    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
+        if (REQUEST_METHOD.contains(request.getMethod())) {
+            if (request.getRequestURI().endsWith("/sys/permission/getUserPermissionByToken")||request.getRequestURI().endsWith("/getEncryptedString")||request.getRequestURI().endsWith("/login")||request.getRequestURI().endsWith("/sys/annountCement/listByUser")) {
+                return;
+            }
+            boolean isSuccess = true;
+            if (response.getStatus() != 200) {
+                isSuccess = false;
+            }
+            String userName = JwtUtil.getUserNameByToken(request);
+            if (StringUtils.isEmpty(userName)) {
+                log.warn("登陆账号用户名为空");
+            }
+            String token = request.getHeader("X-Access-Token");
+            String uri = request.getRequestURI();
+            String currentDate = DateUtils.now();
+            BrowseLogRunnable runnable = new BrowseLogRunnable(userName, isSuccess,token, uri, currentDate);
+            executorServicePool.submit(runnable);
+        }
+    }
+
+
+    private static class BrowseLogRunnable implements Runnable {
+        String userName;
+        Boolean isSuccess;
+        String token;
+        String uri;
+        String currentDate;
+        BrowseLogRunnable(String userName, boolean isSuccess, String token, String uri, String currentDate) {
+            this.userName = userName;
+            this.isSuccess = isSuccess;
+            this.token = token;
+            this.uri = uri;
+            this.currentDate = currentDate;
+        }
+
+        @Override
+        public void run() {
+            try {
+                if (!StringUtils.isEmpty(token)) {
+                    if(isSuccess){
+                        log.info("-SUCCESS " + currentDate + " 用户:" + userName + " 访问了[" + uri + "]?token=\"" + token + "\"");
+                    }else {
+                        log.info("-FALSE " + currentDate + " 用户:" + userName + "” 访问了[" + uri + "]?token=\"" + token + "\"");
+                    }
+                }
+            } catch (Exception e) {
+                log.error(e.toString());
+            }
+        }
+    }
+}
+

+ 4 - 4
src/main/java/cn/com/ctop/okr/utils/JwtUtil.java

@@ -70,14 +70,14 @@ public class JwtUtil {
         return JWT.create().withClaim("username", username).withExpiresAt(date).sign(algorithm);
 
     }
-/*
-    *//**
+
+    /**
      * 根据request中的token获取用户账号
      *
      * @param request
      * @return
      * @throws Exception
-     *//*
+     */
     public static String getUserNameByToken(HttpServletRequest request) throws Exception {
         String accessToken = request.getHeader("X-Access-Token");
         String username = getUsername(accessToken);
@@ -87,7 +87,7 @@ public class JwtUtil {
         return username;
     }
 
-    *//**
+    /**
      * 从session中获取变量
      *
      * @param key

+ 111 - 0
src/main/java/cn/com/ctop/okr/utils/StringUtils.java

@@ -0,0 +1,111 @@
+package cn.com.ctop.okr.utils;
+
+import java.util.ArrayList;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @author jeecg-boot
+ * 2019年12月5日19:57:58
+ */
+public class StringUtils {
+    public static final String COMMA = ",";
+    public static ArrayList<String> stringCutFromBrace(String origin) {
+        String pattern = "(?<=\\{)(.+?)(?=\\})";
+        Pattern p = Pattern.compile(pattern);
+        ArrayList list = new ArrayList();
+        Matcher m = p.matcher(origin);
+        while (m.find()) {
+            list.add(m.group());
+        }
+        return list;
+    }
+
+    /**
+     * 将下划线风格转换为驼峰风格
+     * @param inputString
+     * @return
+     */
+    public static String underlineToCamehunp(String inputString){
+        StringBuilder sb = new StringBuilder();
+        boolean nextUppercase = false;
+        for (int i = 0; i < inputString.length(); i++) {
+            char c = inputString.charAt(i);
+            if (c == '_') {
+                if (sb.length()>0) {
+                    nextUppercase = true;
+                }
+            }else {
+                if (nextUppercase) {
+                    sb.append(Character.toUpperCase(c));
+                    nextUppercase = false;
+                }else {
+                    sb.append(Character.toLowerCase(c));
+                }
+            }
+        }
+        return sb.toString();
+    }
+    private static Pattern linePattern = Pattern.compile("_(\\w)");
+    /** 下划线转驼峰 */
+    public static String lineToHump(String str) {
+        str = str.toLowerCase();
+        Matcher matcher = linePattern.matcher(str);
+        StringBuffer sb = new StringBuffer();
+        while (matcher.find()) {
+            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
+        }
+        matcher.appendTail(sb);
+        return sb.toString();
+    }
+
+    /** 驼峰转下划线(简单写法,效率低于{@link #humpToLine2(String)}) */
+    public static String humpToLine(String str) {
+        return str.replaceAll("[A-Z]", "_$0").toLowerCase();
+    }
+
+    private static Pattern humpPattern = Pattern.compile("[A-Z]");
+
+    /** 驼峰转下划线,效率比上面高 */
+    public static String humpToLine2(String str) {
+        Matcher matcher = humpPattern.matcher(str);
+        StringBuffer sb = new StringBuffer();
+        while (matcher.find()) {
+            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
+        }
+        matcher.appendTail(sb);
+        return sb.toString();
+    }
+
+    //中文转Unicode
+    public static String cnToUnicode(String cn) {
+        char[] chars = cn.toCharArray();
+        String returnStr = "";
+        for (int i = 0; i < chars.length; i++) {
+            returnStr += "\\u" + Integer.toString(chars[i], 16);
+        }
+        return returnStr;
+    }
+
+    //Unicode转中文方法
+    public static String unicodeToCn(String unicode) {
+        /** 以 \ u 分割,因为java注释也能识别unicode,因此中间加了一个空格*/
+        String[] strs = unicode.split("\\\\u");
+        String returnStr = "";
+        // 由于unicode字符串以 \ u 开头,因此分割出的第一个字符是""。
+        for (int i = 1; i < strs.length; i++) {
+            returnStr += (char) Integer.valueOf(strs[i], 16).intValue();
+        }
+        return returnStr;
+    }
+
+    public static String replaceBlank(String str) {
+        String dest = "";
+        if (str!=null) {
+            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
+            Matcher m = p.matcher(str);
+            dest = m.replaceAll("");
+        }
+        return dest;
+    }
+}