Java Code Examples for org.springframework.data.redis.connection.RedisConnection#close()

The following examples show how to use org.springframework.data.redis.connection.RedisConnection#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomRedisTokenStore.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
@Override
public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) {
    String key = authenticationKeyGenerator.extractKey(authentication);
    byte[] serializedKey = serializeKey(AUTH_TO_ACCESS + key);
    byte[] bytes;
    RedisConnection conn = getConnection();
    try {
        bytes = conn.get(serializedKey);
    } finally {
        conn.close();
    }
    OAuth2AccessToken accessToken = deserializeAccessToken(bytes);
    if (accessToken != null) {
        OAuth2Authentication storedAuthentication = readAuthentication(accessToken.getValue());
        if ((storedAuthentication == null || !key.equals(authenticationKeyGenerator.extractKey(storedAuthentication)))) {
            // Keep the stores consistent (maybe the same user is
            // represented by this authentication but the details have
            // changed)
            storeAccessToken(accessToken, authentication);
        }

    }
    return accessToken;
}
 
Example 2
Source File: CustomRedisTokenStore.java    From Auth-service with MIT License 6 votes vote down vote up
public void removeRefreshToken(String tokenValue) {
    byte[] refreshKey = serializeKey(REFRESH + tokenValue);
    byte[] refreshAuthKey = serializeKey(REFRESH_AUTH + tokenValue);
    byte[] refresh2AccessKey = serializeKey(REFRESH_TO_ACCESS + tokenValue);
    byte[] access2RefreshKey = serializeKey(ACCESS_TO_REFRESH + tokenValue);
    RedisConnection conn = getConnection();
    try {
        conn.openPipeline();
        conn.del(refreshKey);
        conn.del(refreshAuthKey);
        conn.del(refresh2AccessKey);
        conn.del(access2RefreshKey);
        conn.closePipeline();
    } finally {
        conn.close();
    }
}
 
Example 3
Source File: CustomRedisTokenStore.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
@Override
public void storeRefreshToken(OAuth2RefreshToken refreshToken, OAuth2Authentication authentication) {
    byte[] refreshKey = serializeKey(REFRESH + refreshToken.getValue());
    byte[] refreshAuthKey = serializeKey(REFRESH_AUTH + refreshToken.getValue());
    byte[] serializedRefreshToken = serialize(refreshToken);
    RedisConnection conn = getConnection();
    try {
        conn.openPipeline();
        if (springDataRedis_2_0) {
            try {
                this.redisConnectionSet_2_0.invoke(conn, refreshKey, serializedRefreshToken);
                this.redisConnectionSet_2_0.invoke(conn, refreshAuthKey, serialize(authentication));
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        } else {
            conn.set(refreshKey, serializedRefreshToken);
            conn.set(refreshAuthKey, serialize(authentication));
        }
        expireRefreshToken(refreshToken, conn, refreshKey, refreshAuthKey);
        conn.closePipeline();
    } finally {
        conn.close();
    }
}
 
Example 4
Source File: CustomRedisTokenStore.java    From Auth-service with MIT License 6 votes vote down vote up
@Override
public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) {
    String key = authenticationKeyGenerator.extractKey(authentication);
    byte[] serializedKey = serializeKey(AUTH_TO_ACCESS + key);
    byte[] bytes = null;
    RedisConnection conn = getConnection();
    try {
        bytes = conn.get(serializedKey);
    } finally {
        conn.close();
    }
    OAuth2AccessToken accessToken = deserializeAccessToken(bytes);
    if (accessToken != null) {
        OAuth2Authentication storedAuthentication = readAuthentication(accessToken.getValue());
        if ((storedAuthentication == null || !key.equals(authenticationKeyGenerator.extractKey(storedAuthentication)))) {
            // Keep the stores consistent (maybe the same user is
            // represented by this authentication but the details have
            // changed)
            storeAccessToken(accessToken, authentication);
        }

    }
    return accessToken;
}
 
