zoukankan      html  css  js  c++  java
  • 接口加解密

    /**
     * 这个就是一个标识是否加解密的注解
     */
    @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;
        }
    
    
    }
  • 相关阅读:
    Nginx出现413 Request Entity Too Large错误解决方法
    office 所有后缀对应的 content-type
    swagger2 注解说明
    DOS查看端口占用及杀掉进程命令
    009:Django的项目规范
    008:第一个Django项目剖析(2)
    007:第一个Django项目剖析(1)
    006:Django介绍
    005:课程准备工作
    004:URL组成部分详解
  • 原文地址:https://www.cnblogs.com/java-le/p/11634380.html
Copyright © 2011-2022 走看看