hecheng %!s(int64=3) %!d(string=hai) anos
pai
achega
29ab09b117

+ 6 - 1
pom.xml

@@ -259,7 +259,6 @@
                 <groupId>com.zd.swagger</groupId>
                 <artifactId>common-swagger</artifactId>
                 <version>${zd.version}</version>
-                <scope>compile</scope>
             </dependency>
 
             <dependency>
@@ -269,6 +268,12 @@
             </dependency>
 
             <dependency>
+                <groupId>com.zd.auth</groupId>
+                <artifactId>zd-auth-api</artifactId>
+                <version>${zd.version}</version>
+            </dependency>
+
+            <dependency>
                 <groupId>com.zd.system</groupId>
                 <artifactId>zd-system-api</artifactId>
                 <version>${zd.version}</version>

+ 1 - 0
zd-api/pom.xml

@@ -19,6 +19,7 @@
         <module>zd-chemical-api</module>
         <module>zd-algorithm-api</module>
         <module>zd-airbottle-api</module>
+        <module>zd-auth-api</module>
     </modules>
 
     <dependencies>

+ 17 - 0
zd-api/zd-auth-api/pom.xml

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>zd-api</artifactId>
+        <groupId>com.zd</groupId>
+        <version>3.1.0</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>com.zd.auth</groupId>
+    <artifactId>zd-auth-api</artifactId>
+    <packaging>jar</packaging>
+    <description>auth接口依赖</description>
+
+</project>

+ 20 - 0
zd-api/zd-auth-api/src/main/java/com/zd/auth/feign/RemoteAuthService.java

@@ -0,0 +1,20 @@
+package com.zd.auth.feign;
+
+import com.zd.auth.feign.factory.RemoteAuthFallback;
+import com.zd.auth.form.LoginBody;
+import com.zd.model.constant.ApplicationConstants;
+import com.zd.model.domain.R;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+
+@FeignClient(contextId = "remoteAuthService", value = ApplicationConstants.AUTH_SERVICE, fallbackFactory = RemoteAuthFallback.class)
+public interface RemoteAuthService {
+
+    /**
+     * 一体机登录
+     * 小程序登录也在用
+     */
+    @PostMapping("/one/login")
+    R<?> oneLogin(@RequestBody LoginBody form);
+}

+ 25 - 0
zd-api/zd-auth-api/src/main/java/com/zd/auth/feign/factory/RemoteAuthFallback.java

@@ -0,0 +1,25 @@
+package com.zd.auth.feign.factory;
+
+import com.zd.auth.feign.RemoteAuthService;
+import com.zd.auth.form.LoginBody;
+import com.zd.model.domain.R;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.cloud.openfeign.FallbackFactory;
+import org.springframework.stereotype.Component;
+
+/**
+ * 用户服务降级处理
+ *
+ * @author zd
+ */
+@Component
+public class RemoteAuthFallback implements FallbackFactory<RemoteAuthService> {
+    private static final Logger log = LoggerFactory.getLogger(RemoteAuthFallback.class);
+
+    @Override
+    public RemoteAuthService create(Throwable throwable) {
+        log.error("授权登录服务调用失败:{}", throwable.getMessage());
+        return form -> R.fail("授权服务调用失败:"+throwable.getMessage());
+    }
+}

zd-auth/src/main/java/com/zd/auth/form/LoginBody.java → zd-api/zd-auth-api/src/main/java/com/zd/auth/form/LoginBody.java


zd-auth/src/main/java/com/zd/auth/form/RegisterBody.java → zd-api/zd-auth-api/src/main/java/com/zd/auth/form/RegisterBody.java


+ 9 - 0
zd-auth/pom.xml

@@ -64,6 +64,15 @@
                 </exclusion>
             </exclusions>
         </dependency>
+        <dependency>
+            <groupId>com.zd.auth</groupId>
+            <artifactId>zd-auth-api</artifactId>
+            <version>3.1.0</version>
+        </dependency>
+        <dependency>
+            <groupId>com.zd.swagger</groupId>
+            <artifactId>common-swagger</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

+ 4 - 0
zd-modules/zd-algorithm/pom.xml

@@ -185,6 +185,10 @@
             <groupId>com.zd.system</groupId>
             <artifactId>zd-system-api</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.zd.auth</groupId>
