徐勤民 2 месяцев назад
Родитель
Сommit
56e8394eec
18 измененных файлов с 304 добавлено и 23 удалено
  1. 8 1
      pom.xml
  2. 52 0
      src/main/java/com/xuqm/server/appmanager/common/JWTHelper.java
  3. 1 1
      src/main/java/com/xuqm/server/appmanager/common/SpringUtilsAuTo.java
  4. 1 1
      src/main/java/com/xuqm/server/appmanager/common/TimeHelper.java
  5. 32 15
      src/main/java/com/xuqm/server/appmanager/controller/sys/v1/TenantV1Controller.java
  6. 43 0
      src/main/java/com/xuqm/server/appmanager/controller/user/v1/UserV1Controller.java
  7. 13 0
      src/main/java/com/xuqm/server/appmanager/entitys/UserInfo.java
  8. 12 0
      src/main/java/com/xuqm/server/appmanager/entitys/UserLogin.java
  9. 4 2
      src/main/java/com/xuqm/server/appmanager/entitys/sys/v1/ApplicationEntity.java
  10. 24 0
      src/main/java/com/xuqm/server/appmanager/entitys/sys/v1/ReleaseEntity.java
  11. 2 0
      src/main/java/com/xuqm/server/appmanager/entitys/sys/v1/TenantUserEntity.java
  12. 24 0
      src/main/java/com/xuqm/server/appmanager/enums/AppStatus.java
  13. 23 0
      src/main/java/com/xuqm/server/appmanager/enums/AppType.java
  14. 19 0
      src/main/java/com/xuqm/server/appmanager/interceptor/InterceptorConfig.java
  15. 39 0
      src/main/java/com/xuqm/server/appmanager/interceptor/JWTInterceptor.java
  16. 4 1
      src/main/java/com/xuqm/server/appmanager/repository/sys/v1/ApplicationRepository.java
  17. 1 0
      src/main/java/com/xuqm/server/appmanager/repository/sys/v1/TenantUserRepository.java
  18. 2 2
      src/main/resources/application.properties

+ 8 - 1
pom.xml

@@ -51,6 +51,13 @@
             <artifactId>mysql-connector-java</artifactId>
             <version>8.0.24</version>
         </dependency>
+        <!--引入jwt-->
+        <dependency>
+            <groupId>com.auth0</groupId>
+            <artifactId>java-jwt</artifactId>
+            <version>4.4.0</version>
+        </dependency>
+
         <dependency>
             <groupId>com.google.code.gson</groupId>
             <artifactId>gson</artifactId>
@@ -63,7 +70,7 @@
         <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
-            <version>29.0-jre</version>
+            <version>32.0.0-android</version>
         </dependency>
 
         <dependency>

+ 52 - 0
src/main/java/com/xuqm/server/appmanager/common/JWTHelper.java

@@ -0,0 +1,52 @@
+package com.xuqm.server.appmanager.common;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTCreator;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.auth0.jwt.interfaces.DecodedJWT;
+import com.xuqm.server.appmanager.entitys.UserInfo;
+import com.xuqm.server.appmanager.entitys.sys.v1.TenantUserEntity;
+
+import java.util.Calendar;
+
+public class JWTHelper {
+    //定义自己的密钥
+    private static final String TOKEN = "token!DASD(#$dsad%$#.";
+
+    /**
+     * 生成token
+     *
+     * @param user 传入的有效负荷
+     * @return
+     */
+    public static String genToken(TenantUserEntity user) {
+        JWTCreator.Builder builder = JWT.create();
+        builder.withClaim("userId", user.getUserId());
+        builder.withClaim("tenantNo", user.getTenantNo());
+        Calendar instance = Calendar.getInstance();
+        //定义过期时间
+        instance.add(Calendar.DATE, 7);
+        builder.withExpiresAt(instance.getTime());
+        return builder.sign(Algorithm.HMAC256(TOKEN));
+    }
+
+    /**
+     * 验证获取token中的有效负载,验证失败返回null
+     *
+     * @param token
+     * @return
+     */
+    public static DecodedJWT verify(String token) {
+        return JWT.require(Algorithm.HMAC256(TOKEN)).build().verify(token);
+    }
+
+    public static UserInfo getUser(String token) {
+        DecodedJWT jwt = null;
+        try {
+            jwt = JWTHelper.verify(token);
+        } catch (Exception ignored) {
+            return null;
+        }
+        return new UserInfo(jwt.getClaim("userId").asString(), jwt.getClaim("tenantNo").asString());
+    }
+}

