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中配置方式)

 
 
  1. @Configuration

  2. public class FilterConfig {


  3.    @Bean

  4.    public FilterRegistrationBean<EncryptionFilter> filterRegistration() {

  5.        EncryptionConfig config = new EncryptionConfig();

  6.        config.setKey("abcdef0123456789");

  7.        config.setRequestDecyptUriList(Arrays.asList("/save", "/decryptEntityXml"));

  8.        config.setResponseEncryptUriList(Arrays.asList("/encryptStr", "/encryptEntity", "/save", "/encryptEntityXml", "/decryptEntityXml"));

  9.        FilterRegistrationBean<EncryptionFilter> registration = new FilterRegistrationBean<EncryptionFilter>();

  10.        registration.setFilter(new EncryptionFilter(config));

  11.        registration.addUrlPatterns("/*");

  12.        registration.setName("EncryptionFilter");

  13.        registration.setOrder(1);

  14.        return registration;

  15.    }


  16. }

  • EncryptionConfig EncryptionConfig是加解密的配置类,配置项目定义如下:

 
 
  1. public class EncryptionConfig {


  2.    /**

  3.     * AES加密Key,长度必须16

  4.     */

  5.    private String key = "d7b85f6e214abcda";


  6.    /**

  7.     * 需要对响应内容进行加密的接口URI<br>

  8.     * 比如:/user/list<br>

  9.     * 不支持@PathVariable格式的URI

  10.     */

  11.    private List<String> responseEncryptUriList = new ArrayList<String>();


  12.    /**

  13.     * 需要对请求内容进行解密的接口URI<br>

  14.     * 比如:/user/list<br>

  15.     * 不支持@PathVariable格式的URI

  16.     */

  17.    private List<String> requestDecyptUriList = new ArrayList<String>();


  18.    /**

  19.     * 响应数据编码

  20.     */

  21.    private String responseCharset = "UTF-8";


  22.    /**

  23.     * 开启调试模式,调试模式下不进行加解密操作,用于像Swagger这种在线API测试场景

  24.     */

  25.    private boolean debug = false;

  26. }

自定义加密算法

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

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

 
 
  1. /**

  2. * 自定义RSA算法

  3. *

  4. * @author yinjihuan

  5. *

  6. * @date 2019-01-12

  7. *

  8. * @about http://cxytiandi.com/about

  9. *

  10. */

  11. public class RsaEncryptAlgorithm implements EncryptAlgorithm {


  12.    public String encrypt(String content, String encryptKey) throws Exception {

  13.        return RSAUtils.encryptByPublicKey(content);

  14.    }


  15.    public String decrypt(String encryptStr, String decryptKey) throws Exception {

  16.        return RSAUtils.decryptByPrivateKey(encryptStr);

  17.    }


  18. }

注册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/");来指定需要处理的接口地址。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31561268/viewspace-2563639/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/31561268/viewspace-2563639/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值