JD-220: Add logging aspect (#25)

* JD-220: Add logging aspect

* JD-220: Fix checkstyle issue

* JD-220: Fix typo
This commit is contained in:
Alexander Romanov 2021-04-01 17:44:22 +03:00 committed by GitHub
parent ef04864747
commit 8b5b891c6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 118 additions and 5 deletions

View File

@ -12,7 +12,7 @@
</parent>
<artifactId>adapter-bank-spring-boot-starter</artifactId>
<version>0.0.18</version>
<version>0.0.19</version>
<packaging>jar</packaging>
<name>adapter-bank-spring-boot-starter</name>
<description>Spring boot starter for bank adapter</description>
@ -110,6 +110,11 @@
<version>${spring-boot.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!--rbk-->
<dependency>

View File

@ -0,0 +1,22 @@
package com.rbkmoney.adapter.bank.spring.boot.starter.logging.annotation;
import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.RequestMethod;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface GetLogging {
@AliasFor("value")
String endpoint() default "/";
@AliasFor("endpoint")
String value() default "/";
RequestMethod method() default RequestMethod.GET;
}

View File

@ -0,0 +1,22 @@
package com.rbkmoney.adapter.bank.spring.boot.starter.logging.annotation;
import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.RequestMethod;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface PostLogging {
@AliasFor("value")
String endpoint() default "/";
@AliasFor("endpoint")
String value() default "/";
RequestMethod method() default RequestMethod.POST;
}

View File

@ -0,0 +1,61 @@
package com.rbkmoney.adapter.bank.spring.boot.starter.logging.aspect;
import com.rbkmoney.adapter.bank.spring.boot.starter.logging.annotation.GetLogging;
import com.rbkmoney.adapter.bank.spring.boot.starter.logging.annotation.PostLogging;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMethod;
@Slf4j
@Aspect
@Configuration
public class RequestLoggingAspect {
private static final String REQUEST_LOG = "Request [{} {}]: {}";
private static final String RESPONSE_LOG = "Response [{} {}]: {}";
@Around("@annotation(com.rbkmoney.adapter.bank.spring.boot.starter.logging.annotation.GetLogging)")
public Object logGetRequest(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
GetLogging getLogging = AnnotationUtils.findAnnotation(signature.getMethod(), GetLogging.class);
RequestMethod method = getLogging.method();
String endpoint = getLogging.value();
return log(joinPoint, method, endpoint);
}
@Around("@annotation(com.rbkmoney.adapter.bank.spring.boot.starter.logging.annotation.PostLogging)")
public Object logPostRequest(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
PostLogging postLogging = AnnotationUtils.findAnnotation(signature.getMethod(), PostLogging.class);
RequestMethod method = postLogging.method();
String endpoint = postLogging.value();
return log(joinPoint, method, endpoint);
}
private Object log(
ProceedingJoinPoint joinPoint,
RequestMethod method,
String endpoint) throws Throwable {
if (joinPoint.getArgs().length != 1) {
log.debug("Unable to log request. Unsupported method signature with more than one argument: {}",
joinPoint.getArgs());
return joinPoint.proceed();
}
Object request = joinPoint.getArgs()[0];
log.info(REQUEST_LOG, method, endpoint, request);
Object response = joinPoint.proceed();
log.info(RESPONSE_LOG, method, endpoint, response);
return response;
}
}

View File

@ -3,4 +3,5 @@ com.rbkmoney.adapter.bank.spring.boot.starter.configuration.AppConfiguration,\
com.rbkmoney.adapter.bank.spring.boot.starter.configuration.SerializerConfiguration,\
com.rbkmoney.adapter.bank.spring.boot.starter.configuration.DeserializerConfiguration,\
com.rbkmoney.adapter.bank.spring.boot.starter.controller.AdapterControllerDecorator,\
com.rbkmoney.adapter.bank.spring.boot.starter.service.ThreeDsPropertiesServiceImpl
com.rbkmoney.adapter.bank.spring.boot.starter.service.ThreeDsPropertiesServiceImpl,\
com.rbkmoney.adapter.bank.spring.boot.starter.logging.aspect.RequestLoggingAspect

View File

@ -1,16 +1,18 @@
package com.rbkmoney.adapter.bank.spring.boot.starter.utils;
import com.rbkmoney.adapter.common.utils.encryption.HmacEncryption;
import java.security.NoSuchAlgorithmException;
import org.junit.Assert;
import org.junit.Test;
import java.security.NoSuchAlgorithmException;
public class SignGeneratorTest {
@Test
public void sign() throws NoSuchAlgorithmException {
String test =
"511.483USD677144616IT Books. Qty: 217Books Online Inc.14www.sample.com1512345678901234589999999919pgw@mail.sample.com11--142003010515302116F2B2DD7E603A7ADA33https://www.sample.com/shop/reply";
String test = "511.483USD677144616IT Books. Qty: 217Books Online Inc.14" +
"www.sample.com1512345678901234589999999919pgw@mail.sample.com11--" +
"142003010515302116F2B2DD7E603A7ADA33https://www.sample.com/shop/reply";
String s = HmacEncryption.calculateHMacSha1(test, "00112233445566778899AABBCCDDEEFF");
Assert.assertEquals("FACC882CA67E109E409E3974DDEDA8AAB13A5E48", s.toUpperCase());