API数据加密框架monkey-api-encrypt

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

原理也很简单,经过Spring提供的RequestBodyAdvice和ResponseBodyAdvice就能够对请求响应作处理。github

原本也是打算更新一下的,由于在Spring Cloud Zuul中也须要加解密,个人那个封装就用不了。算法

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

相比以前的变化:后端

  • 内置AES加密算法,能够配置不一样的加密key
  • 再也不绑定Spring Boot,经过配置Filter便可使用加解密
  • Spring Cloud Zuul框架也能够支持
  • 支持用户自定义加密算法

GitHub地址:https://github.com/yinjihuan/...api

示例代码:https://github.com/yinjihuan/...安全

monkey-api-encrypt没有发布到Maven中央仓库,只发布到jitpack这个仓库,你们也能够自行下载源码打包传到本身公司的私服上。springboot

自动加解密的好处

传统作法以下:微信

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

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

经过使用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

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

猿天地

相关文章
相关标签/搜索