org.shoulder.crypto.negotiation.cache.RedisNegotiationResultCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shoulder-crypto-negotiation Show documentation
Show all versions of shoulder-crypto-negotiation Show documentation
Shoulder 提供的 协商加密模块,用于非信任网络环境下的安全通信。基于 DH + ECC 实现先进的加密算法协商算法,比传统的 DH + DES 协商算法性能显著更高,更安全。
package org.shoulder.crypto.negotiation.cache;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import org.shoulder.core.util.JsonUtils;
import org.shoulder.crypto.negotiation.dto.NegotiationResult;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.concurrent.TimeUnit;
/**
* 密钥协商结果缓存
*
* @author lym
*/
public class RedisNegotiationResultCache implements NegotiationResultCache {
private static final String DEFAULT_CLIENT_KEY_PREFIX = "negotiation:asClient:";
private static final String DEFAULT_SERVER_KEY_PREFIX = "negotiation:asServer:";
/**
* 客户端存储安全会话信息缓存的key前缀
*/
private final String clientKeyPrefix;
/**
* 服务端存储安全会话信息缓存的key前缀
*/
private final String serverKeyPrefix;
private RedisTemplate redisTemplate;
public RedisNegotiationResultCache(RedisTemplate redisTemplate) {
this(redisTemplate, DEFAULT_CLIENT_KEY_PREFIX, DEFAULT_SERVER_KEY_PREFIX);
}
public RedisNegotiationResultCache(RedisTemplate redisTemplate,
String clientKeyPrefix, String serverKeyPrefix) {
this.redisTemplate = redisTemplate;
this.clientKeyPrefix = clientKeyPrefix;
this.serverKeyPrefix = serverKeyPrefix;
}
@Override
public void put(@Nonnull String cacheKey, @Nonnull NegotiationResult negotiationResult, boolean asClient) {
String key = buildCacheKey(cacheKey, asClient);
redisTemplate.opsForValue().set(
key, JsonUtils.toJson(negotiationResult),
negotiationResult.getExpireTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS
);
}
@Override
@Nullable
public NegotiationResult get(String cacheKey, boolean asClient) {
String key = buildCacheKey(cacheKey, asClient);
Object obj = redisTemplate.opsForValue().get(key);
if (obj == null) {
return null;
}
return JsonUtils.parseObject(String.valueOf(obj), NegotiationResult.class);
}
@Override
public void delete(String cacheKey, boolean asClient) {
String key = buildCacheKey(cacheKey, asClient);
redisTemplate.delete(key);
}
private String buildCacheKey(String cacheKey, boolean asClient) {
return (asClient ? clientKeyPrefix : serverKeyPrefix) + cacheKey;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy