Selaa lähdekoodia

feat: 添加token过期检查

weijianghai 11 kuukautta sitten
vanhempi
commit
79b11954f9

+ 32 - 6
scripts/8000.conf

@@ -20,6 +20,21 @@ server {
     location /house-car/oss {
         proxy_pass http://172.16.107.4:39000;
     }
+    
+    # 百度地图
+    location /dugis-baidu/baidumap/jsapi {
+        proxy_pass http://172.16.107.4:39000/house-car/oss/public/dugis-baidu/baidumap/jsapi;
+    }
+
+    # 不动产后端接口
+    location /house-car/house/api {
+        proxy_pass http://172.16.107.4:39100;
+    }
+
+    # 车辆后端接口
+    location /house-car/car/api {
+        proxy_pass http://172.16.107.4:39100;
+    }
 
     # 不动产资源地图接口
     location /house-car/house/resource-map/api {
@@ -87,15 +102,26 @@ server {
         proxy_pass http://172.16.107.4:39205;
     }
 
-
-    # 不动产系统
-    location /house-car/house/dist/ {
-        proxy_pass http://172.16.107.4:39089/house/dist/;
+    # 不动产gdc接口
+    location /house-car/house/dist/api {
+        proxy_pass http://172.16.107.4:39101;
+        #proxy_pass http://172.16.107.4:39089/house/dist/api;
     }
-    location /house-car/house/api/ { 
-  	proxy_pass http://172.16.107.4:39089/house/api/; 
+
+    # 不动产gdc页面
+    location /house-car/house/dist {
+        rewrite ^/house-car/house/dist/(.*)$ /$1 break;
+        proxy_pass http://172.16.107.4:39206;
     }
 
+    # 不动产系统
+    #location /house-car/house/dist/ {
+    #    proxy_pass http://172.16.107.4:39089/house/dist/;
+    #}
+    #location /house-car/house/api/ { 
+  #	proxy_pass http://172.16.107.4:39089/house/api/; 
+   # }
+
     #车辆系统
     location /house-car/car/ {
         proxy_redirect off;

+ 0 - 4
scripts/car-report/default.conf

@@ -19,8 +19,4 @@ server {
     location /house-car/oss {
         proxy_pass http://172.16.107.4:39000;
     }
-    # 百度地图
-    location /dugis-baidu {
-        proxy_pass http://172.16.107.4:39000;
-    }
 }

+ 0 - 4
scripts/car-resource-map/default.conf

@@ -19,8 +19,4 @@ server {
     location /house-car/oss {
         proxy_pass http://172.16.107.4:39000;
     }
-    # 百度地图
-    location /dugis-baidu {
-        proxy_pass http://172.16.107.4:39000;
-    }
 }

+ 0 - 4
scripts/house-report/default.conf

@@ -19,8 +19,4 @@ server {
     location /house-car/oss {
         proxy_pass http://172.16.107.4:39000;
     }
-    # 百度地图
-    location /dugis-baidu {
-        proxy_pass http://172.16.107.4:39000;
-    }
 }

+ 2 - 2
scripts/house-resource-map/default.conf

@@ -20,7 +20,7 @@ server {
         proxy_pass http://172.16.107.4:39000;
     }
     # 百度地图
-    location /dugis-baidu {
-        proxy_pass http://172.16.107.4:39000;
+    location /dugis-baidu/baidumap/jsapi {
+        proxy_pass http://172.16.107.4:39000/house-car/oss/public/dugis-baidu/baidumap/jsapi;
     }
 }

+ 2 - 0
src/main/java/com/nokia/financeapi/config/web/CorsConfig.java

@@ -2,6 +2,7 @@ package com.nokia.financeapi.config.web;
 
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Profile;
 import org.springframework.web.cors.CorsConfiguration;
 import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
 import org.springframework.web.filter.CorsFilter;
@@ -9,6 +10,7 @@ import org.springframework.web.filter.CorsFilter;
 /**
  * 跨域配置
  */
+@Profile({"test", "dev"})
 @Configuration
 public class CorsConfig {
 

+ 40 - 18
src/main/java/com/nokia/financeapi/config/web/RequestLogHandlerInterceptor.java

@@ -85,14 +85,26 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
         try {
             String urlDecodeToken = URLDecoder.decode(token, StandardCharsets.UTF_8);
             log.info("urlDecodeToken: {}", urlDecodeToken);
-            String decodeToken = AESUtil.decrypt(urlDecodeToken);
-            log.info("decodeToken: {}", decodeToken);
+            String decryptToken = AESUtil.decrypt(urlDecodeToken);
+            log.info("decryptToken: {}", decryptToken);
             Gson gson = new Gson();
-            Map<String, String> map = gson.fromJson(decodeToken, new TypeToken<Map<String, String>>() {
+            Map<String, String> map = gson.fromJson(decryptToken, new TypeToken<Map<String, String>>() {
             }.getType());
-            if (!"FINANCE".equals(map.get("APP_ID")) || !StringUtils.hasText(map.get("LOGIN_ID"))) {
+            if (!"FINANCE".equals(map.get("APP_ID"))
+                    || !StringUtils.hasText(map.get("LOGIN_ID"))
+                    || !StringUtils.hasText(map.get("TIME_STAMP"))
+                    || !StringUtils.hasText(map.get("EXPIRE_TIME"))
+            ) {
                 return forbidden(response);
             }
+            LocalDateTime timeStamp = LocalDateTime.parse(map.get("TIME_STAMP"),
+                    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+            int expireTime = Integer.parseInt(map.get("EXPIRE_TIME"));
+            LocalDateTime expireTimeStamp = timeStamp.plusSeconds(expireTime);
+            // token过期
+            if (now.isAfter(expireTimeStamp)) {
+                return unauthorized(response);
+            }
             Map<String, String> requestParameters = new HashMap<>();
             requestParameters.put("request_parameters", gson.toJson(parameters));
             requestParameters.put("request_body", body);
@@ -104,8 +116,10 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
             requestLogPo.setRequestParameters(gson.toJson(requestParameters));
             requestLogPo.setHeaders(gson.toJson(headers));
             requestLogPo.setAppId(map.get("APP_ID"));
-            setTimeStamp(map, requestLogPo);
             requestLogPo.setToken(urlDecodeToken);
+            requestLogPo.setTimeStamp(timeStamp);
+            requestLogPo.setExpireTime(expireTime);
+            requestLogPo.setExpireTimeStamp(expireTimeStamp);
             requestLogService.offer(requestLogPo);
         } catch (Exception e) {
             log.warn("token解密失败: {}", token);
@@ -114,18 +128,6 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
         return true;
     }
 
-    private void setTimeStamp(Map<String, String> map, RequestLogPo requestLogPo) {
-        String timeStamp = map.get("TIME_STAMP");
-        if (StringUtils.hasText(timeStamp)) {
-            try {
-                requestLogPo.setTimeStamp(LocalDateTime.parse(map.get("TIME_STAMP"),
-                        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
-            } catch (Exception e) {
-                log.warn("时间戳解析失败: {}", timeStamp);
-            }
-        }
-    }
-
     @Override
     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
                                 @Nullable Exception ex) throws Exception {
@@ -148,8 +150,28 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
     private boolean forbidden(HttpServletResponse response) throws IOException {
         response.setCharacterEncoding("UTF-8");
         response.setContentType("application/json; charset=utf-8");
-        byte[] bytes = new Gson().toJson(R.error().code(403).message("没有权限访问")).getBytes();
+        String r = new Gson().toJson(R.error().code(403).message("没有权限访问"));
+        byte[] bytes = r.getBytes();
         response.getOutputStream().write(bytes);
+        StopWatch stopWatch = STOP_WATCH_THREAD_LOCAL.get();
+        stopWatch.stop();
+        log.warn("返回 {}: {}", response.getStatus(), r);
+        log.info("耗时 {} ms", stopWatch.getTotalTimeMillis());
+        STOP_WATCH_THREAD_LOCAL.remove();
+        return false;
+    }
+
+    private boolean unauthorized(HttpServletResponse response) throws IOException {
+        response.setCharacterEncoding("UTF-8");
+        response.setContentType("application/json; charset=utf-8");
+        String r = new Gson().toJson(R.error().code(401).message("请刷新页面或重新登录"));
+        byte[] bytes = r.getBytes();
+        response.getOutputStream().write(bytes);
+        StopWatch stopWatch = STOP_WATCH_THREAD_LOCAL.get();
+        stopWatch.stop();
+        log.info("返回 {}: {}", response.getStatus(), r);
+        log.info("耗时 {} ms", stopWatch.getTotalTimeMillis());
+        STOP_WATCH_THREAD_LOCAL.remove();
         return false;
     }
 }

+ 6 - 2
src/main/java/com/nokia/financeapi/dao/common/RequestLogDao.java

@@ -24,7 +24,9 @@ request_parameters,
 headers,
 app_id,
 time_stamp,
-token
+token,
+expire_time,
+expire_time_stamp
 )
 values
 <foreach collection="list" item="item" index="index" separator=",">
@@ -37,7 +39,9 @@ values
 #{item.headers},
 #{item.appId},
 #{item.timeStamp},
-#{item.token}
+#{item.token},
+#{item.expireTime},
+#{item.expireTimeStamp}
 )
 </foreach>
 </script>

+ 8 - 0
src/main/java/com/nokia/financeapi/pojo/po/common/RequestLogPo.java

@@ -54,4 +54,12 @@ public class RequestLogPo {
      * 访问令牌
      */
     private String token;
+    /**
+     * 过期秒数,从token解密
+     */
+    private Integer expireTime;
+    /**
+     * 过期时间
+     */
+    private LocalDateTime expireTimeStamp;
 }

+ 3 - 1
src/main/java/com/nokia/financeapi/utils/AESUtil.java

@@ -77,10 +77,12 @@ public class AESUtil {
         // 时间戳
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         map.put("TIME_STAMP", sdf.format(new Date()));
+//        map.put("TIME_STAMP", "2024-03-29 00:00:00");
         // 当前登录账号
         map.put("LOGIN_ID", "test");
         //请求的地址
         map.put("REQUEST_URL","");
+        map.put("EXPIRE_TIME","10");
 
         // 加密串
         String encrypt = AESUtil.encrypt(new Gson().toJson(map), "2na$$PdV9AW8b#CS");
@@ -88,6 +90,6 @@ public class AESUtil {
         System.out.println(encrypt);
         //解密
         System.out.println(AESUtil.decrypt(encrypt,"2na$$PdV9AW8b#CS"));
-        System.out.println(AESUtil.decrypt("zoQtYlmhk/add/mBUBZD5mFJB1IXEwaLRS97Uf9z9Hlqdh8eF/add/c2gDUmD3pwGPfA8FQKBNTCB8LPlCPF/vHcU/2tL7Ps/add/6rn6w5rDS98R4GJueGgU01n12YZVS/FNx6pSI/add/BuEy1XyrSg8QBfx7igSozPxmdZ6a235/add/fqZPPSjoQReI","2na$$PdV9AW8b#CS"));
+        System.out.println(AESUtil.decrypt("zoQtYlmhk/add/mBUBZD5mFJB1IXEwaLRS97Uf9z9Hlqdh/UHio66b35GOo/eEziALCI/P8MJ/NfqMqiE5mpkHr0/add/309c0RoCYGzuwK1F7OYCOaxqUB83JYaeV6eJIWkvJpp","2na$$PdV9AW8b#CS"));
     }
 }

+ 2 - 2
src/main/resources/application-test.yml

@@ -1,9 +1,9 @@
 server:
-  port: 12130
+  port: 39100
 spring:
   datasource:
     driver-class-name: org.postgresql.Driver
-    url: jdbc:postgresql://192.168.50.3:15432/finance
+    url: jdbc:postgresql://192.168.50.3:15432/financialdb
     username: postgres
     password: NFQCgBA6YhNvgAqG6THw
 logging: