Search in sources :

Example 31 with RedisConnection

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();
    }
}
Also used : ExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken) OAuth2RefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken) Date(java.util.Date) RedisConnection(org.springframework.data.redis.connection.RedisConnection) ExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)

Example 32 with RedisConnection

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);
}
Also used : RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 33 with RedisConnection

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());
}
Also used : RedisConnection(org.springframework.data.redis.connection.RedisConnection) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 34 with RedisConnection

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());
    }
}
Also used : RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 35 with RedisConnection

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;
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) RedisCallback(org.springframework.data.redis.core.RedisCallback) DataAccessException(org.springframework.dao.DataAccessException) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Aggregations

RedisConnection (org.springframework.data.redis.connection.RedisConnection)41 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)9 RedisConnectionFailureException (org.springframework.data.redis.RedisConnectionFailureException)8 Properties (java.util.Properties)7 DataAccessException (org.springframework.dao.DataAccessException)7 RedisCallback (org.springframework.data.redis.core.RedisCallback)7 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)7 Test (org.junit.Test)5 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)4 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)4 ExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)3 com.alicp.jetcache (com.alicp.jetcache)2 AbstractExternalCache (com.alicp.jetcache.external.AbstractExternalCache)2 java.util (java.util)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Function (java.util.function.Function)2 Logger (org.slf4j.Logger)2