Example 5
Source File: CustomRedisTokenStore.java    From Auth-service with MIT License 6 votes vote down vote up
@Override
public Collection<OAuth2AccessToken> findTokensByClientIdAndUserName(String clientId, String userName) {
    byte[] approvalKey = serializeKey(UNAME_TO_ACCESS + getApprovalKey(clientId, userName));
    List<byte[]> byteList = null;
    RedisConnection conn = getConnection();
    try {
        byteList = conn.lRange(approvalKey, 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);
}
 
Example 6
Source File: DefaultRedisCacheWriter.java    From api-gateway-old with Apache License 2.0 5 votes vote down vote up
private <T> T execute(String name, Function<RedisConnection, T> callback) {

        RedisConnection connection = connectionFactory.getConnection();
        try {

            checkAndPotentiallyWaitUntilUnlocked(name, connection);
            return callback.apply(connection);
        } finally {
            connection.close();
        }
    }
 
Example 7
Source File: BaseRedisTest.java    From spring-boot-email-tools with Apache License 2.0 5 votes vote down vote up
@BeforeTransaction
public void assertBeforeTransaction() {
    if (nonNull(beforeTransactionAssertion)) {
        final RedisConnection connection = connectionFactory.getConnection();
        beforeTransactionAssertion.assertBeforeTransaction(connection);
        connection.close();
    }
}
 
Example 8
Source File: DefaultRedisCacheWriter.java    From black-shop with Apache License 2.0 5 votes vote down vote up
private <T> T execute(String name, Function<RedisConnection, T> callback) {

		RedisConnection connection = connectionFactory.getConnection();
		try {

			checkAndPotentiallyWaitUntilUnlocked(name, connection);
			return callback.apply(connection);
		} finally {
			connection.close();
		}
	}
 
Example 9
Source File: CustomRedisTokenStore.java    From Auth-service with MIT License 5 votes vote down vote up
@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;
}
 
Example 10
Source File: CustomRedisTokenStore.java    From Auth-service with MIT License 5 votes vote down vote up
public OAuth2Authentication readAuthenticationForRefreshToken(String token) {
    RedisConnection conn = getConnection();
    try {
        byte[] bytes = conn.get(serializeKey(REFRESH_AUTH + token));
        OAuth2Authentication auth = deserializeAuthentication(bytes);
        return auth;
    } finally {
        conn.close();
    }
}
 
Example 11
Source File: RedisAuthenticationCodeServices.java    From Taroco with Apache License 2.0 5 votes vote down vote up
@Override
protected void store(final String code, final OAuth2Authentication authentication) {
    RedisConnection conn = getConnection();
    try {
        conn.hSet(serializationStrategy.serialize(PREFIX),
                serializationStrategy.serialize(code),
                serializationStrategy.serialize(authentication));
    } catch (Exception e) {
        log.error("保存authentication code 失败", e);
    } finally {
        conn.close();
    }
}
 
Example 12
Source File: RedisDistributedMapCacheClientService.java    From nifi with Apache License 2.0 5 votes vote down vote up
private <T> T withConnection(final RedisAction<T> action) throws IOException {
    RedisConnection redisConnection = null;
    try {
        redisConnection = redisConnectionPool.getConnection();
        return action.execute(redisConnection);
    } finally {
       if (redisConnection != null) {
           try {
               redisConnection.close();
           } catch (Exception e) {
               getLogger().warn("Error closing connection: " + e.getMessage(), e);
           }
       }
    }
}
 
Example 13
Source File: CustomRedisTokenStore.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<OAuth2AccessToken> findTokensByClientIdAndUserName(String clientId, String userName) {
    byte[] approvalKey = serializeKey(SecurityConstants.REDIS_UNAME_TO_ACCESS + getApprovalKey(clientId, userName));
    List<byte[]> byteList;
    RedisConnection conn = getConnection();
    try {
        byteList = conn.lRange(approvalKey, 0, -1);
    } finally {
        conn.close();
    }
    return getTokenCollections(byteList);
}
 
Example 14
Source File: DefaultRedisCacheWriter.java    From api-gateway-old with Apache License 2.0 5 votes vote down vote up
private void executeLockFree(Consumer<RedisConnection> callback) {

        RedisConnection connection = connectionFactory.getConnection();

        try {
            callback.accept(connection);
        } finally {
            connection.close();
        }
    }
 
Example 15
Source File: CustomRedisTokenStore.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
@Override
public OAuth2RefreshToken readRefreshToken(String tokenValue) {
    byte[] key = serializeKey(REFRESH + tokenValue);
    byte[] bytes;
    RedisConnection conn = getConnection();
    try {
        bytes = conn.get(key);
    } finally {
        conn.close();
    }
    return deserializeRefreshToken(bytes);
}
 
