|
@@ -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;
|
|
|
+
|
|
|
+ }
|
|
|
+}
|