@@ -122,6 +122,8 @@ Save the *pom.xml* file.
122
122
123
123
Open * App.java* and replace the code with the following code:
124
124
125
+
126
+ ### [ Microsoft Entra ID Authentication (recommended)] ( #tab/entraid )
125
127
``` java
126
128
package example.demo ;
127
129
@@ -140,7 +142,6 @@ import javax.cache.configuration.Configuration;
140
142
import javax.cache.configuration.MutableConfiguration ;
141
143
import java.time.LocalDateTime ;
142
144
143
-
144
145
/**
145
146
* Redis test
146
147
*
@@ -165,56 +166,106 @@ public class App {
165
166
166
167
System . out. println(" \n Cache Command : SET Message" );
167
168
map. put(" Message" ,
168
- String . format(" Hello! The cache is working from Java! %s" , LocalDateTime . now()));
169
+ String . format(" Hello! The cache is working from Java! %s" , LocalDateTime . now()));
169
170
170
171
// Demonstrate "SET Message" executed as expected
171
172
System . out. println(" \n Cache Command : GET Message" );
172
173
System . out. println(" Cache Response : " + map. get(" Message" ));
173
174
174
175
redissonClient. shutdown();
175
- }
176
-
177
- private static Config getConfig (){
178
- if (" MicrosoftEntraID" . equals(System . getenv(" AUTH_TYPE" ))) {
179
- System . out. println(" Auth with Microsoft Entra ID" );
180
- return getConfigAuthWithAAD();
181
- } else if (" RedisKey" . equals(System . getenv(" AUTH_TYPE" ))) {
182
- System . out. println(" Auth with Redis key" );
183
- return getConfigAuthWithKey();
184
- }
185
- System . out. println(" Auth with Redis key" );
186
- return getConfigAuthWithKey();
187
- }
188
176
189
- private static Config getConfigAuthWithKey () {
190
- // Connect to the Azure Cache for Redis over the TLS/SSL port using the key
191
- Config redissonconfig = new Config ();
192
- redissonconfig. useSingleServer(). setPassword(System . getenv(" REDIS_CACHE_KEY" ))
193
- .setAddress(String . format(" rediss://%s:6380" , System . getenv(" REDIS_CACHE_HOSTNAME" )));
194
- return redissonconfig;
195
177
}
196
178
197
- private static Config getConfigAuthWithAAD () {
179
+ private static Config getConfig () {
198
180
// Construct a Token Credential from Identity library, e.g. DefaultAzureCredential / ClientSecretCredential / Client CertificateCredential / ManagedIdentityCredential etc.
199
181
DefaultAzureCredential defaultAzureCredential = new DefaultAzureCredentialBuilder (). build();
200
182
201
183
// Fetch a Microsoft Entra token to be used for authentication.
202
184
String token = defaultAzureCredential
203
- .getToken(new TokenRequestContext ()
204
- .addScopes(" acca5fbb-b7e4-4009-81f1-37e38fd66d78 /.default" )). block(). getToken();
185
+ .getToken(new TokenRequestContext ()
186
+ .addScopes(" https://redis.azure.com /.default" )). block(). getToken();
205
187
206
188
// Connect to the Azure Cache for Redis over the TLS/SSL port using the key
207
189
Config redissonconfig = new Config ();
208
190
redissonconfig. useSingleServer()
209
- .setAddress(String . format(" rediss://%s:6380" , System . getenv(" REDIS_CACHE_HOSTNAME" )))
210
- .setUsername(System . getenv(" USER_NAME" )) // (Required) Username is Object ID of your managed identity or service principal
211
- .setPassword(token); // Microsoft Entra access token as password is required.
191
+ .setAddress(String . format(" rediss://%s:6380" , System . getenv(" REDIS_CACHE_HOSTNAME" )))
192
+ .setUsername(System . getenv(" USER_NAME" )) // (Required) Username is Object ID of your managed identity or service principal
193
+ .setPassword(token); // Microsoft Entra access token as password is required.
212
194
return redissonconfig;
213
195
}
196
+ }
197
+
198
+ ```
199
+
200
+ ### [ Access Key Authentication] ( #tab/accesskey )
201
+
202
+ ``` java
203
+ package example.demo ;
204
+
205
+ import org.redisson.Redisson ;
206
+ import org.redisson.api.RedissonClient ;
207
+ import org.redisson.config.Config ;
208
+ import org.redisson.jcache.configuration.RedissonConfiguration ;
209
+
210
+ import javax.cache.Cache ;
211
+ import javax.cache.CacheManager ;
212
+ import javax.cache.Caching ;
213
+ import javax.cache.configuration.Configuration ;
214
+ import javax.cache.configuration.MutableConfiguration ;
215
+ import java.time.LocalDateTime ;
216
+
217
+ /**
218
+ * Redis test
219
+ *
220
+ */
221
+ public class App {
222
+ public static void main (String [] args ) {
223
+
224
+ Config redissonconfig = getConfig();
214
225
226
+ RedissonClient redissonClient = Redisson . create(redissonconfig);
227
+
228
+ MutableConfiguration<String , String > jcacheConfig = new MutableConfiguration<> ();
229
+ Configuration<String , String > config = RedissonConfiguration . fromInstance(redissonClient, jcacheConfig);
230
+
231
+ // Perform cache operations using JCache
232
+ CacheManager manager = Caching . getCachingProvider(). getCacheManager();
233
+ Cache<String , String > map = manager. createCache(" test" , config);
234
+
235
+ // Simple get and put of string data into the cache
236
+ System . out. println(" \n Cache Command : GET Message" );
237
+ System . out. println(" Cache Response : " + map. get(" Message" ));
238
+
239
+ System . out. println(" \n Cache Command : SET Message" );
240
+ map. put(" Message" ,
241
+ String . format(" Hello! The cache is working from Java! %s" , LocalDateTime . now()));
242
+
243
+ // Demonstrate "SET Message" executed as expected
244
+ System . out. println(" \n Cache Command : GET Message" );
245
+ System . out. println(" Cache Response : " + map. get(" Message" ));
246
+
247
+ redissonClient. shutdown();
248
+
249
+ }
250
+
251
+ private static Config getConfig () {
252
+ // Connect to the Azure Cache for Redis over the TLS/SSL port using the key
253
+ Config redissonconfig = new Config ();
254
+ redissonconfig. useSingleServer(). setPassword(System . getenv(" REDIS_CACHE_KEY" ))
255
+ .setAddress(String . format(" rediss://%s:6380" , System . getenv(" REDIS_CACHE_HOSTNAME" )));
256
+ return redissonconfig;
257
+ }
215
258
}
259
+
216
260
```
217
261
262
+ ---
263
+
264
+
265
+
266
+ ``` java
267
+
268
+
218
269
This code shows you how to connect to an Azure Cache for Redis instance using Microsoft Entra ID with the JCache API support from the Redisson client library. The code also stores and retrieves a string value in the cache. For more information on JCache , see the [JCache specification](https: // jcp.org/en/jsr/detail?id=107).
219
270
220
271
Save * App . java* .
0 commit comments