+ 1 - 1
src/main/java/com/xuqm/server/appmanager/common/SpringUtilsAuTo.java

@@ -1,4 +1,4 @@
-package cn.org.bjca.trust.java.imserver.common;
+package com.xuqm.server.appmanager.common;
 
 import org.springframework.aop.framework.AopContext;
 import org.springframework.beans.BeansException;

+ 1 - 1
src/main/java/com/xuqm/server/appmanager/common/TimeHelper.java

@@ -1,4 +1,4 @@
-package cn.org.bjca.trust.java.imserver.common;
+package com.xuqm.server.appmanager.common;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;

+ 32 - 15
src/main/java/com/xuqm/server/appmanager/controller/sys/v1/TenantV1Controller.java

@@ -1,6 +1,8 @@
 package com.xuqm.server.appmanager.controller.sys.v1;
 
-import cn.org.bjca.trust.java.imserver.common.TimeHelper;
+import com.xuqm.server.appmanager.common.JWTHelper;
+import com.xuqm.server.appmanager.common.TimeHelper;
+import com.xuqm.server.appmanager.entitys.UserInfo;
 import com.xuqm.server.appmanager.entitys.sys.v1.ApplicationEntity;
 import com.xuqm.server.appmanager.entitys.sys.v1.TenantEntity;
 import com.xuqm.server.appmanager.entitys.sys.v1.TenantUserEntity;
@@ -9,11 +11,9 @@ import com.xuqm.server.appmanager.repository.sys.v1.ApplicationRepository;
 import com.xuqm.server.appmanager.repository.sys.v1.TenantRepository;
 import com.xuqm.server.appmanager.repository.sys.v1.TenantUserRepository;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
+import java.util.List;
 import java.util.Random;
 
 @RestController