+            <artifactId>zd-auth-api</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

+ 2 - 5
zd-modules/zd-algorithm/src/main/java/com/zd/alg/AlgorithmApplication.java

@@ -9,16 +9,13 @@ import com.zd.common.core.launch.ZdStartApplication;
 import com.zd.common.mqtt.model.MqttProperties;
 import com.zd.model.constant.ApplicationConstants;
 import com.zd.model.constant.Constants;
-import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.boot.web.servlet.ServletComponentScan;
-import org.springframework.cloud.openfeign.EnableFeignClients;
 import org.springframework.context.ConfigurableApplicationContext;
 
 @ServletComponentScan("com.zd.alg.iot.vmp.conf")
-@EnableFeignClients(basePackages = Constants.BASE_PACKAGE)
 @EnableCustomConfig
 @EnableRyFeignClients
 @SpringBootApplication(scanBasePackages = Constants.BASE_PACKAGE)
@@ -29,12 +26,12 @@ public class AlgorithmApplication {
     private static ConfigurableApplicationContext context;
     public static void main(String[] args) {
         AlgorithmApplication.args=args;
-        context = ZdStartApplication.run(ApplicationConstants.ALGORITHM_SERVICE, AlgorithmApplication.class, args);
+        AlgorithmApplication.context = ZdStartApplication.run(ApplicationConstants.ALGORITHM_SERVICE, AlgorithmApplication.class, args);
     }
 
     // 项目重启
     public static void restart() {
-        context.close();
+        AlgorithmApplication.context.close();
         AlgorithmApplication.context = SpringApplication.run(AlgorithmApplication.class, args);
     }
 }

+ 1 - 1
zd-modules/zd-algorithm/src/main/java/com/zd/alg/forward/controller/SignInCheckController.java

