Skip to content

Commit 8ffe48e

Browse files
JWT_change1
1 parent ea2dcc9 commit 8ffe48e

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

JMS/com/ibm/mq/samples/jms/JmsPut.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232

3333
import com.ibm.mq.jms.MQDestination;
3434

35+
import java.net.URI;
36+
import java.net.http.HttpClient;
37+
import java.net.http.HttpRequest;
38+
import java.net.http.HttpRequest.BodyPublishers;
39+
import java.net.http.HttpResponse;
40+
3541
// Use these imports for building with Jakarta Messaging
3642
// import jakarta.jms.Destination;
3743
// import jakarta.jms.JMSConsumer;
@@ -81,8 +87,8 @@ public static void main(String[] args) {
8187
JmsConnectionFactory connectionFactory = createJMSConnectionFactory();
8288
setJMSProperties(connectionFactory);
8389
logger.info("created connection factory");
84-
85-
context = connectionFactory.createContext();
90+
String access_token = obtainToken();
91+
context = connectionFactory.createContext(null , access_token);
8692
logger.info("context created");
8793

8894
// Set targetClient to be non JMS, so no JMS headers are transmitted.
@@ -172,8 +178,8 @@ private static void setJMSProperties(JmsConnectionFactory cf) {
172178
cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, APP_NAME);
173179
if (null != APP_USER && !APP_USER.trim().isEmpty()) {
174180
cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true);
175-
cf.setStringProperty(WMQConstants.USERID, APP_USER);
176-
cf.setStringProperty(WMQConstants.PASSWORD, APP_PASSWORD);
181+
// cf.setStringProperty(WMQConstants.USERID, APP_USER);
182+
// cf.setStringProperty(WMQConstants.PASSWORD, APP_PASSWORD);
177183
}
178184
if (CIPHER_SUITE != null && !CIPHER_SUITE.isEmpty()) {
179185
cf.setStringProperty(WMQConstants.WMQ_SSL_CIPHER_SUITE, CIPHER_SUITE);
@@ -213,4 +219,40 @@ private static void initialiseLogging() {
213219
logger.finest("Logging initialised");
214220
}
215221

222+
public static String obtainToken() {
223+
String access_token = "";
224+
String tokenEndpoint = System.getenv("JWT_TOKEN_ENDPOINT");
225+
String tokenUsername = System.getenv("JWT_TOKEN_USERNAME");
226+
String tokenPassword = System.getenv("JWT_TOKEN_PWD");
227+
String tokenClientId = System.getenv("JWT_TOKEN_CLIENTID");
228+
229+
//build the string with parameters provided as environment variables, to include in the POST request to the token issuer
230+
String postBuild = String.format("client_id=%s&username=%s&password=%s&grant_type=password",tokenClientId, tokenUsername, tokenPassword);
231+
logger.info("parameter string: " + postBuild);
232+
HttpClient client = HttpClient.newHttpClient();
233+
234+
HttpRequest request = HttpRequest.newBuilder()
235+
.uri(URI.create(
236+
// "<keycloak URL>/realms/master/protocol/openid-connect/token"
237+
tokenEndpoint
238+
))
239+
.POST(
240+
BodyPublishers.ofString(postBuild))
241+
.setHeader("Content-Type", "application/x-www-form-urlencoded")
242+
.build();
243+
logger.info("obtaining token from:" + tokenEndpoint);
244+
try {
245+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
246+
logger.fine(String.valueOf(response.statusCode()));
247+
logger.fine(response.body());
248+
249+
JSONObject myJsonObject = new JSONObject(response.body());
250+
access_token = myJsonObject.getString("access_token");
251+
logger.info("Using token:" + access_token);
252+
} catch (Exception e) {
253+
logger.info("Using token:" + access_token);
254+
e.printStackTrace();
255+
}
256+
return access_token;
257+
}
216258
}

0 commit comments

Comments
 (0)