| | |
| | | import cn.hutool.crypto.asymmetric.RSA; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.se.simu.constant.CacheConstants; |
| | | import com.se.simu.constant.RedisCache; |
| | | import com.se.simu.domain.EntityTypeInfo; |
| | | import com.se.simu.domain.LoginParams; |
| | | import com.se.simu.helper.CaffeineHelper; |
| | | import com.se.simu.service.ProjectRelatedService; |
| | | import com.se.simu.utils.CustomWebClient; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | import reactor.core.publisher.Mono; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.security.KeyPair; |
| | | import java.security.PrivateKey; |
| | | import java.security.PublicKey; |
| | |
| | | import java.util.Objects; |
| | | import java.util.concurrent.CompletableFuture; |
| | | import java.util.concurrent.ExecutionException; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.concurrent.atomic.AtomicReference; |
| | | |
| | | @Slf4j |
| | | @Service |
| | | public class ProjectRelatedServiceImpl implements ProjectRelatedService { |
| | | |
| | | @Resource |
| | | private RedisCache redisCache; |
| | | |
| | | @Resource |
| | | public RedisTemplate redisTemplate; |
| | | |
| | | |
| | | // 公钥地址 |
| | |
| | | log.info("statusCode = " + statusCode); |
| | | String data = postResponseJson.getString("data"); |
| | | // 缓存data,并设置1小时有效期 |
| | | redisCache.deleteObject(CacheConstants.USER_CACHE_KEY + "rsa_data_set"); |
| | | redisCache.setCacheObject(CacheConstants.USER_CACHE_KEY + "rsa_data_set", data, 1, TimeUnit.HOURS); |
| | | CaffeineHelper.remove(CacheConstants.USER_CACHE_KEY + "rsa_data_set"); |
| | | CaffeineHelper.put(CacheConstants.USER_CACHE_KEY + "rsa_data_set", data); |
| | | log.info("publicKey = " + data); |
| | | } catch (Exception e) { |
| | | log.info("Failed to parse JSON: " + e.getMessage()); |
| | |
| | | @Override |
| | | public Object loginEntity(LoginParams loginParams) { |
| | | // 判断redis中缓存是否存在(过期) |
| | | Boolean isExists = redisCache.hasKey(CacheConstants.USER_CACHE_KEY + "entity_db_response"); |
| | | boolean isExists = Objects.nonNull(CaffeineHelper.getMd5(CacheConstants.USER_CACHE_KEY + "entity_db_response")); |
| | | if (isExists) { |
| | | return JSONObject.parseObject(redisCache.getCacheObject(CacheConstants.USER_CACHE_KEY + "entity_db_response")); |
| | | return JSONObject.parseObject((String) CaffeineHelper.get(CacheConstants.USER_CACHE_KEY + "entity_db_response")); |
| | | } |
| | | // 设置请求体 |
| | | // 获取私钥和公钥,长度必须是16、24或32 |
| | | String publicKey = redisCache.getCacheObject(CacheConstants.USER_CACHE_KEY + "rsa_data_set"); |
| | | String publicKey = (String) CaffeineHelper.get(CacheConstants.USER_CACHE_KEY + "rsa_data_set"); |
| | | // 假设从Redis中获取到用户名,判断 |
| | | if (!StringUtils.isNotBlank(publicKey)) { |
| | | // 缓存中没有用户名,则进行登录 |
| | |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | publicKey = redisCache.getCacheObject(CacheConstants.USER_CACHE_KEY + "rsa_data_set"); |
| | | publicKey = (String) CaffeineHelper.get(CacheConstants.USER_CACHE_KEY + "rsa_data_set"); |
| | | log.info("publicKey = " + publicKey); |
| | | // 公钥加密 |
| | | AsymmetricCrypto rsa = SecureUtil.rsa(null, publicKey); |
| | |
| | | String data = JSONObject.parseObject(response).getString("data"); |
| | | String token = JSONObject.parseObject(data).getString("token"); |
| | | // 设置data到Redis中 |
| | | redisCache.deleteObject(CacheConstants.USER_CACHE_KEY + "entity_db_response"); |
| | | redisCache.setCacheObject(CacheConstants.USER_CACHE_KEY + "entity_db_response", response, 1, TimeUnit.HOURS); |
| | | CaffeineHelper.remove(CacheConstants.USER_CACHE_KEY + "entity_db_response"); |
| | | CaffeineHelper.put(CacheConstants.USER_CACHE_KEY + "entity_db_response", response); |
| | | // 设置data到Redis中 |
| | | redisCache.deleteObject(CacheConstants.USER_CACHE_KEY + "entity_db_token"); |
| | | redisCache.setCacheObject(CacheConstants.USER_CACHE_KEY + "entity_db_token", token, 1, TimeUnit.HOURS); |
| | | CaffeineHelper.remove(CacheConstants.USER_CACHE_KEY + "entity_db_token"); |
| | | CaffeineHelper.put(CacheConstants.USER_CACHE_KEY + "entity_db_token", token); |
| | | } else { |
| | | // 登录失败 |
| | | log.error("登录失败 $= " + response); |
| | |
| | | */ |
| | | @Override |
| | | public Object getEntityPublicKey() { |
| | | // 判断redis中缓存是否存在(过期) |
| | | Boolean isExists = redisCache.hasKey(CacheConstants.USER_CACHE_KEY + "EntityPublicKey"); |
| | | // 判断中缓存是否存在(过期) |
| | | boolean isExists = Objects.nonNull(CaffeineHelper.getMd5(CacheConstants.USER_CACHE_KEY + "EntityPublicKey")); |
| | | if (isExists) { |
| | | return JSONObject.parseObject(redisCache.getCacheObject(CacheConstants.USER_CACHE_KEY + "EntityPublicKey")); |
| | | return JSONObject.parseObject((String) CaffeineHelper.get(CacheConstants.USER_CACHE_KEY + "EntityPublicKey")); |
| | | } |
| | | HashMap<String, String> headers = new HashMap<>(); |
| | | headers.put("Content-Type", "application/json"); |
| | |
| | | log.info("statusCode = " + statusCode); |
| | | String data = postResponseJson.getString("data"); |
| | | // 缓存data,并设置1小时有效期 |
| | | redisCache.deleteObject(CacheConstants.USER_CACHE_KEY + "EntityPublicKey"); |
| | | redisCache.setCacheObject(CacheConstants.USER_CACHE_KEY + "EntityPublicKey", response, 1, TimeUnit.HOURS); |
| | | CaffeineHelper.remove(CacheConstants.USER_CACHE_KEY + "EntityPublicKey"); |
| | | CaffeineHelper.put(CacheConstants.USER_CACHE_KEY + "EntityPublicKey", response); |
| | | log.info("EntityPublicKey = " + data); |
| | | } catch (Exception e) { |
| | | log.info("Failed to parse JSON: " + e.getMessage()); |
| | |
| | | public Object getDbLits() { |
| | | HashMap<String, String> headers = new HashMap<>(); |
| | | headers.put("Content-Type", "application/json"); |
| | | String token = redisCache.getCacheObject(CacheConstants.USER_CACHE_KEY + "entity_db_token"); |
| | | String token = (String) CaffeineHelper.get(CacheConstants.USER_CACHE_KEY + "entity_db_token"); |
| | | log.info("token = " + token); |
| | | // 添加form参数 |
| | | HashMap<String, Object> params = new HashMap<>(); |
| | |
| | | log.info("statusCode = " + statusCode); |
| | | String data = postResponseJson.getString("data"); |
| | | // 缓存data,并设置1小时有效期 |
| | | redisCache.deleteObject(CacheConstants.USER_CACHE_KEY + "EntityDbNameList"); |
| | | redisCache.setCacheObject(CacheConstants.USER_CACHE_KEY + "EntityDbNameList", response, 1, TimeUnit.HOURS); |
| | | CaffeineHelper.remove(CacheConstants.USER_CACHE_KEY + "EntityDbNameList"); |
| | | CaffeineHelper.put(CacheConstants.USER_CACHE_KEY + "EntityDbNameList", response); |
| | | log.info("EntityDbNameList = " + data); |
| | | } catch (Exception e) { |
| | | log.info("Failed to parse JSON: " + e.getMessage()); |
| | |
| | | public Object getEntityTypeInfo(EntityTypeInfo entityTypeInfo) { |
| | | HashMap<String, String> headers = new HashMap<>(); |
| | | headers.put("Content-Type", "application/json"); |
| | | String token = redisCache.getCacheObject(CacheConstants.USER_CACHE_KEY + "entity_db_token"); |
| | | String token = (String) CaffeineHelper.get(CacheConstants.USER_CACHE_KEY + "entity_db_token"); |
| | | log.info("token = " + token); |
| | | // 添加form参数 |
| | | HashMap<String, Object> params = new HashMap<>(); |