Skip to content

Commit 422a485

Browse files
authored
support entra id auth
1 parent b94ca33 commit 422a485

File tree

1 file changed

+136
-40
lines changed

1 file changed

+136
-40
lines changed

articles/azure-cache-for-redis/cache-java-get-started.md

Lines changed: 136 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,49 +33,152 @@ Clone the repo [Java quickstart](https://github.com/Azure-Samples/azure-cache-re
3333

3434
Depending on your operating system, add environment variables for your **Host name** and **Primary access key** that you noted previously. Open a command prompt, or a terminal window, and set up the following values:
3535

36-
### [Linux](#tab/bash)
36+
## [Microsoft Entra ID Authentication (recommended)](#tab/entraid)
3737

3838
```bash
39-
export REDISCACHEHOSTNAME=<your-host-name>.redis.cache.windows.net
40-
export REDISCACHEKEY=<your-primary-access-key>
39+
export REDIS_CACHE_HOSTNAME=<your-host-name>.redis.cache.windows.net
40+
export USER_NAME=<user-name>
4141
```
4242

43-
### [Windows](#tab/cmd)
43+
Replace the placeholders with the following values:
4444

45-
```cmd
46-
set REDISCACHEHOSTNAME=<your-host-name>.redis.cache.windows.net
47-
set REDISCACHEKEY=<your-primary-access-key>
48-
```
45+
- `<your-host-name>`: The DNS host name, obtained from the *Properties* section of your Azure Cache for Redis resource in the Azure portal.
46+
- `<user-name>`: Object ID of your managed identity or service principal.
4947

50-
---
48+
## [Access Key Authentication](#tab/accesskey)
49+
50+
```bash
51+
export REDIS_CACHE_HOSTNAME=<your-host-name>.redis.cache.windows.net
52+
export REDIS_CACHE_KEY=<your-primary-access-key>
53+
```
5154

5255
Replace the placeholders with the following values:
5356

5457
- `<your-host-name>`: The DNS host name, obtained from the *Properties* section of your Azure Cache for Redis resource in the Azure portal.
5558
- `<your-primary-access-key>`: The primary access key, obtained from the *Access keys* section of your Azure Cache for Redis resource in the Azure portal.
5659

57-
## Understand the Java sample
60+
---
61+
5862

59-
In this sample, you use Maven to run the quickstart app.
63+
## Create a new Java app
6064

61-
1. Change to the new *redistest* project directory.
65+
Using Maven, generate a new quickstart app:
66+
67+
```xml
68+
mvn archetype:generate \
69+
-DarchetypeGroupId=org.apache.maven.archetypes \
70+
-DarchetypeArtifactId=maven-archetype-quickstart \
71+
-DarchetypeVersion=1.3 \
72+
-DinteractiveMode=false \
73+
-DgroupId=example.demo \
74+
-DartifactId=redis-jedis-test \
75+
-Dversion=1.0
76+
```
77+
78+
1. Change to the new **redis-jedis-test** project directory.
6279

6380
1. Open the *pom.xml* file. In the file, you see a dependency for [Jedis](https://github.com/xetorthio/jedis):
6481

82+
## [Microsoft Entra ID Authentication (recommended)](#tab/entraid)
83+
84+
```xml
85+
<dependency>
86+
<groupId>com.azure</groupId>
87+
<artifactId>azure-identity</artifactId>
88+
<version>1.11.2</version> <!-- {x-version-update;com.azure:azure-identity;dependency} -->
89+
</dependency>
90+
91+
<dependency>
92+
<groupId>redis.clients</groupId>
93+
<artifactId>jedis</artifactId>
94+
<version>5.1.0</version> <!-- {x-version-update;redis.clients:jedis;external_dependency} -->
95+
</dependency>
96+
```
97+
98+
99+
## [Access Key Authentication](#tab/accesskey)
100+
65101
```xml
66102
<dependency>
67-
<groupId>redis.clients</groupId>
68-
<artifactId>jedis</artifactId>
69-
<version>4.1.0</version>
70-
<type>jar</type>
71-
<scope>compile</scope>
103+
<groupId>redis.clients</groupId>
104+
<artifactId>jedis</artifactId>
105+
<version>5.1.0</version> <!-- {x-version-update;redis.clients:jedis;external_dependency} -->
72106
</dependency>
73107
```
108+
109+
---
110+
111+
74112

75113
1. Close the *pom.xml* file.
76114

77115
1. Open *App.java* and see the code with the following code:
116+
117+
## [Microsoft Entra ID Authentication (recommended)](#tab/entraid)
118+
```java
119+
package example.demo;
120+
121+
import com.azure.identity.DefaultAzureCredential;
122+
import com.azure.identity.DefaultAzureCredentialBuilder;
123+
import com.azure.core.credential.TokenRequestContext;
124+
import redis.clients.jedis.DefaultJedisClientConfig;
125+
import redis.clients.jedis.Jedis;
126+
127+
/**
128+
* Redis test
129+
*
130+
*/
131+
public class App
132+
{
133+
public static void main( String[] args )
134+
{
135+
136+
boolean useSsl = true;
137+
138+
//Construct a Token Credential from Identity library, e.g. DefaultAzureCredential / ClientSecretCredential / Client CertificateCredential / ManagedIdentityCredential etc.
139+
DefaultAzureCredential defaultAzureCredential = new DefaultAzureCredentialBuilder().build();
140+
141+
// Fetch a Microsoft Entra token to be used for authentication. This token will be used as the password.
142+
String token = defaultAzureCredential
143+
.getToken(new TokenRequestContext()
144+
.addScopes("https://redis.azure.com/.default")).block().getToken();
78145

146+
String cacheHostname = System.getenv("REDIS_CACHE_HOSTNAME");
147+
String username = System.getenv("USER_NAME");
148+
149+
// Connect to the Azure Cache for Redis over the TLS/SSL port using the key.
150+
Jedis jedis = new Jedis(cacheHostname, 6380, DefaultJedisClientConfig.builder()
151+
.password(token) // Microsoft Entra access token as password is required.
152+
.user(username) // Username is Required
153+
.ssl(useSsl) // SSL Connection is Required
154+
.build());
155+
// Perform cache operations using the cache connection object...
156+
157+
// Simple PING command
158+
System.out.println( "\nCache Command : Ping" );
159+
System.out.println( "Cache Response : " + jedis.ping());
160+
161+
// Simple get and put of integral data types into the cache
162+
System.out.println( "\nCache Command : GET Message" );
163+
System.out.println( "Cache Response : " + jedis.get("Message"));
164+
165+
System.out.println( "\nCache Command : SET Message" );
166+
System.out.println( "Cache Response : " + jedis.set("Message", "Hello! The cache is working from Java!"));
167+
168+
// Demonstrate "SET Message" executed as expected...
169+
System.out.println( "\nCache Command : GET Message" );
170+
System.out.println( "Cache Response : " + jedis.get("Message"));
171+
172+
// Get the client list, useful to see if connection list is growing...
173+
System.out.println( "\nCache Command : CLIENT LIST" );
174+
System.out.println( "Cache Response : " + jedis.clientList());
175+
176+
jedis.close();
177+
}
178+
}
179+
```
180+
181+
## [Access Key Authentication](#tab/accesskey)
79182
```java
80183
package example.demo;
81184

@@ -92,8 +195,8 @@ In this sample, you use Maven to run the quickstart app.
92195
{
93196

94197
boolean useSsl = true;
95-
String cacheHostname = System.getenv("REDISCACHEHOSTNAME");
96-
String cachekey = System.getenv("REDISCACHEKEY");
198+
String cacheHostname = System.getenv("REDIS_CACHE_HOSTNAME");
199+
String cachekey = System.getenv("REDIS_CACHE_KEY");
97200

98201
// Connect to the Azure Cache for Redis over the TLS/SSL port using the key.
99202
Jedis jedis = new Jedis(cacheHostname, 6380, DefaultJedisClientConfig.builder()
@@ -127,6 +230,9 @@ In this sample, you use Maven to run the quickstart app.
127230
}
128231
```
129232

233+
---
234+
235+
130236
This code shows you how to connect to an Azure Cache for Redis instance using the cache host name and key environment variables. The code also stores and retrieves a string value in the cache. The `PING` and `CLIENT LIST` commands are also executed.
131237

132238
1. Close the *App.java*.
@@ -135,40 +241,30 @@ In this sample, you use Maven to run the quickstart app.
135241

136242
1. First, if you haven't already, you must set the environment variables as noted previously.
137243
138-
### [Linux](#tab/bash)
244+
## [Microsoft Entra ID Authentication (recommended)](#tab/entraid)
139245
140-
```bash
141-
export REDISCACHEHOSTNAME=<your-host-name>.redis.cache.windows.net
142-
export REDISCACHEKEY=<your-primary-access-key>
143-
```
246+
```bash
247+
export REDIS_CACHE_HOSTNAME=<your-host-name>.redis.cache.windows.net
248+
export USER_NAME=<user-name>
249+
```
144250
145-
### [Windows](#tab/cmd)
251+
## [Access Key Authentication](#tab/accesskey)
146252
147-
```cmd
148-
set REDISCACHEHOSTNAME=<your-host-name>.redis.cache.windows.net
149-
set REDISCACHEKEY=<your-primary-access-key>
150-
```
253+
```bash
254+
export REDIS_CACHE_HOSTNAME=<your-host-name>.redis.cache.windows.net
255+
export REDIS_CACHE_KEY=<your-primary-access-key>
256+
```
257+
151258
152259
---
153260
154261
1. Execute the following Maven command to build and run the app:
155262
156-
### [Linux](#tab/bash)
157-
158263
```bash
159264
mvn compile
160265
mvn exec:java -D exec.mainClass=example.demo.App
161266
```
162267
163-
### [Windows](#tab/cmd)
164-
165-
```cmd
166-
mvn compile
167-
mvn exec:java -D exec.mainClass=example.demo.App
168-
```
169-
170-
---
171-
172268
In the following output, you can see that the `Message` key previously had a cached value. The value was updated to a new value using `jedis.set`. The app also executed the `PING` and `CLIENT LIST` commands.
173269
174270
```output

0 commit comments

Comments
 (0)