Explorar el Código

国密依赖添加和appToken添加和模板方法添加

gao.qiang hace 2 años
padre
commit
430931ef7b

+ 11 - 0
base-common/pom.xml

@@ -190,6 +190,17 @@
             <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus-boot-starter</artifactId>
         </dependency>
+        <!-- BC容器对国密算法的实现 -->
+        <dependency>
+            <groupId>org.bouncycastle</groupId>
+            <artifactId>bcprov-jdk15on</artifactId>
+            <version>1.68</version>
+        </dependency>
+        <dependency>
+            <groupId>org.bouncycastle</groupId>
+            <artifactId>bcpkix-jdk15on</artifactId>
+            <version>1.68</version>
+        </dependency>
     </dependencies>
 
 </project>

+ 5 - 0
base-common/src/main/java/com/ozs/common/constant/CacheConstants.java

@@ -17,6 +17,11 @@ public class CacheConstants
      */
     public static final String GEOHAZARDMONITOR_LOGIN_TOKEN_KEY = "geoHazardMonitor_login_tokens:";
 
+    /**
+     * 地址灾害app redis key
+     */
+    public static final String APP_TOKEN_KEY = "app_tokens:";
+
     /**
      * 验证码 redis key
      */

+ 6 - 0
base-common/src/main/java/com/ozs/common/constant/Constants.java

@@ -89,6 +89,12 @@ public class Constants
      */  
     public static final String GEOHAZARDMONITOR_KEY = "GeoHazardMonitor_key";
 
+    /**
+     * 地址灾害令牌前缀
+     */
+    public static final String APP_KEY = "app_key";
+
+
     /**
      * 用户ID
      */

+ 132 - 0
base-common/src/main/java/com/ozs/common/utils/AppTokenUtils.java

@@ -0,0 +1,132 @@
+package com.ozs.common.utils;
+
+import com.ozs.common.constant.CacheConstants;
+import com.ozs.common.constant.Constants;
+import com.ozs.common.core.redis.RedisCache;
+import com.ozs.common.utils.uuid.IdUtils;
+import io.jsonwebtoken.Claims;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+
+@Component
+public class AppTokenUtils {
+
+    @Autowired
+    private RedisCache redisCache;
+    
+    // app令牌自定义标识
+    @Value("${apptoken.header}")
+    private String appHeader;
+
+    // app令牌秘钥
+    @Value("${apptoken.secret}")
+    private String appSecret;
+
+    // app令牌有效期(默认15天)
+    @Value("${apptoken.expireTime}")
+    private int appExpireTime;
+
+    protected static final long MILLIS_SECOND = 1000;
+
+    protected static final long MILLIS_MINUTE = 60 * MILLIS_SECOND;
+
+    private static final Long MILLIS_MINUTE_TEN = 20 * 60 * 1000L;
+
+
+    /**
+     * 地址灾害获取token信息
+     *
+     * @return 用户信息
+     */
+    public String getGeoHazardMonitorToken(HttpServletRequest request) {
+        // 获取请求携带的令牌
+        String token = getGeoToken(request);
+        if (StringUtils.isNotEmpty(token)) {
+            try {
+                Claims claims = parseToken(token);
+                // 解析对应的权限
+                String uuid = (String) claims.get(Constants.APP_KEY);
+                String userKey = getGeoHazardMonitorTokenKey(uuid);
+                String tokens = redisCache.getCacheObject(userKey);
+                return tokens;
+            } catch (Exception e) {
+            }
+        }
+        return null;
+    }
+
+
+    /**
+     * 地址灾害创建令牌
+     *
+     * @return
+     */
+    public ArrayList<String> createGeoHazardMonitorToken() {
+        ArrayList<String> parameter = new ArrayList<>();
+        String token = IdUtils.fastUUID();
+        String tokenKey = getGeoHazardMonitorTokenKey(token);
+        parameter.add(String.valueOf(System.currentTimeMillis() + appExpireTime * MILLIS_MINUTE));
+        redisCache.setCacheObject(tokenKey, token, appExpireTime, TimeUnit.MINUTES);
+        Map<String, Object> claims = new HashMap<>();
+        claims.put(Constants.APP_KEY, token);
+        parameter.add(createToken(claims));
+        return parameter;
+    }
+
+
+    /**
+     * 从数据声明生成令牌
+     *
+     * @param claims 数据声明
+     * @return 令牌
+     */
+    private String createToken(Map<String, Object> claims) {
+        String token = Jwts.builder()
+                .setClaims(claims)
+                .signWith(SignatureAlgorithm.HS512, appSecret).compact();
+        return token;
+    }
+
+    /**
+     * 从令牌中获取数据声明
+     *
+     * @param token 令牌
+     * @return 数据声明
+     */
+    private Claims parseToken(String token) {
+        return Jwts.parser()
+                .setSigningKey(appSecret)
+                .parseClaimsJws(token)
+                .getBody();
+    }
+
+
+    /**
+     * 获取请求token
+     *
+     * @param request
+     * @return token
+     */
+    private String getGeoToken(HttpServletRequest request) {
+        String token = request.getHeader(appHeader);
+        if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX)) {
+            token = token.replace(Constants.TOKEN_PREFIX, "");
+        }
+        return token;
+    }
+
+    private String getGeoHazardMonitorTokenKey(String uuid) {
+        return CacheConstants.APP_TOKEN_KEY + uuid;
+
+    }
+}

+ 20 - 0
base-common/src/main/java/com/ozs/common/utils/file/FileUtils.java

@@ -5,6 +5,7 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
@@ -18,6 +19,7 @@ import com.ozs.common.utils.DateUtils;
 import com.ozs.common.utils.StringUtils;
 import com.ozs.common.utils.uuid.IdUtils;
 import org.apache.commons.io.FilenameUtils;
+import org.springframework.util.ObjectUtils;
 
 /**
  * 文件处理工具类
@@ -288,4 +290,22 @@ public class FileUtils
         String baseName = FilenameUtils.getBaseName(fileName);
         return baseName;
     }
+
+    public static void writeBytesByInput(InputStream ins, OutputStream os) throws IOException {
+        try {
+            if (ObjectUtils.isEmpty(ins)) {
+                throw new FileNotFoundException();
+            }
+            byte[] b = new byte[1024];
+            int length;
+            while ((length = ins.read(b)) > 0) {
+                os.write(b, 0, length);
+            }
+        } catch (IOException e) {
+            throw e;
+        } finally {
+            IOUtils.close(os);
+            IOUtils.close(ins);
+        }
+    }
 }