|
32 | 32 |
|
33 | 33 | import com.ibm.mq.jms.MQDestination; |
34 | 34 |
|
| 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 | + |
35 | 41 | // Use these imports for building with Jakarta Messaging |
36 | 42 | // import jakarta.jms.Destination; |
37 | 43 | // import jakarta.jms.JMSConsumer; |
@@ -81,8 +87,8 @@ public static void main(String[] args) { |
81 | 87 | JmsConnectionFactory connectionFactory = createJMSConnectionFactory(); |
82 | 88 | setJMSProperties(connectionFactory); |
83 | 89 | logger.info("created connection factory"); |
84 | | - |
85 | | - context = connectionFactory.createContext(); |
| 90 | + String access_token = obtainToken(); |
| 91 | + context = connectionFactory.createContext(null , access_token); |
86 | 92 | logger.info("context created"); |
87 | 93 |
|
88 | 94 | // Set targetClient to be non JMS, so no JMS headers are transmitted. |
@@ -172,8 +178,8 @@ private static void setJMSProperties(JmsConnectionFactory cf) { |
172 | 178 | cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, APP_NAME); |
173 | 179 | if (null != APP_USER && !APP_USER.trim().isEmpty()) { |
174 | 180 | 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); |
177 | 183 | } |
178 | 184 | if (CIPHER_SUITE != null && !CIPHER_SUITE.isEmpty()) { |
179 | 185 | cf.setStringProperty(WMQConstants.WMQ_SSL_CIPHER_SUITE, CIPHER_SUITE); |
@@ -213,4 +219,40 @@ private static void initialiseLogging() { |
213 | 219 | logger.finest("Logging initialised"); |
214 | 220 | } |
215 | 221 |
|
| 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 | + } |
216 | 258 | } |
0 commit comments