Explorar el Código

优化鉴权逻辑

ximinghao hace 4 meses
padre
commit
aa5d1b03ae

+ 0 - 29
src/main/java/com/skyversation/xjcy/dms/DMSService.java

@@ -9,7 +9,6 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
-import org.springframework.util.StringUtils;
 
 import java.time.LocalDate;
 import java.util.*;
@@ -19,34 +18,6 @@ import java.util.stream.Collectors;
 public class DMSService {
     @Value("${app.dms.path}")
     private String path;
-    @Value("${app.oauth.login-name}")
-    private String loginName;
-    @Value("${app.oauth.password}")
-    private String password;
-    @Value("${app.oauth.path}")
-    private String oauthPath;
-
-    public String loginForToken() throws Exception {
-        if (!StringUtils.hasText(loginName) || !StringUtils.hasText(password)) {
-            throw new Exception("请提供用于访问DMS的账号密码");
-        }
-        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
-        params.add("userName", loginName);
-        params.add("password", password);
-        params.add("clientId", "2");
-        String response = HttpUtil.requestPost(oauthPath, params, new HashMap<>());
-        JSONObject jsonObject = JSON.parseObject(response);
-        if ("密码错误".equals(jsonObject.getString("message"))) {
-            loginName = null;
-            password = null;
-            throw new Exception("请检查使用的账户和密码是否正确,已停止使用该账号密码以避免锁定账号");
-        }
-        if (!Objects.equals(jsonObject.getString("code"), "200")) {
-            throw new Exception(jsonObject.getString("message"));
-        }
-        return jsonObject.getString("message");
-    }
-
 
     private JSONObject sendQueryToDms(DMSRequest request) {
         JSONObject jsonObject;

+ 66 - 0
src/main/java/com/skyversation/xjcy/oauth/LoginService.java

@@ -0,0 +1,66 @@
+package com.skyversation.xjcy.oauth;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.skyversation.xjcy.dms.DMSService;
+import com.skyversation.xjcy.util.HttpUtil;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.util.StringUtils;
+
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Objects;
+
+@Service
+public class LoginService {
+
+    @Value("${app.oauth.login-name}")
+    private String loginName;
+    @Value("${app.oauth.password}")
+    private String password;
+    @Value("${app.oauth.path}")
+    private String oauthPath = null;
+
+    private String cacheToken;
+
+    private String loginForToken() throws RuntimeException {
+        if (!StringUtils.hasText(loginName) || !StringUtils.hasText(password)) {
+            throw new RuntimeException("请提供用于访问DMS的账号密码");
+        }
+        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
+        params.add("userName", loginName);
+        params.add("password", password);
+        params.add("clientId", "2");
+        String response = HttpUtil.requestPost(oauthPath+"/api/user/login", params, new HashMap<>());
+        JSONObject jsonObject = JSON.parseObject(response);
+        if ("密码错误".equals(jsonObject.getString("message"))) {
+            loginName = null;
+            password = null;
+            throw new RuntimeException("请检查使用的账户和密码是否正确,已停止使用该账号密码以避免锁定账号");
+        }
+        if (!Objects.equals(jsonObject.getString("code"), "200")) {
+            throw new RuntimeException(jsonObject.getString("message"));
+        }
+        return jsonObject.getString("message");
+    }
+    public boolean checkToken(String token){
+        Map<String, String> header = new HashMap<>();
+        header.put("token", token);
+        String response = HttpUtil.requestGet(oauthPath+"/user/validateToken",null,header);
+        JSONObject jsonObject = JSON.parseObject(response);
+        Integer code = jsonObject.getInteger("code");
+        return Integer.valueOf(200).equals(code);
+    }
+    public String getToken() throws RuntimeException {
+        if (StringUtils.hasText(cacheToken)&&checkToken(cacheToken)) {
+            return cacheToken;
+        }else {
+            cacheToken = loginForToken();
+            return cacheToken;
+        }
+    }
+}

+ 10 - 14
src/main/java/com/skyversation/xjcy/service/DataCountService.java

@@ -6,6 +6,7 @@ import com.skyversation.xjcy.bean.*;
 import com.skyversation.xjcy.dms.DMSQuery;
 import com.skyversation.xjcy.dms.DMSService;
 import com.skyversation.xjcy.enums.EnterpriseLevel;
+import com.skyversation.xjcy.oauth.LoginService;
 import com.skyversation.xjcy.util.SpecialisationStringCollector;
 import com.skyversation.xjcy.util.TimeUtil;
 import lombok.NoArgsConstructor;
@@ -55,6 +56,8 @@ public class DataCountService {
     private final AtomicLong lastStartTime = new AtomicLong(0);
 
     private final AtomicBoolean running = new AtomicBoolean(false);
+    @Resource
+    private LoginService loginService;
 
     @PostConstruct
     public void tryCount() {
@@ -113,22 +116,15 @@ public class DataCountService {
         buildMap = new LinkedMultiValueMap<>();
         allPark = new ArrayList<>();
 
-        String token;
-        try {
-            token = dmsService.loginForToken();
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
-
         LocalDate now = LocalDate.now();
 
-        //统一查询所有需要的数据避免处理时间太长token过期,数据量大不了所以不做流式
-        List<IndustrialPark> allIndustrialPark = dmsService.query(token, DMSQuery.INDUSTRIAL_PARK, IndustrialPark.class, now);
-        List<Enterprise> allEnterprise = dmsService.query(token, DMSQuery.ENTERPRISE, Enterprise.class, now);
-        List<EnterpriseEconomic> inDateEnterpriseEconomic = dmsService.query(token, DMSQuery.ECONOMIC_ALL_INDEED, EnterpriseEconomic.class, now);
-        List<Order> allOrder = dmsService.query(token, DMSQuery.ORDER, Order.class, now);
-        List<LeaseDetail> lastLeaseDetail = dmsService.query(token, DMSQuery.LAST_LEASE_DETAIL, LeaseDetail.class, now);
-        List<LeaseDetail> lastYearLease = dmsService.query(token, DMSQuery.LAST_YEAR_LEASE_DETAIL, LeaseDetail.class, now);
+        //数据量大不了所以不做流式
+        List<IndustrialPark> allIndustrialPark = dmsService.query(loginService.getToken(), DMSQuery.INDUSTRIAL_PARK, IndustrialPark.class, now);
+        List<Enterprise> allEnterprise = dmsService.query(loginService.getToken(), DMSQuery.ENTERPRISE, Enterprise.class, now);
+        List<EnterpriseEconomic> inDateEnterpriseEconomic = dmsService.query(loginService.getToken(), DMSQuery.ECONOMIC_ALL_INDEED, EnterpriseEconomic.class, now);
+        List<Order> allOrder = dmsService.query(loginService.getToken(), DMSQuery.ORDER, Order.class, now);
+        List<LeaseDetail> lastLeaseDetail = dmsService.query(loginService.getToken(), DMSQuery.LAST_LEASE_DETAIL, LeaseDetail.class, now);
+        List<LeaseDetail> lastYearLease = dmsService.query(loginService.getToken(), DMSQuery.LAST_YEAR_LEASE_DETAIL, LeaseDetail.class, now);
         //处理一下拿到的数据,建一建索引
         Map<String, List<EnterpriseEconomic>> enterpriseEconomicMap = new HashMap<>();
         inDateEnterpriseEconomic.forEach(e -> {

+ 2 - 2
src/main/java/com/skyversation/xjcy/service/OWSService.java

@@ -44,8 +44,8 @@ public class OWSService {
             map.add(entry.getKey(), entry.getValue());
         }
         try {
-            ResponseEntity<String> result = HttpUtil.requestGet(owsPath, map, Collections.emptyMap());
-            JSONObject json = JSONObject.parseObject(result.getBody());
+            String result = HttpUtil.requestGet(owsPath, map, Collections.emptyMap());
+            JSONObject json = JSONObject.parseObject(result);
             return json.getJSONArray("features");
         } catch (RestClientException e) {
             if (depth >= 3) {

+ 5 - 2
src/main/java/com/skyversation/xjcy/service/SerialNumberGenerator.java

@@ -1,6 +1,7 @@
 package com.skyversation.xjcy.service;
 
 import com.skyversation.xjcy.dms.DMSService;
+import com.skyversation.xjcy.oauth.LoginService;
 import lombok.Getter;
 import org.springframework.stereotype.Service;
 
@@ -17,7 +18,9 @@ import java.util.concurrent.locks.ReentrantLock;
 @Service
 public class SerialNumberGenerator {
     @Resource
-    DMSService dmsService;
+    private DMSService dmsService;
+    @Resource
+    private LoginService loginService;
 
     // 前缀枚举定义(可根据需要扩展)
     @Getter
@@ -60,7 +63,7 @@ public class SerialNumberGenerator {
     public void autoInit() {
         String token;
         try {
-            token = dmsService.loginForToken();
+            token = loginService.getToken();
         } catch (Exception e) {
             throw new RuntimeException(e);
         }

+ 2 - 2
src/main/java/com/skyversation/xjcy/util/HttpUtil.java

@@ -57,7 +57,7 @@ public class HttpUtil {
         return response.getBody();
     }
 
-    public static ResponseEntity<String> requestGet(String url,
+    public static String requestGet(String url,
                                                     MultiValueMap<String, String> params,
                                                     Map<String, String> headerMap) {
         SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
@@ -80,7 +80,7 @@ public class HttpUtil {
 
         HttpEntity<?> entity = new HttpEntity<>(headers);
         URI uri = URI.create(builder.toUriString());
-        return client.exchange(uri, HttpMethod.GET, entity, String.class);
+        return client.exchange(uri, HttpMethod.GET, entity, String.class).getBody();
     }
 
 

+ 1 - 1
src/main/resources/application.yml

@@ -25,6 +25,6 @@ app:
   oauth:
     login-name: ${DMS_LOGIN_NAME:user_hj}
     password: ${DMS_PASSWORD:Hj@123456}
-    path: ${OAUTH_LOGIN_PATH:http://121.43.55.7:10086/oauth/api/user/login}
+    path: ${OAUTH_LOGIN_PATH:http://121.43.55.7:10086/oauth}
   ows:
     path: ${OWS_PATH:http://121.43.55.7:8889/geoserver/xjxm/ows}