CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-redis-clients--jedis

Jedis is a blazingly small and sane Redis java client.

Overview
Eval results
Files

authentication.mddocs/

Authentication

Jedis provides comprehensive authentication support including traditional Redis AUTH, Redis ACL, and advanced token-based authentication with AuthX integration.

Capabilities

Authentication Manager

Core authentication management for token-based authentication systems.

/**
 * Manager for AuthX token-based authentication
 */
public class AuthXManager {
    /**
     * Create authentication manager with credentials provider
     * @param credentialsProvider Provider for credentials
     */
    public AuthXManager(RedisCredentialsProvider credentialsProvider);
    
    /**
     * Authenticate a connection
     * @param connection Connection to authenticate
     * @throws JedisAuthenticationException if authentication fails
     */
    public void authenticate(Connection connection);
    
    /**
     * Refresh authentication token
     * @throws JedisAuthenticationException if refresh fails
     */
    public void refreshToken();
    
    /**
     * Check if token needs refresh
     * @return true if token should be refreshed
     */
    public boolean shouldRefreshToken();
    
    /**
     * Get current token expiration time
     * @return Expiration timestamp in milliseconds
     */
    public long getTokenExpiration();
}

Token Credentials

Implementation of RedisCredentials for token-based authentication.

/**
 * Token-based credentials implementation
 */
public class TokenCredentials implements RedisCredentials {
    /**
     * Create token credentials
     * @param token Authentication token
     */
    public TokenCredentials(String token);
    
    /**
     * Get the authentication token
     * @return Authentication token
     */
    public String getToken();
    
    /**
     * Get user name (may be null for token auth)
     * @return User name or null
     */
    public String getUser();
    
    /**
     * Get password (returns token)
     * @return Authentication token
     */
    public String getPassword();
}

Authentication Exception

Exception thrown for authentication-related errors.

/**
 * Exception for authentication failures
 */
public class JedisAuthenticationException extends JedisException {
    /**
     * Create authentication exception
     * @param message Error message
     */
    public JedisAuthenticationException(String message);
    
    /**
     * Create authentication exception with cause
     * @param message Error message
     * @param cause Underlying cause
     */
    public JedisAuthenticationException(String message, Throwable cause);
}

Authentication Event Listener

Interface for handling authentication events.

/**
 * Listener for authentication events
 */
public interface AuthXEventListener {
    /**
     * Called when authentication succeeds
     * @param connection Authenticated connection
     */
    void onAuthenticationSuccess(Connection connection);
    
    /**
     * Called when authentication fails
     * @param connection Connection that failed
     * @param exception Authentication exception
     */
    void onAuthenticationFailure(Connection connection, JedisAuthenticationException exception);
    
    /**
     * Called when token is refreshed
     * @param newToken New authentication token
     */
    void onTokenRefresh(String newToken);
}

Usage Examples

Basic Authentication

import redis.clients.jedis.*;

// Traditional AUTH with password
Jedis jedis = new Jedis("localhost", 6379, 
    DefaultJedisClientConfig.builder()
        .password("mypassword")
        .build());

// Redis ACL with username and password
Jedis jedis = new Jedis("localhost", 6379,
    DefaultJedisClientConfig.builder()
        .user("myuser")
        .password("mypassword")  
        .build());

Token-Based Authentication

import redis.clients.jedis.authentication.*;

// Create token credentials
TokenCredentials tokenCreds = new TokenCredentials("jwt_token_here");

// Create credentials provider
RedisCredentialsProvider provider = new DefaultRedisCredentialsProvider(tokenCreds);

// Create authentication manager
AuthXManager authManager = new AuthXManager(provider);

// Use with Jedis client
JedisClientConfig config = DefaultJedisClientConfig.builder()
    .credentialsProvider(provider)
    .build();

Jedis jedis = new Jedis("localhost", 6379, config);

Authentication with Event Handling

import redis.clients.jedis.authentication.*;

// Create event listener
AuthXEventListener listener = new AuthXEventListener() {
    @Override
    public void onAuthenticationSuccess(Connection connection) {
        System.out.println("Authentication successful");
    }
    
    @Override
    public void onAuthenticationFailure(Connection connection, JedisAuthenticationException exception) {
        System.err.println("Authentication failed: " + exception.getMessage());
    }
    
    @Override
    public void onTokenRefresh(String newToken) {
        System.out.println("Token refreshed");
    }
};

// Configure authentication with event handling
AuthXManager authManager = new AuthXManager(provider);
// Note: Event listener integration depends on implementation

Microsoft EntraID Integration

Jedis includes built-in support for Microsoft EntraID (formerly Azure AD) through the extension library:

// Microsoft EntraID authentication (requires redis-authx-entraid dependency)
// This example shows the general pattern - actual implementation may vary
import redis.clients.authentication.entraid.*;

// Configure EntraID credentials
EntraIDCredentialsProvider entraProvider = new EntraIDCredentialsProvider()
    .tenantId("your-tenant-id")
    .clientId("your-client-id")
    .clientSecret("your-client-secret");

// Use with Jedis
JedisClientConfig config = DefaultJedisClientConfig.builder()
    .credentialsProvider(entraProvider)
    .build();

Jedis jedis = new Jedis("redis-enterprise-server", 6379, config);

Connection Pool with Authentication

// Pool with authentication
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(20);

JedisClientConfig clientConfig = DefaultJedisClientConfig.builder()
    .user("myuser")
    .password("mypassword")
    .build();

JedisPool pool = new JedisPool(poolConfig, "localhost", 6379, clientConfig);

try (Jedis jedis = pool.getResource()) {
    jedis.set("key", "value");
}

SSL with Authentication

// SSL connection with authentication
JedisClientConfig sslConfig = DefaultJedisClientConfig.builder()
    .ssl(true)
    .user("myuser")
    .password("mypassword")
    .build();

Jedis jedis = new Jedis("redis-ssl-server", 6380, sslConfig);

Error Handling

try {
    Jedis jedis = new Jedis("localhost", 6379,
        DefaultJedisClientConfig.builder()
            .password("wrong_password")
            .build());
    jedis.ping();
} catch (JedisAuthenticationException e) {
    System.err.println("Authentication failed: " + e.getMessage());
} catch (JedisConnectionException e) {
    System.err.println("Connection failed: " + e.getMessage());
}

Best Practices

  1. Use Connection Pooling: Always use connection pools in production for authenticated connections
  2. Secure Token Storage: Store authentication tokens securely and rotate them regularly
  3. Handle Token Expiration: Implement proper token refresh logic for long-running applications
  4. Use SSL: Always use SSL/TLS with authentication in production environments
  5. Monitor Authentication: Log authentication events for security monitoring
  6. Least Privilege: Use Redis ACL to grant minimum required permissions

Install with Tessl CLI

npx tessl i tessl/maven-redis-clients--jedis

docs

authentication.md

client-side-caching.md

clustering.md

commands-operations.md

connection-management.md

core-clients.md

exceptions.md

index.md

modules.md

parameters.md

pubsub.md

transactions-pipelining.md

tile.json