@@ -29,9 +29,7 @@ public class TenantV1Controller {
 
     @PostMapping("/tenant/create")
     public HttpResult<String> tenantCreate(@RequestBody TenantUserEntity tenant) throws Exception {
-        if (null == tenant.getUserPhone() ||
-                null == tenant.getUserEmail() ||
-                null == tenant.getUserPwd()) {
+        if (null == tenant.getUserPhone() || null == tenant.getUserEmail() || null == tenant.getUserPwd()) {
             return new HttpResult<>(201, "参数错误", null);
         } else {
             TenantUserEntity u = tenantUserRepository.findFirstByUserEmailOrUserPhone(tenant.getUserEmail(), tenant.getUserPhone());
@@ -53,23 +51,42 @@ public class TenantV1Controller {
 
     }
 
-    @PostMapping("/app/create")
-    public HttpResult<String> appCreate(@RequestBody ApplicationEntity application) throws Exception {
-        if (null == application.getAppName() ||
-                null == application.getTenantNo()) {
+    @PostMapping("/manager/create")
+    public HttpResult<String> appCreate(@RequestBody ApplicationEntity application, @RequestHeader(name = "token") String token) throws Exception {
+        if (null == application.getAppName() || null == application.getPackageName()) {
             return new HttpResult<>(201, "参数错误", null);
         } else {
-            ApplicationEntity app = applicationRepository.findFirstByAppNameAndTenantNo(application.getAppName(), application.getTenantNo());
+            UserInfo userInfo = JWTHelper.getUser(token);
+            if (null == userInfo) {
+                return new HttpResult<>(401, "登录失效", null);
+            }
+            ApplicationEntity app = applicationRepository.findFirstByAppNameAndTenantNo(application.getAppName(), userInfo.getTenantNo());
             if (null != app) return new HttpResult<>(201, "已创建相关类型的同名应用", null);
             app = new ApplicationEntity();
-            app.setTenantNo(application.getTenantNo());
+            app.setTenantNo(userInfo.getTenantNo());
             app.setAppName(application.getAppName());
+            app.setPackageName(application.getPackageName());
+            app.setDownloadUrl(application.getDownloadUrl());
             app.setAppId(TimeHelper.getTimeString("yyyyMMddHHmm") + (new Random().nextInt(899999999) + 100000000));
             applicationRepository.save(app);
 
-            return new HttpResult<>(200, "创建成功", "创建成功");
+            return new HttpResult<>(200, "创建成功", app.getAppId());
+        }
+
+    }
+
+    @PostMapping("/manager/list")
+    public HttpResult<List<ApplicationEntity>> appList(@RequestBody ApplicationEntity application, @RequestHeader(name = "token") String token) throws Exception {
+
+        UserInfo userInfo = JWTHelper.getUser(token);
+        if (null == userInfo) {
+            return new HttpResult<>(401, "登录失效", null);
         }
 
+
+        List<ApplicationEntity> apps = applicationRepository.findAllByTenantNo(userInfo.getTenantNo());
+
+        return new HttpResult<>(200, "成功", apps);
     }
 
 }

+ 43 - 0
src/main/java/com/xuqm/server/appmanager/controller/user/v1/UserV1Controller.java

@@ -0,0 +1,43 @@
+package com.xuqm.server.appmanager.controller.user.v1;
+
+import com.xuqm.server.appmanager.common.JWTHelper;
+import com.xuqm.server.appmanager.entitys.UserLogin;
+import com.xuqm.server.appmanager.entitys.sys.v1.TenantUserEntity;
+import com.xuqm.server.appmanager.http.HttpResult;
+import com.xuqm.server.appmanager.repository.sys.v1.ApplicationRepository;
+import com.xuqm.server.appmanager.repository.sys.v1.TenantRepository;
+import com.xuqm.server.appmanager.repository.sys.v1.TenantUserRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("user/v1")
+public class UserV1Controller {
+
+    @Autowired
+    private TenantRepository tenantRepository;
+    @Autowired
+    private TenantUserRepository tenantUserRepository;
+    @Autowired
+    private ApplicationRepository applicationRepository;
+
+    @PostMapping("/login")
+    public HttpResult<String> userLogin(@RequestBody UserLogin user) throws Exception {
+        if (null == user.getEmail() ||
+                null == user.getPassword()) {
+            return new HttpResult<>(201, "参数错误", null);
+        } else {
+            TenantUserEntity u = tenantUserRepository.findFirstByUserEmailAndUserPwd(user.getEmail(), user.getPassword());
+
+            if (u == null) {
+                return new HttpResult<>(501, "用户名或密码错误", null);
+            }
+
+            return new HttpResult<>(200, "登录成功", JWTHelper.genToken(u));
+        }
+
+    }
+}

+ 13 - 0
src/main/java/com/xuqm/server/appmanager/entitys/UserInfo.java

@@ -0,0 +1,13 @@
+package com.xuqm.server.appmanager.entitys;
+
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+
+@Data
+@AllArgsConstructor
+public class UserInfo {
+    private String userId;
+    private String tenantNo;
+}

+ 12 - 0
src/main/java/com/xuqm/server/appmanager/entitys/UserLogin.java

@@ -0,0 +1,12 @@
+package com.xuqm.server.appmanager.entitys;
+
+
+import jakarta.persistence.Entity;
+import lombok.Data;
+
+
+@Data
+public class UserLogin {
+    private String email;
+    private String password;
+}

+ 4 - 2
src/main/java/com/xuqm/server/appmanager/entitys/sys/v1/ApplicationEntity.java

@@ -1,16 +1,18 @@
 package com.xuqm.server.appmanager.entitys.sys.v1;
 
 import com.xuqm.server.appmanager.entitys.converter.AbstractBaseTimeEntity;
+import com.xuqm.server.appmanager.enums.AppType;
 import jakarta.persistence.Entity;
 import jakarta.persistence.Table;
 import lombok.Data;
 
 @Entity
-@Table(name = "applications")
+@Table(name = "application_release")
 @Data
 public class ApplicationEntity extends AbstractBaseTimeEntity {
     private String appId;
     private String appName;
     private String tenantNo;
-    private String userId;
+    private String downloadUrl;
+    private String packageName;
 }

+ 24 - 0
src/main/java/com/xuqm/server/appmanager/entitys/sys/v1/ReleaseEntity.java

@@ -0,0 +1,24 @@
+package com.xuqm.server.appmanager.entitys.sys.v1;
+
+import com.xuqm.server.appmanager.entitys.converter.AbstractBaseTimeEntity;
+import com.xuqm.server.appmanager.enums.AppStatus;
+import jakarta.persistence.Entity;
+import jakarta.persistence.Table;
+import lombok.Data;
+
+@Entity
+@Table(name = "release")
+@Data
+public class ReleaseEntity extends AbstractBaseTimeEntity {
+    private String appId;
+    private AppStatus huawei;
+    private AppStatus xiaomi;
+    private AppStatus meizu;
+    private AppStatus rongyao;
+    private AppStatus oppo;
+    private AppStatus vivo;
+    private AppStatus google;
+    private String url;
+    private String versionCode;
+    private String versionName;
+}

+ 2 - 0
src/main/java/com/xuqm/server/appmanager/entitys/sys/v1/TenantUserEntity.java

@@ -1,5 +1,6 @@
 package com.xuqm.server.appmanager.entitys.sys.v1;
 
+import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.xuqm.server.appmanager.entitys.converter.AbstractBaseTimeEntity;
 import jakarta.persistence.Entity;
 import jakarta.persistence.Table;
@@ -13,6 +14,7 @@ public class TenantUserEntity extends AbstractBaseTimeEntity {
     private String userName;
     private String userPhone;
     private String userEmail;
+    @JsonIgnore
     private String userPwd;
     private String tenantNo;
 }

+ 24 - 0
src/main/java/com/xuqm/server/appmanager/enums/AppStatus.java

@@ -0,0 +1,24 @@
+package com.xuqm.server.appmanager.enums;
+
+public enum AppStatus {
+    // 不需要发布、没有上传安装包、已上传待审核、审核完成待发布、已发布
+    NOT(0),ABSENT(1), AUDIT(2), WAIT(3), RELEASE(4) ;
+    private int status = 0;
+
+    AppStatus(final int status) {
+        this.status = status;
+    }
+
+    public int status() {
+        return this.status;
+    }
+
+    public static AppStatus getStatus(final int status) {
+        for (final AppStatus value : AppStatus.values()) {
+            if (value.status == status) {
+                return value;
+            }
+        }
+        return NOT;
+    }
+}

+ 23 - 0
src/main/java/com/xuqm/server/appmanager/enums/AppType.java

@@ -0,0 +1,23 @@
+package com.xuqm.server.appmanager.enums;
+
+public enum AppType {
+    UNKNOWN(0),IM(1), APP(2) ;
+    private int type = -1;
+
+    AppType(final int type) {
+        this.type = type;
+    }
+
+    public int type() {
+        return this.type;
+    }
+
+    public static AppType getType(final int type) {
+        for (final AppType value : AppType.values()) {
+            if (value.type == type) {
+                return value;
+            }
+        }
+        return UNKNOWN;
+    }
+}

+ 19 - 0
src/main/java/com/xuqm/server/appmanager/interceptor/InterceptorConfig.java

@@ -0,0 +1,19 @@
+package com.xuqm.server.appmanager.interceptor;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+@Configuration
+public class InterceptorConfig implements WebMvcConfigurer {
+
+    @Override
+    public void addInterceptors(InterceptorRegistry registry) {
+        registry.addInterceptor(new JWTInterceptor())
+                //其他接口token验证
+                .addPathPatterns("/**")
+                //所有用户都放心
+                .excludePathPatterns("/tenant/create","/user/v1/login","/hello/**");
+    }
+}
+

+ 39 - 0
src/main/java/com/xuqm/server/appmanager/interceptor/JWTInterceptor.java

@@ -0,0 +1,39 @@
+package com.xuqm.server.appmanager.interceptor;
+
+import com.auth0.jwt.exceptions.AlgorithmMismatchException;
+import com.auth0.jwt.exceptions.SignatureVerificationException;
+import com.auth0.jwt.exceptions.TokenExpiredException;
+import com.xuqm.server.appmanager.common.JWTHelper;
+import com.xuqm.server.appmanager.common.json.GsonImplHelp;
+import com.xuqm.server.appmanager.http.HttpResult;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import org.springframework.web.servlet.HandlerInterceptor;
+
+public class JWTInterceptor implements HandlerInterceptor {
+
+    @Override
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+        String msg = "";
+        //获取请求头中令牌
+        String token = request.getHeader("token");
+        try {
+            JWTHelper.verify(token);//验证令牌
+            return true;//放行请求
+        } catch (SignatureVerificationException e) {
+            msg = "无效签名!";
+        } catch (TokenExpiredException e) {
+            msg = "token过期!";
+        } catch (AlgorithmMismatchException e) {
+            msg = "token算法不一致!";
+        } catch (Exception e) {
+            msg = "token无效!!";
+        }
+        HttpResult<String> result = new HttpResult<>(401, msg, null);
+        //将map 专为json  jackson
+        response.setContentType("application/json;charset=UTF-8");
+        response.getWriter().println(GsonImplHelp.get().toJson(result));
+        return false;
+    }
+}
+

+ 4 - 1
src/main/java/com/xuqm/server/appmanager/repository/sys/v1/ApplicationRepository.java

@@ -1,12 +1,15 @@
 package com.xuqm.server.appmanager.repository.sys.v1;
 
 import com.xuqm.server.appmanager.entitys.sys.v1.ApplicationEntity;
+import com.xuqm.server.appmanager.enums.AppType;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.stereotype.Repository;
 
+import java.util.List;
+
 @Repository
 public interface ApplicationRepository extends JpaRepository<ApplicationEntity, Long> {
     ApplicationEntity findFirstByAppNameAndTenantNo(String appName, String tenantNo);
-    ApplicationEntity findFirstByAppId(String appId);
+    List<ApplicationEntity> findAllByTenantNo(String tenantNo);
     boolean existsByAppId(String appId);
 }

+ 1 - 0
src/main/java/com/xuqm/server/appmanager/repository/sys/v1/TenantUserRepository.java

@@ -8,5 +8,6 @@ import org.springframework.stereotype.Repository;
 @Repository
 public interface TenantUserRepository extends JpaRepository<TenantUserEntity, Long> {
     TenantUserEntity findFirstByUserEmailOrUserPhone(String userEmail, String userPhone);
+    TenantUserEntity findFirstByUserEmailAndUserPwd(String userEmail, String userPwd);
 
 }

+ 2 - 2
src/main/resources/application.properties

@@ -4,11 +4,11 @@ server.port=4562
 #????????????
 spring.jpa.hibernate.ddl-auto=update
 #mysql?????????????
-spring.datasource.url = jdbc:mysql://114.115.203.60:3306/app-manager?serverTimezone=Asia/Shanghai
+spring.datasource.url = jdbc:mysql://39.107.53.187:3306/app-manager?serverTimezone=Asia/Shanghai
 #??????
 spring.datasource.username = app-manager
 #?????
-spring.datasource.password = hjCcSd43pkxbdCHX
+spring.datasource.password = x2XSX47tBtiAhxCz
 #mysql???????????
 spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
 #jpa?????????Hibernate?sql(??)