Explorar el Código

获取token和数据传输代码开发

gao.qiang hace 2 años
padre
commit
0d681867f4

+ 135 - 0
base-common/src/main/java/com/ozs/common/utils/ApiTokenUtils.java

@@ -0,0 +1,135 @@
+package com.ozs.common.utils;
+
+import com.ozs.common.constant.CacheConstants;
+import com.ozs.common.constant.Constants;
+import com.ozs.common.core.domain.model.LoginUser;
+import com.ozs.common.core.redis.RedisCache;
+import com.ozs.common.utils.ip.AddressUtils;
+import com.ozs.common.utils.ip.IpUtils;
+import com.ozs.common.utils.uuid.IdUtils;
+import eu.bitwalker.useragentutils.UserAgent;
+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 ApiTokenUtils {
+
+    @Autowired
+    private RedisCache redisCache;
+
+    // 令牌自定义标识
+    @Value("${token.header}")
+    private String header;
+
+    // 令牌秘钥
+    @Value("${token.secret}")
+    private String secret;
+
+    // 令牌有效期(默认30分钟)
+    @Value("${token.expireTime}")
+    private int expireTime;
+
+    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.GEOHAZARDMONITOR_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() + expireTime * MILLIS_MINUTE));
+        redisCache.setCacheObject(tokenKey, token, expireTime, TimeUnit.MINUTES);
+        Map<String, Object> claims = new HashMap<>();
+        claims.put(Constants.GEOHAZARDMONITOR_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, secret).compact();
+        return token;
+    }
+
+    /**
+     * 从令牌中获取数据声明
+     *
+     * @param token 令牌
+     * @return 数据声明
+     */
+    private Claims parseToken(String token) {
+        return Jwts.parser()
+                .setSigningKey(secret)
+                .parseClaimsJws(token)
+                .getBody();
+    }
+
+
+    /**
+     * 获取请求token
+     *
+     * @param request
+     * @return token
+     */
+    private String getGeoToken(HttpServletRequest request) {
+        String token = request.getHeader(header);
+        if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX)) {
+            token = token.replace(Constants.TOKEN_PREFIX, "");
+        }
+        return token;
+    }
+
+    private String getGeoHazardMonitorTokenKey(String uuid) {
+        return CacheConstants.GEOHAZARDMONITOR_LOGIN_TOKEN_KEY + uuid;
+        
+    }
+}

+ 0 - 65
base-framework/src/main/java/com/ozs/framework/web/service/TokenService.java

@@ -78,33 +78,6 @@ public class TokenService
         return null;
     }
 
-    /**
-     * 地址灾害获取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.GEOHAZARDMONITOR_KEY);
-                String userKey = getGeoHazardMonitorTokenKey(uuid);
-                String tokens = redisCache.getCacheObject(userKey);
-                return tokens;
-            }
-            catch (Exception e)
-            {
-            }
-        }
-        return null;
-    }
-
     /**
      * 设置用户身份信息
      */
@@ -146,23 +119,6 @@ public class TokenService
         return createToken(claims);
     }
 
-    /**
-     * 地址灾害创建令牌
-     *
-     * @return
-     */
-    public ArrayList<String> createGeoHazardMonitorToken() {
-        ArrayList<String> parameter = new ArrayList<>();
-        String token = IdUtils.fastUUID();
-        String tokenKey = getGeoHazardMonitorTokenKey(token);
-        parameter.add(String.valueOf(System.currentTimeMillis() + expireTime * MILLIS_MINUTE));
-        redisCache.setCacheObject(tokenKey, token, expireTime, TimeUnit.MINUTES);
-        Map<String, Object> claims = new HashMap<>();
-        claims.put(Constants.GEOHAZARDMONITOR_KEY, token);
-        parameter.add(createToken(claims));
-        return parameter;
-    }
-
     /**
      * 验证令牌有效期,相差不足20分钟,自动刷新缓存
      *
@@ -264,29 +220,8 @@ public class TokenService
         return token;
     }
 
-    /**
-     * 获取请求token
-     *
-     * @param request
-     * @return token
-     */
-    private String getGeoToken(HttpServletRequest request)
-    {
-        String token = request.getHeader("token");
-        if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX))
-        {
-            token = token.replace(Constants.TOKEN_PREFIX, "");
-        }
-        return token;
-    }
-
     private String getTokenKey(String uuid)
     {
         return CacheConstants.LOGIN_TOKEN_KEY + uuid;
     }
-
-    private String getGeoHazardMonitorTokenKey(String uuid)
-    {   
-        return CacheConstants.GEOHAZARDMONITOR_LOGIN_TOKEN_KEY + uuid;
-    }
 }

+ 0 - 53
base-system/src/main/java/com/ozs/system/domain/vo/ReqMsgAlarmVo.java

@@ -1,53 +0,0 @@
-package com.ozs.system.domain.vo;
-
-import com.ozs.common.core.domain.entity.SysDept;
-import lombok.Data;
-
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * @author Administrator
- * 报警信息请求参数
- */
-@Data
-public class ReqMsgAlarmVo implements Serializable {
-
-    private static final long serialVersionUID = 1L;
-
-    /** 主键ID */
-    private Long alarmId;
-
-    /** 报警时间 */
-    private String alarmTime;
-
-    /** 相机编码 */
-    private Long alarmCamera;
-
-    /** 报警里程位置 */
-    private String alarmMile;
-
-    /** 报警内容 */
-    private String content;
-
-    /** 报警置信度(百分比) */
-    private String alarmConfidence;
-
-    /** 报警行别 */
-    private Integer lineDir;
-
-    /** 报警病害类型(目前只有1泥石流,后续可扩展使用) */
-    private Integer alarmType;
-
-    /** 报警病害属性(可以给出泥石流的框定范围) */
-    private String alarmArrr;
-
-    private String files;
-
-    /** 报警图片地址 */
-    private String imageUrl;
-
-    /** 报警是否解除 1已解除2未解除 */
-    private Integer isLock;
-}

+ 0 - 27
base-system/src/main/java/com/ozs/system/domain/vo/RespGeoHazardMonitorVo.java

@@ -1,27 +0,0 @@
-package com.ozs.system.domain.vo;
-
-import lombok.Data;
-
-import java.io.Serializable;
-
-/**
- * 地址灾害获取token返回参数
- * @author Administrator
- */
-@Data
-public class RespGeoHazardMonitorVo implements Serializable {
-
-    private static final long serialVersionUID = 1L;
-    /**
-     * 访问令牌
-     */
-    private String accessToken;
-    /**
-     * 过期时间,单位秒
-     */
-    private Long expiresIn;
-    /**
-     * 令牌类型
-     */
-    private String  tokenType;
-}

+ 0 - 27
base-system/src/main/java/com/ozs/system/domain/vo/RespMsgAlarmVo.java

@@ -1,27 +0,0 @@
-package com.ozs.system.domain.vo;
-
-import lombok.Data;
-
-import java.io.Serializable;
-
-/**
- * @author Administrator
- * 报警信息请求参数
- */
-@Data
-public class RespMsgAlarmVo implements Serializable {
-
-    private static final long serialVersionUID = 1L;
-
-    /** 主键ID */
-    private Long alarmId;
-
-    /** 报警解除时间 */
-    private String cancelTime;
-
-    /** 相机编码 */
-    private Long alarmCamera;
-    
-    /** 报警是否解除 1已解除2未解除 */
-    private Integer isCancel;
-}