use of org.springframework.data.redis.connection.RedisConnection in project spring-security-oauth by spring-projects.
the class RedisTokenStore method storeAccessToken.
@Override
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
byte[] serializedAccessToken = serialize(token);
byte[] serializedAuth = serialize(authentication);
byte[] accessKey = serializeKey(ACCESS + token.getValue());
byte[] authKey = serializeKey(AUTH + token.getValue());
byte[] authToAccessKey = serializeKey(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication));
byte[] approvalKey = serializeKey(UNAME_TO_ACCESS + getApprovalKey(authentication));
byte[] clientId = serializeKey(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId());
RedisConnection conn = getConnection();
try {
conn.openPipeline();
if (springDataRedis_2_0) {
try {
this.redisConnectionSet_2_0.invoke(conn, accessKey, serializedAccessToken);
this.redisConnectionSet_2_0.invoke(conn, authKey, serializedAuth);
this.redisConnectionSet_2_0.invoke(conn, authToAccessKey, serializedAccessToken);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} else {
conn.set(accessKey, serializedAccessToken);
conn.set(authKey, serializedAuth);
conn.set(authToAccessKey, serializedAccessToken);
}
if (!authentication.isClientOnly()) {
conn.rPush(approvalKey, serializedAccessToken);
}
conn.rPush(clientId, serializedAccessToken);
if (token.getExpiration() != null) {
int seconds = token.getExpiresIn();
conn.expire(accessKey, seconds);
conn.expire(authKey, seconds);
conn.expire(authToAccessKey, seconds);
conn.expire(clientId, seconds);
conn.expire(approvalKey, seconds);
}
OAuth2RefreshToken refreshToken = token.getRefreshToken();
if (refreshToken != null && refreshToken.getValue() != null) {
byte[] refresh = serialize(token.getRefreshToken().getValue());
byte[] auth = serialize(token.getValue());
byte[] refreshToAccessKey = serializeKey(REFRESH_TO_ACCESS + token.getRefreshToken().getValue());
byte[] accessToRefreshKey = serializeKey(ACCESS_TO_REFRESH + token.getValue());
if (springDataRedis_2_0) {
try {
this.redisConnectionSet_2_0.invoke(conn, refreshToAccessKey, auth);
this.redisConnectionSet_2_0.invoke(conn, accessToRefreshKey, refresh);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} else {
conn.set(refreshToAccessKey, auth);
conn.set(accessToRefreshKey, refresh);
}
if (refreshToken instanceof ExpiringOAuth2RefreshToken) {
ExpiringOAuth2RefreshToken expiringRefreshToken = (ExpiringOAuth2RefreshToken) refreshToken;
Date expiration = expiringRefreshToken.getExpiration();
if (expiration != null) {
int seconds = Long.valueOf((expiration.getTime() - System.currentTimeMillis()) / 1000L).intValue();
conn.expire(refreshToAccessKey, seconds);
conn.expire(accessToRefreshKey, seconds);
}
}
}
conn.closePipeline();
} finally {
conn.close();
}
}
use of org.springframework.data.redis.connection.RedisConnection in project spring-integration by spring-projects.
the class RedisAvailableTests method awaitContainerSubscribedWithPatterns.
protected void awaitContainerSubscribedWithPatterns(RedisMessageListenerContainer container) throws Exception {
this.awaitContainerSubscribed(container);
RedisConnection connection = TestUtils.getPropertyValue(container, "subscriptionTask.connection", RedisConnection.class);
int n = 0;
while (n++ < 300 && connection.getSubscription().getPatterns().size() == 0) {
Thread.sleep(100);
}
assertTrue("RedisMessageListenerContainer Failed to Subscribe with patterns", n < 300);
// wait another second because of race condition
Thread.sleep(1000);
}
use of org.springframework.data.redis.connection.RedisConnection in project spring-integration by spring-projects.
the class RedisOutboundGatewayTests method testMGetCommand.
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testMGetCommand() {
RedisConnection connection = this.getConnectionFactoryForTest().getConnection();
byte[] value1 = "bar1".getBytes();
byte[] value2 = "bar2".getBytes();
connection.set("foo1".getBytes(), value1);
connection.set("foo2".getBytes(), value2);
this.mgetCommandChannel.send(MessageBuilder.withPayload(new String[] { "foo1", "foo2" }).build());
Message<?> receive = this.replyChannel.receive(1000);
assertNotNull(receive);
assertThat((List<byte[]>) receive.getPayload(), Matchers.contains(value1, value2));
connection.del("foo1".getBytes(), "foo2".getBytes());
}
use of org.springframework.data.redis.connection.RedisConnection in project spring-integration by spring-projects.
the class RedisStoreWritingMessageHandler method processInPipeline.
private void processInPipeline(PipelineCallback callback) {
RedisConnection connection = RedisConnectionUtils.bindConnection(this.redisTemplate.getConnectionFactory());
try {
connection.openPipeline();
callback.process();
} finally {
connection.closePipeline();
RedisConnectionUtils.unbindConnection(this.redisTemplate.getConnectionFactory());
}
}
use of org.springframework.data.redis.connection.RedisConnection in project Spring-Family by Sierou-Java.
the class RedisLock method expire.
private boolean expire(final String lockKey, final Long expireTime) {
Object obj = null;
try {
obj = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
StringRedisSerializer serializer = new StringRedisSerializer();
boolean bool = connection.expire(serializer.serialize(lockKey), expireTime);
connection.close();
return bool;
}
});
return (Boolean) obj;
} catch (Exception e) {
log.error("expire redis error, key : {}", lockKey);
}
return false;
}
Aggregations