zoukankan      html  css  js  c++  java
  • API数据加密框架monkey-api-encrypt

    之前有写过一篇加密的文章《前后端API交互如何保证数据安全性》。主要是在Spring Boot中如何对接口的数据进行自动加解密操作,通过注解的方式来指定是否需要加解密。

    原理也很简单,通过Spring提供的RequestBodyAdvice和ResponseBodyAdvice就可以对请求响应做处理。

    本来也是打算更新一下的,因为在Spring Cloud Zuul中也需要加解密,我的那个封装就用不了。

    恰巧上周肥朝大佬跟我聊了下,提供了一些非常有用的建议,于是周六花了一天时间重构了一下加密的框架,不再以Spring Boot Starter的方式提供服务,直接是一个jar包,基于Servlet层面来对数据进行加解密处理。

    相比之前的变化:

    • 内置AES加密算法,可以配置不同的加密key
    • 不再绑定Spring Boot,通过配置Filter即可使用加解密
    • Spring Cloud Zuul框架也可以支持
    • 支持用户自定义加密算法

    GitHub地址:https://github.com/yinjihuan/monkey-api-encrypt

    示例代码:https://github.com/yinjihuan/monkey-api-encrypt/tree/master/encrypt-springboot-example

    monkey-api-encrypt没有发布到Maven中央仓库,只发布到jitpack这个仓库,大家也可以自行下载源码打包传到自己公司的私服上。

    自动加解密的好处

    传统做法如下:

    // 客户端传来的数据就是加密好的字符串
    public String add(String data) {
       // 1. 通过工具类将数据解密,然后序列化成对象使用
       // 2. 处理业务逻辑,数据返回的时候用工具类将数据加密返回给客户端
    }
    

    缺点是在每个业务方法中都要手动的去处理加解密的逻辑。

    通过使用monkey-api-encrypt的话可以让开发人员不需要关注加解密的逻辑,比如:

    @PostMapping("/save")
    public UserResult add(@RequestBody User data) {
        UserResult  result = new UserResult ();
        result.setXXX....
        return result;
    }
    

    上面的代码跟平常写的一模一样,没有加解密的逻辑,需要对数据做加解密逻辑的时候,只需要配置一个过滤器,然后指定哪些URI需要加解密即可。下面来学习下如何使用monkey-api-encrypt。

    快速使用

    下面以jitpack仓库示列

    第一步:pom.xml中增加仓库地址

    <repositories>
      <repository>
         <id>jitpack.io</id>
         <url>https://jitpack.io</url>
      </repository>
    </repositories>
    

    第二步:增加项目依赖

    <dependency>
        <groupId>com.github.yinjihuan</groupId>
        <artifactId>monkey-api-encrypt</artifactId>
        <version>1.1.1</version>
    </dependency>
    

    第三步:配置加解密过滤器(Spring Boot中配置方式)

    @Configuration
    public class FilterConfig {
    
        @Bean
        public FilterRegistrationBean<EncryptionFilter> filterRegistration() {
        	EncryptionConfig config = new EncryptionConfig();
        	config.setKey("abcdef0123456789");
        	config.setRequestDecyptUriList(Arrays.asList("/save", "/decryptEntityXml"));
        	config.setResponseEncryptUriList(Arrays.asList("/encryptStr", "/encryptEntity", "/save", "/encryptEntityXml", "/decryptEntityXml"));
            FilterRegistrationBean<EncryptionFilter> registration = new FilterRegistrationBean<EncryptionFilter>();
            registration.setFilter(new EncryptionFilter(config));
            registration.addUrlPatterns("/*");
            registration.setName("EncryptionFilter");
            registration.setOrder(1);
            return registration;
        }
    
    }
    
    • EncryptionConfig
      EncryptionConfig是加解密的配置类,配置项目定义如下:
    public class EncryptionConfig {
    
    	/**
    	 * AES加密Key,长度必须16
    	 */
    	private String key = "d7b85f6e214abcda";
    	
    	/**
    	 * 需要对响应内容进行加密的接口URI<br>
    	 * 比如:/user/list<br>
    	 * 不支持@PathVariable格式的URI
    	 */
    	private List<String> responseEncryptUriList = new ArrayList<String>();
    	
    	/**
    	 * 需要对请求内容进行解密的接口URI<br>
    	 * 比如:/user/list<br>
    	 * 不支持@PathVariable格式的URI
    	 */
    	private List<String> requestDecyptUriList = new ArrayList<String>();
    
    	/**
    	 * 响应数据编码
    	 */
    	private String responseCharset = "UTF-8";
    	
    	/**
    	 * 开启调试模式,调试模式下不进行加解密操作,用于像Swagger这种在线API测试场景
    	 */
    	private boolean debug = false;
    }
    

    自定义加密算法

    内置了AES加密算法对数据进行加解密操作,同时用户可以自定义算法来代替内置的算法。

    自定义算法需要实现EncryptAlgorithm接口:

    /**
     * 自定义RSA算法
     * 
     * @author yinjihuan
     * 
     * @date 2019-01-12
     * 
     * @about http://cxytiandi.com/about
     *
     */
    public class RsaEncryptAlgorithm implements EncryptAlgorithm {
    
    	public String encrypt(String content, String encryptKey) throws Exception {
    		return RSAUtils.encryptByPublicKey(content);
    	}
    
    	public String decrypt(String encryptStr, String decryptKey) throws Exception {
    		return RSAUtils.decryptByPrivateKey(encryptStr);
    	}
    
    }
    

    注册Filter的时候指定算法:

    EncryptionConfig config = new EncryptionConfig();
    registration.setFilter(new EncryptionFilter(config, new RsaEncryptAlgorithm()));
    

    常见问题

    1. Spring Cloud Zuul中如何使用?

    使用方式和Spring Boot中一样,没区别。

    2. 如果需要所有请求都做加解密处理怎么办?

    默认不配置RequestDecyptUriList和ResponseEncryptUriList的情况下,就会对所有请求进行处理(拦截器指定范围内的请求)

    3. Swagger测试接口的时候怎么处理?

    可以开启调试模式,就不对请求做加解密处理,通过配置debug=true

    4. RequestDecyptUriList和ResponseEncryptUriList能否支持/user/*模式匹配?

    过滤器本身就有这个功能了,所以框架中是完全匹配相等才可以,可以通过过滤器的 registration.addUrlPatterns("/user/","/order/");来指定需要处理的接口地址。

    欢迎加入我的知识星球,一起交流技术,免费学习猿天地的课程(http://cxytiandi.com/course)

    微信扫码加入猿天地知识星球

    猿天地

  • 相关阅读:
    相机标定 和 单应性矩阵H
    全景视频拼接关键技术
    OpenCV图像变换(仿射变换与透视变换)
    用非数学语言讲解贝叶斯定理
    (二)SMO算法
    (一)SVM原理
    什么是赋范线性空间、内积空间,度量空间,希尔伯特空间
    决策树算法(五)——处理一些特殊的分类
    V8学习资源
    perforce 学习资源
  • 原文地址:https://www.cnblogs.com/yinjihuan/p/10464255.html
Copyright © 2011-2022 走看看