接口加解密

/**
 * 這個就是一個標識是否加解密的註解
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CryptFlag {

}
@Slf4j
@ControllerAdvice
public class CryptResponseBodyAdvice implements ResponseBodyAdvice {


    @Qualifier(value = "initAes")
    @Autowired
    private InitAes initAes;

    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        Method method = returnType.getMethod();
        String url = request.getURI().toASCIIString();
        CryptFlag cryptFlag = method.getAnnotation(CryptFlag.class);
        //有@CryptFlag註解的接口加密,沒有就普通返回

        ObjectMapper mapper = new ObjectMapper();

        String o = null;

        try {

            o = mapper.writeValueAsString(body);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        if (null != cryptFlag) {
            log.info("{}.{},url={},加密數據爲:{},原始數據爲:{}", method.getDeclaringClass().getSimpleName(),
                    method.getName(), url, encrypt(o.toString(), initAes), o.toString());
            return encrypt(o.toString(), initAes);
        } else {
            log.info("{}.{},url={},result={}", method.getDeclaringClass().getSimpleName(), method.getName(), url, body);
            return body;
        }

    }


}
@Slf4j
@ControllerAdvice
public class RequestBodyBefore implements RequestBodyAdvice {

    @Qualifier(value = "initAes")
    @Autowired
    private InitAes initAes;


    @Override
    public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
        return new HttpInputMessage() {
            @Override
            public InputStream getBody() throws IOException {

                //這個裏面進行處理
                //判斷該接口上是否有註解
                Method method = parameter.getMethod();
                CryptFlag cryptFlag = method.getAnnotation(CryptFlag.class);
                String controllerName = parameter.getContainingClass().toString() + parameter.getMethod().getName();
                String json = "";
                //含有加解密的註解
                if (null != cryptFlag) {
                    //得到加密字符串
                    String enCryptStr = IOUtils.toString(inputMessage.getBody());
                    log.info("加密字符串爲:{}", enCryptStr);
                    json = decrypt(enCryptStr, initAes);
                    log.info("解密事後的json字符串爲:{}", json);
                } else {
                    json = IOUtils.toString(inputMessage.getBody());
                    log.info("json字符串爲:{}", json);
                }
                log.info("方法簽名爲:{}", controllerName);
                return new ByteArrayInputStream(json.getBytes(inputMessage.getHeaders().getContentType().getCharset()));

            }

            @Override
            public HttpHeaders getHeaders() {
                return inputMessage.getHeaders();
            }
        };
    }

    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }


}
相關文章
相關標籤/搜索