Example 16
Source File: CustomRedisTokenStore.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
public void removeAccessToken(String tokenValue) {
    byte[] accessKey = serializeKey(ACCESS + tokenValue);
    byte[] authKey = serializeKey(SecurityConstants.REDIS_TOKEN_AUTH + tokenValue);
    byte[] accessToRefreshKey = serializeKey(ACCESS_TO_REFRESH + tokenValue);
    RedisConnection conn = getConnection();
    try {
        conn.openPipeline();
        conn.get(accessKey);
        conn.get(authKey);
        conn.del(accessKey);
        conn.del(accessToRefreshKey);
        // Don't remove the refresh token - it's up to the caller to do that
        conn.del(authKey);
        List<Object> results = conn.closePipeline();
        byte[] access = (byte[]) results.get(0);
        byte[] auth = (byte[]) results.get(1);

        OAuth2Authentication authentication = deserializeAuthentication(auth);
        if (authentication != null) {
            String key = authenticationKeyGenerator.extractKey(authentication);
            byte[] authToAccessKey = serializeKey(AUTH_TO_ACCESS + key);
            byte[] unameKey = serializeKey(SecurityConstants.REDIS_UNAME_TO_ACCESS + getApprovalKey(authentication));
            byte[] clientId = serializeKey(SecurityConstants.REDIS_CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId());
            conn.openPipeline();
            conn.del(authToAccessKey);
            conn.lRem(unameKey, 1, access);
            conn.lRem(clientId, 1, access);
            conn.del(serialize(ACCESS + key));
            conn.closePipeline();
        }
    } finally {
        conn.close();
    }
}
 
Example 17
Source File: CustomRedisTokenStore.java    From Auth-service with MIT License 5 votes vote down vote up
public void removeAccessToken(String tokenValue) {
    byte[] accessKey = serializeKey(ACCESS + tokenValue);
    byte[] authKey = serializeKey(AUTH + tokenValue);
    byte[] accessToRefreshKey = serializeKey(ACCESS_TO_REFRESH + tokenValue);
    RedisConnection conn = getConnection();
    try {
        conn.openPipeline();
        conn.get(accessKey);
        conn.get(authKey);
        conn.del(accessKey);
        conn.del(accessToRefreshKey);
        // Don't remove the refresh token - it's up to the caller to do that
        conn.del(authKey);
        List<Object> results = conn.closePipeline();
        byte[] access = (byte[]) results.get(0);
        byte[] auth = (byte[]) results.get(1);

        OAuth2Authentication authentication = deserializeAuthentication(auth);
        if (authentication != null) {
            String key = authenticationKeyGenerator.extractKey(authentication);
            byte[] authToAccessKey = serializeKey(AUTH_TO_ACCESS + key);
            byte[] unameKey = serializeKey(UNAME_TO_ACCESS + getApprovalKey(authentication));
            byte[] clientId = serializeKey(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId());
            conn.openPipeline();
            conn.del(authToAccessKey);
            conn.lRem(unameKey, 1, access);
            conn.lRem(clientId, 1, access);
            conn.del(serialize(ACCESS + key));
            conn.closePipeline();
        }
    } finally {
        conn.close();
    }
}
 
Example 18
Source File: RedisServiceImpl.java    From redis-admin with Apache License 2.0 5 votes vote down vote up
private String getDataType(String serverName, int dbIndex, String key) {
	RedisTemplate redisTemplate = RedisApplication.redisTemplatesMap.get(serverName);
	RedisConnection connection = RedisConnectionUtils.getConnection(redisTemplate.getConnectionFactory());
	connection.select(dbIndex);
	DataType dataType = connection.type(key.getBytes());
	connection.close();
	return dataType.name().toUpperCase();
}
 
Example 19
Source File: CustomRedisTokenStore.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
@Override
public OAuth2Authentication readAuthentication(String token) {
    byte[] bytes;
    RedisConnection conn = getConnection();
    try {
        bytes = conn.get(serializeKey(SecurityConstants.REDIS_TOKEN_AUTH + token));
    } finally {
        conn.close();
    }
    return deserializeAuthentication(bytes);
}
 
Example 20
Source File: RedisLockProvider.java    From ShedLock with Apache License 2.0 4 votes vote down vote up
private static void close(RedisConnection redisConnection) {
    if (redisConnection != null) {
        redisConnection.close();
    }
}