Search in sources :

Example 6 with RedisConnection

use of org.springframework.data.redis.connection.RedisConnection in project spring-security-oauth by spring-projects.

the class RedisTokenStore method findTokensByClientId.

@Override
public Collection<OAuth2AccessToken> findTokensByClientId(String clientId) {
    byte[] key = serializeKey(CLIENT_ID_TO_ACCESS + clientId);
    List<byte[]> byteList = null;
    RedisConnection conn = getConnection();
    try {
        byteList = conn.lRange(key, 0, -1);
    } finally {
        conn.close();
    }
    if (byteList == null || byteList.size() == 0) {
        return Collections.<OAuth2AccessToken>emptySet();
    }
    List<OAuth2AccessToken> accessTokens = new ArrayList<OAuth2AccessToken>(byteList.size());
    for (byte[] bytes : byteList) {
        OAuth2AccessToken accessToken = deserializeAccessToken(bytes);
        accessTokens.add(accessToken);
    }
    return Collections.<OAuth2AccessToken>unmodifiableCollection(accessTokens);
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) ArrayList(java.util.ArrayList) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 7 with RedisConnection

use of org.springframework.data.redis.connection.RedisConnection in project spring-security-oauth by spring-projects.

the class RedisTokenStore method removeAccessTokenUsingRefreshToken.

private void removeAccessTokenUsingRefreshToken(String refreshToken) {
    byte[] key = serializeKey(REFRESH_TO_ACCESS + refreshToken);
    List<Object> results = null;
    RedisConnection conn = getConnection();
    try {
        conn.openPipeline();
        conn.get(key);
        conn.del(key);
        results = conn.closePipeline();
    } finally {
        conn.close();
    }
    if (results == null) {
        return;
    }
    byte[] bytes = (byte[]) results.get(0);
    String accessToken = deserializeString(bytes);
    if (accessToken != null) {
        removeAccessToken(accessToken);
    }
}
Also used : RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 8 with RedisConnection

use of org.springframework.data.redis.connection.RedisConnection in project spring-security-oauth by spring-projects.

the class RedisTokenStore method readAuthentication.

@Override
public OAuth2Authentication readAuthentication(String token) {
    byte[] bytes = null;
    RedisConnection conn = getConnection();
    try {
        bytes = conn.get(serializeKey(AUTH + token));
    } finally {
        conn.close();
    }
    OAuth2Authentication auth = deserializeAuthentication(bytes);
    return auth;
}
Also used : OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 9 with RedisConnection

use of org.springframework.data.redis.connection.RedisConnection in project spring-integration by spring-projects.

the class RedisAvailableTests method awaitContainerSubscribedNoWait.

private void awaitContainerSubscribedNoWait(RedisMessageListenerContainer container) throws InterruptedException {
    RedisConnection connection = null;
    int n = 0;
    while (n++ < 300 && (connection = TestUtils.getPropertyValue(container, "subscriptionTask.connection", RedisConnection.class)) == null) {
        Thread.sleep(100);
    }
    assertNotNull("RedisMessageListenerContainer Failed to Connect", connection);
    n = 0;
    while (n++ < 300 && !connection.isSubscribed()) {
        Thread.sleep(100);
    }
    assertTrue("RedisMessageListenerContainer Failed to Subscribe", n < 300);
}
Also used : RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 10 with RedisConnection

use of org.springframework.data.redis.connection.RedisConnection in project springBoot-learn-demo by nbfujx.

the class RedisLock method getSet.

private String getSet(final String key, final String value) {
    Object obj = null;
    try {
        obj = redisTemplate.execute(new RedisCallback<Object>() {

            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                StringRedisSerializer serializer = new StringRedisSerializer();
                byte[] ret = connection.getSet(serializer.serialize(key), serializer.serialize(value));
                connection.close();
                return serializer.deserialize(ret);
            }
        });
    } catch (Exception e) {
        logger.error("setNX redis error, key : {}", key);
    }
    return obj != null ? (String) obj : null;
}
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