Reputation: 5
I am pretty new to making api calls. I have been given a java class that generates token. I have been asked to build a web service that makes api calls to the SecurityUtil .calculateAuthorizationSignature(fields, clientId, clientSecret) method using those
parameters instead of having them hard coded as shown in the class below:
public class SecurityUtil { public static void main(String[] args) {
String[] fields = new String[3];
// POC+100+05QQAWQERQWHYTFDYUSwY
fields[0] = "POC";
fields[1] = "100";
fields[2] = "05QQAWQERQWHYTFDYUSwY2";
String clientId = "dfaaa525-704c-41f4-9d95-7983f9bee18d";
String clientSecret = "6r9186uxrt031lw0diivck9noma1onfq";
String signatureStr = new SecurityUtil()
.calculateAuthorizationSignature(fields, clientId, clientSecret);
System.out.println(signatureStr);
}
public String encodeBase64(String val) {
return Base64.getEncoder().encodeToString(val.getBytes());
}
public String decodeBase64(String val) throws UnsupportedEncodingException {
return new String(Base64.getDecoder().decode(val), "ASCII");
}
public String hmacSha256(String val, String key) {
return new HmacUtils(HmacAlgorithms.HMAC_SHA_256, key).hmacHex(val);
}
public String calculateAuthorizationSignature(String[] fields, String id, String secret) {
StringBuilder sb = new StringBuilder();
boolean addSeparator = false;
for (String s : fields) {
if (addSeparator) {
sb.append("+");
}
sb.append(s);
addSeparator = true;
}
String serverSignature = hmacSha256(sb.toString(), secret);
String clientId = encodeBase64(id);
Instant instant = Instant.now();
Long timeStampMillis = instant.getEpochSecond();
String timeStamp = encodeBase64(String.valueOf(timeStampMillis));
String cipher = serverSignature + "." + timeStamp + "." + clientId;
return encodeBase64(cipher);
}
}
am using spring boot and my pom file is shown below:
https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE com.abelinho.securityutil secutildemo 0.0.1-SNAPSHOT secutildemo Demo project for Spring Boot
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
and my project structure is as shown below:
Kindly assist. Thanks guys!
Upvotes: 0
Views: 379
Reputation: 556
To make API call you need to use any http client like RestTemplate or FeignClient using rest template you can call API,
fields[0] = "POC";
fields[1] = "100";
fields[2] = "05QQAWQERQWHYTFDYUSwY2";
String clientId = "dfaaa525-704c-41f4-9d95-7983f9bee18d";
String clientSecret = "6r9186uxrt031lw0diivck9noma1onfq";
public String copyAssests(String clientId , String clientSecret, String[] fields) {
return restTemplate.exchange("url", HttpMethod.POST, getHttpEntity(request, null, appCode), String.class, fields).getBody();
}
private <T> HttpEntity<T> getHttpEntity(T t, String authorization, String appCode) {
HttpHeaders headers = new HttpHeaders();
headers.add("header", "value");
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
return new HttpEntity<>(t, headers);
}
Upvotes: 0