Browse Source

1.对梦天的token验证进行优化,

training
pref_mail@163.com 1 year ago
parent
commit
038cc239a4
  1. 8
      blade-service/logpm-factory/src/main/java/com/logpm/factory/comfac/service/impl/FactoryAuthServiceImpl.java
  2. 26
      blade-service/logpm-factory/src/main/java/com/logpm/factory/comfac/service/impl/FactoryTokenServiceImpl.java

8
blade-service/logpm-factory/src/main/java/com/logpm/factory/comfac/service/impl/FactoryAuthServiceImpl.java

@ -13,6 +13,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springblade.common.exception.CustomerException; import org.springblade.common.exception.CustomerException;
import org.springblade.common.utils.TokenUtil; import org.springblade.common.utils.TokenUtil;
import org.springblade.core.redis.cache.BladeRedis;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -31,6 +32,8 @@ public class FactoryAuthServiceImpl implements IFactoryAuthService {
private final IFactoryAccountService factoryAccountService; private final IFactoryAccountService factoryAccountService;
private final IFactoryTokenService factoryTokenService; private final IFactoryTokenService factoryTokenService;
private final BladeRedis bladeRedis;
@Override @Override
public FactoryTokenVO getToken(String corpId, String appKey, String appSecret) throws NoSuchAlgorithmException { public FactoryTokenVO getToken(String corpId, String appKey, String appSecret) throws NoSuchAlgorithmException {
@ -71,6 +74,11 @@ public class FactoryAuthServiceImpl implements IFactoryAuthService {
factoryTokenVO.setToken(token); factoryTokenVO.setToken(token);
factoryTokenVO.setExpireTime(date); factoryTokenVO.setExpireTime(date);
factoryTokenVO.setExpireTimeLong(date.getTime()); factoryTokenVO.setExpireTimeLong(date.getTime());
// 增加redis缓存 疑似梦天请求太快 无法出现数据
bladeRedis.setEx("corpId:"+corpId, factoryToken, 60*60*24*30L);
return factoryTokenVO; return factoryTokenVO;
} }

26
blade-service/logpm-factory/src/main/java/com/logpm/factory/comfac/service/impl/FactoryTokenServiceImpl.java

@ -8,6 +8,7 @@ import lombok.AllArgsConstructor;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springblade.core.mp.base.BaseServiceImpl; import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.redis.cache.BladeRedis;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -20,6 +21,8 @@ public class FactoryTokenServiceImpl extends BaseServiceImpl<FactoryTokenMapper,
private static final Logger logger = LoggerFactory.getLogger(FactoryTokenServiceImpl.class); private static final Logger logger = LoggerFactory.getLogger(FactoryTokenServiceImpl.class);
private final BladeRedis bladeRedis;
@Override @Override
public boolean verifyToken(String token, String corpId) throws NoSuchAlgorithmException { public boolean verifyToken(String token, String corpId) throws NoSuchAlgorithmException {
logger.info("#########verifyToken: 验证token开始"); logger.info("#########verifyToken: 验证token开始");
@ -27,14 +30,27 @@ public class FactoryTokenServiceImpl extends BaseServiceImpl<FactoryTokenMapper,
// String tokenAbst = MD5Utils.md5Hex(token.getBytes(StandardCharsets.UTF_8)); // String tokenAbst = MD5Utils.md5Hex(token.getBytes(StandardCharsets.UTF_8));
//编写查询条件 //编写查询条件
QueryWrapper<FactoryToken> queryWrapper = new QueryWrapper<>(); String key ="corpId:"+corpId;
queryWrapper.eq("token",token) FactoryToken factoryToken = bladeRedis.get(key);
.eq("corpid",corpId);
FactoryToken factoryToken = baseMapper.selectOne(queryWrapper); if(Objects.isNull(factoryToken)){
QueryWrapper<FactoryToken> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("token",token);
factoryToken = baseMapper.selectOne(queryWrapper);
}
if(Objects.isNull(factoryToken)){ if(Objects.isNull(factoryToken)){
logger.error("#########verifyToken: token验证不通过 token={},corpid={}",token,corpId); logger.error("#########verifyToken: token1验证不通过 token={},corpid={}",token,corpId);
return false;
}
logger.info("#########verifyToken: 验证token 传入token {} 验证token {}",token,factoryToken.getToken());
if(!factoryToken.getToken().equals(token)){
logger.error("#########verifyToken: toke2n验证不通过 token={},corpid={}",token,corpId);
return false; return false;
} }
Long expireTimeLong = factoryToken.getExpireTime().getTime(); Long expireTimeLong = factoryToken.getExpireTime().getTime();
Long now = new Date().getTime(); Long now = new Date().getTime();
//判断是否过期 //判断是否过期

Loading…
Cancel
Save