@@ -158,7 +158,7 @@ public class SignInCheckController {
      * 火焰算法验证
      * @param file 带验证图片
      */
-    @ApiOperation(value = "进入项验证:合并,codes格式:1,2,3")
+    @ApiOperation(value = "火焰算法验证")
     @PostMapping("/checkFire")
     public R checkFire(@ApiParam("图片文件") @RequestParam("file") MultipartFile file) {
         return R.ok(fireImageService.catchImage(file));

+ 14 - 15
zd-modules/zd-algorithm/src/main/java/com/zd/alg/forward/serivce/LoginService.java

@@ -1,6 +1,9 @@
 package com.zd.alg.forward.serivce;
 
+import com.alibaba.fastjson.JSON;
 import com.zd.alg.forward.config.AlgorithmYml;
+import com.zd.auth.feign.RemoteAuthService;
+import com.zd.auth.form.LoginBody;
 import com.zd.common.core.exception.ServiceException;
 import com.zd.common.core.security.TokenService;
 import com.zd.model.constant.SecurityConstants;
@@ -18,6 +21,7 @@ import org.springframework.util.Assert;
 import org.springframework.web.client.RestClientException;
 import org.springframework.web.client.RestTemplate;
 
+import javax.annotation.Resource;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -32,6 +36,9 @@ public class LoginService {
     @Autowired
     TokenService tokenService;
 
+    @Resource
+    private RemoteAuthService remoteAuthService;
+
     private String token;
 
     public String getToken() {
@@ -48,22 +55,14 @@ public class LoginService {
      */
     public void loginLab() {
         //不再重复登录
-        RestTemplate restTemplate = new RestTemplate();
-        //创建请求头
-        String url = algorithmYml.getLoginUri() + "auth/one/login";
-        Map<String, String> map = new HashMap<>(2);
-        map.put("username", "admin");
-        map.put("password", "zd123456..");
-        ParameterizedTypeReference<R<Map<String, Object>>> reference = new ParameterizedTypeReference<R<Map<String, Object>>>(){};
+        LoginBody body=new LoginBody();
+        body.setUsername("admin");
+        body.setPassword("zd123456..");
         try {
-            ResponseEntity<R<Map<String, Object>>> response = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(map), reference);
-            R<Map<String, Object>> parse = response.getBody();
-            if (parse != null) {
-                Assert.isTrue(parse.getCode() == R.SUCCESS, "登录失败:" + parse.getMsg());
-                //拿到token
-                Map<String, Object> data = parse.getData();
-                setToken("Bearer " + data.get("access_token"));
-            }
+            R<?> r = remoteAuthService.oneLogin(body);
+            Assert.isTrue(r.getCode() == R.SUCCESS, "登录失败:" + r.getMsg());
+            Map<String, Object> data = (Map<String, Object>) r.getData();
+            setToken("Bearer " + data.get("access_token"));
         } catch (RestClientException e) {
             throw new ServiceException(e.getMessage());
         }

+ 6 - 11
zd-modules/zd-algorithm/src/main/java/com/zd/alg/forward/serivce/SendSginAccessLogService.java

@@ -6,6 +6,7 @@ import com.zd.alg.forward.domain.VideoRequestData;
 import com.zd.alg.forward.properties.FireProperties;
 import com.zd.algorithm.api.speaker.entity.PlayVo;
 import com.zd.algorithm.api.speaker.feign.RemoteSpeakService;
+import com.zd.common.core.exception.PreAuthorizeException;
 import com.zd.common.core.utils.StringUtils;
 import com.zd.model.constant.HttpStatus;
 import com.zd.model.constant.SecurityConstants;
@@ -220,9 +221,7 @@ public class SendSginAccessLogService {
         ResponseEntity<R<T>> exchange = restTemplateLocal.exchange(url, method, requestEntity, reference);
         R<T> body = exchange.getBody();
         if (body != null && body.getCode() == HttpStatus.UNAUTHORIZED) {
-            requestEntity = getMapHttpEntityLogin(params);
-            exchange = restTemplateLocal.exchange(url, method, requestEntity, reference);
-
+            throw new PreAuthorizeException();
         }
         body = exchange.getBody();
         return body;
@@ -249,14 +248,10 @@ public class SendSginAccessLogService {
 
     private <T> HttpEntity<T> getMapHttpEntity(T params) {
         String token = loginLab.getToken();
-        HttpHeaders headers = new HttpHeaders();
-        headers.add(SecurityConstants.TOKEN_AUTHENTICATION, token);
-        return new HttpEntity<>(params, headers);
-    }
-
-    private <T> HttpEntity<T> getMapHttpEntityLogin(T params) {
-        loginLab.loginLab();
-        String token = loginLab.getToken();
+        if (token==null){
+            loginLab.loginLab();
+            token = loginLab.getToken();
+        }
         HttpHeaders headers = new HttpHeaders();
         headers.add(SecurityConstants.TOKEN_AUTHENTICATION, token);
         return new HttpEntity<>(params, headers);

+ 7 - 4
zd-modules/zd-algorithm/src/main/java/com/zd/alg/speaker/service/impl/SpeakerServiceImpl.java

@@ -6,6 +6,7 @@ import com.zd.alg.speaker.service.ISpeakerService;
 import com.zd.alg.speaker.utils.OkHttpRequest;
 import com.zd.algorithm.api.speaker.entity.PlayBatchVo;
 import com.zd.algorithm.api.speaker.entity.PlayVo;
+import com.zd.common.core.exception.ServiceException;
 import com.zd.common.core.redis.RedisService;
 import com.zd.common.core.utils.StringUtils;
 import com.zd.model.entity.UrlVo;
@@ -75,10 +76,12 @@ public class SpeakerServiceImpl implements ISpeakerService {
         jsonUser.put("user", jsonObject);
         String remoteUrl = remoteAddress.getProtocol() + "://" + remoteAddress.getRemoteIp() + ":" + remoteAddress.getRemotePort() + remoteAddress.getLoginUrl();
         String responseStr = okHttpRequest.doPostJson(remoteUrl, jsonUser.toString());
-        JSONObject responseJson = JSONObject.parseObject(responseStr);
-        JSONObject userJson = responseJson.getJSONObject("user");
-        String tokenStr = userJson.getString("token");
-        return tokenStr;
+        if (org.springframework.util.StringUtils.hasLength(responseStr)){
+            JSONObject responseJson = JSONObject.parseObject(responseStr);
+            JSONObject userJson = responseJson.getJSONObject("user");
+            return userJson.getString("token");
+        }
+        throw new ServiceException("喇叭服务登录异常");
     }
 
     @Override