|
| 1 | +package io.lettuce.examples; |
| 2 | + |
| 3 | +import io.lettuce.core.ClientOptions; |
| 4 | +import io.lettuce.core.RedisClient; |
| 5 | +import io.lettuce.core.SslOptions; |
| 6 | +import io.lettuce.core.api.StatefulRedisConnection; |
| 7 | + |
| 8 | +import javax.net.ssl.SNIHostName; |
| 9 | +import javax.net.ssl.SNIServerName; |
| 10 | +import javax.net.ssl.SSLParameters; |
| 11 | +import java.io.File; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +public class ConnectToRedisSSLWithSni { |
| 16 | + |
| 17 | + public static void main(String[] args) { |
| 18 | + // Syntax: rediss://[password@]host[:port][/databaseNumber] |
| 19 | + // Adapt the port to the stunnel port in front of your Redis instance |
| 20 | + RedisClient redisClient = RedisClient.create("rediss://127.0.0.1:36443"); |
| 21 | + |
| 22 | + List<SNIServerName> serverNames = new ArrayList<>(); |
| 23 | + |
| 24 | + // Hint : Enable SSL debugging (-Djavax.net.debug=ssl to the VM Args) |
| 25 | + // to verify/troubleshoot ssl configuration |
| 26 | + // Hint : Adapt the server name to switch between multiple instances |
| 27 | + serverNames.add(new SNIHostName("redis-sni1.local")); |
| 28 | + // serverNames.add(new SNIHostName("redis-sni2.local")); |
| 29 | + SslOptions sslOptions = SslOptions.builder().jdkSslProvider().truststore(new File("work/truststore.jks"), "changeit") |
| 30 | + .sslParameters(() -> { |
| 31 | + SSLParameters parameters = new SSLParameters(); |
| 32 | + parameters.setServerNames(serverNames); |
| 33 | + return parameters; |
| 34 | + }).build(); |
| 35 | + |
| 36 | + ClientOptions clientOptions = ClientOptions.builder().sslOptions(sslOptions).build(); |
| 37 | + redisClient.setOptions(clientOptions); |
| 38 | + |
| 39 | + StatefulRedisConnection<String, String> connection = redisClient.connect(); |
| 40 | + System.out.println("Connected to Redis using TLS with enabled SNI"); |
| 41 | + |
| 42 | + connection.close(); |
| 43 | + redisClient.shutdown(); |
| 44 | + } |
| 45 | + |
| 46 | +} |
0 commit comments