// Custom provided authentication request, it is not mandatory. Emjapi can requests token from the client info.
// TokenRequest tokenRequest = new TokenRequest();
// settings.setAuthenticationRequest(tokenRequest::requestToken);
private static final String NAMESPACE = "<QUEUE_NAMESPACE>";
...
@PostMapping(MESSAGE_REST_PATH)
public ResponseEntity<String> sendMessage(@RequestBody String message, @PathVariable String queueName) throws MessagingException {
try {
queueName = decodeValue(queueName);
} catch (UnsupportedEncodingException e1) {
return ResponseEntity.badRequest().body("Unable to decode the queuename");
}
// add namespace
queueName = NAMESPACE + queueName;
...
enterprise-messaging
:@Configuration
public class MessagingServiceConfig {
@Bean
public MessagingServiceFactory getMessagingServiceFactory() throws JsonParseException, JsonMappingException, IOException {
String credentials = System.getenv("enterprise-messaging");
Map<String, Object> credentialsMap = new ObjectMapper().readValue(credentials, HashMap.class);
return MessagingServiceFactoryCreator.createFactoryFromCredentials(credentialsMap);
}
...
@Configuration
public class MessagingServiceConfig {
@Bean
public MessagingServiceFactory getMessagingServiceFactory() throws JsonParseException, JsonMappingException, IOException {
String encodedKeyValue = JsonPath.read(getCredentialStoreKey(), "$.value");
byte[] decodedKeyValue = Base64.getDecoder().decode(encodedKeyValue);
Map<String, Object> credentialsMap = new ObjectMapper().readValue(decodedKeyValue, HashMap.class);
return MessagingServiceFactoryCreator.createFactoryFromCredentials(credentialsMap);
}
@Bean
public MessagingServiceJmsConnectionFactory getMessagingServiceJmsConnectionFactory(MessagingServiceFactory messagingServiceFactory) {
try {
/*
* The settings object is preset with default values (see JavaDoc) and can be adjusted. The settings aren't required and depend on the use-case. Note: a connection will be closed after an idle time of 5 minutes.
*/
MessagingServiceJmsSettings settings = new MessagingServiceJmsSettings();
settings.setFailoverMaxReconnectAttempts(5); // use -1 for unlimited attempts
settings.setFailoverInitialReconnectDelay(3000);
settings.setFailoverReconnectDelay(3000);
settings.setJmsRequestTimeout(30000);
settings.setAmqpIdleTimeout(-1);
// Custom provided authentication request, it is not mandatory. Emjapi can requests token from the client info.
// TokenRequest tokenRequest = new TokenRequest();
// settings.setAuthenticationRequest(tokenRequest::requestToken);
return messagingServiceFactory.createConnectionFactory(MessagingServiceJmsConnectionFactory.class, settings);
} catch (MessagingException e) {
throw new IllegalStateException("Unable to create the Connection Factory", e);
}
}
private String decryptPayload(String responsePayload) {
final AlgorithmConstraints CONTENT_ENCRYPTION_ALGORITHM_CONSTRAINTS = new AlgorithmConstraints(ConstraintType.PERMIT, ContentEncryptionAlgorithmIdentifiers.AES_256_GCM);
final AlgorithmConstraints KEY_ENCRYPTION_ALGORITHM_CONSTRAINTS = new AlgorithmConstraints(ConstraintType.PERMIT, KeyManagementAlgorithmIdentifiers.RSA_OAEP_256);
// get service binding environment variables
final String vcapServicesJson = System.getenv("VCAP_SERVICES");
String PRIVATE_KEY = JsonPath.read(vcapServicesJson, "$.credstore[0].credentials.encryption.client_private_key");
String decryptedPayload = null;
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] binaryKey = Base64.getDecoder().decode(PRIVATE_KEY);
RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(new PKCS8EncodedKeySpec(binaryKey));
JsonWebEncryption jwe = new JsonWebEncryption();
jwe.setAlgorithmConstraints(KEY_ENCRYPTION_ALGORITHM_CONSTRAINTS);
jwe.setContentEncryptionAlgorithmConstraints(CONTENT_ENCRYPTION_ALGORITHM_CONSTRAINTS);
jwe.setKey(privateKey);
jwe.setCompactSerialization(responsePayload);
Long iat = jwe.getHeaders().getLongHeaderValue("iat");
System.out.println("iat:" + iat);
decryptedPayload = jwe.getPayload();
System.out.println(decryptedPayload);
} catch (Exception e) {
e.printStackTrace();
}
return decryptedPayload;
}
private String getCredentialStoreKey() {
String result = null;
try {
// get service binding environment variables
final String vcapServicesJson = System.getenv("VCAP_SERVICES");
String USERNAME = JsonPath.read(vcapServicesJson, "$.credstore[0].credentials.username");
String PASSWORD = JsonPath.read(vcapServicesJson, "$.credstore[0].credentials.password");
String URL = JsonPath.read(vcapServicesJson, "$.credstore[0].credentials.url");
String encodedCredentials = Base64.getEncoder().encodeToString((USERNAME + ":" + PASSWORD).getBytes());
final CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet(URL + "/key?name=event-mesh-service-key");
// add request headers
request.addHeader(HttpHeaders.AUTHORIZATION, "Basic " + encodedCredentials);
request.addHeader("sapcp-credstore-namespace", "<CRED_STORE_NAMESPACE>");
try (CloseableHttpResponse response = httpClient.execute(request)) {
// Get HttpResponse Status
System.out.println(response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
if (entity != null) {
// return it as a String
String responseBody = EntityUtils.toString(entity);
System.out.println(responseBody);
result = decryptPayload(responseBody);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
6 | |
5 | |
5 | |
5 | |
5 | |
4 | |
4 | |
4 | |
3 | |
3 |