springboot2.2.X手册:防抓包?快速实现API接口数据加密

有没有遇到这样子的接口,放到互联网上面去,谁都可以调用,谁都可以访问,完全就是公开的,这样子的接口,如果只是普通的数据,其实可以考虑,只是可以考虑,但是,一般情况下,我们是不允许这样子做的 。

springboot2.2.X手册:防抓包?快速实现API接口数据加密

文章插图
 
接口安全防什么1、防止恶意调用攻击
2、防止篡改信息攻击
3、防拦截攻击,数据被截取后进行修改后重新放回去
4、防止数据泄漏攻击
 
什么是抓包
抓包(packet capture)就是将网络传输发送与接收的数据包进行截获、重发、编辑、转存等操作,也用来检查网络安全 。抓包也经常被用来进行数据截取等 。
【springboot2.2.X手册:防抓包?快速实现API接口数据加密】这是百度百科给我们的解释,当我们一些放到互联网上的数据,直接采用明文的话,就很容易被抓包,然后进行修改或者被恶意植入木马,这是比较恶心的行为,今天我们就来研究一下怎么样对接口进行数据加密 。
 
POM文件 <!-- springboot核心web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 配置包,用于配置属性文件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><!-- 统一API包 --><dependency><groupId>com.boots</groupId><artifactId>module-boots-api</artifactId><version>2.0.0.RELEASE</version></dependency>编写加密解密工具类/** * All rights Reserved, Designed By 林溪 * Copyright:Copyright(C) 2016-2020 * Company溪云阁 . */package com.module.boots.api.de.utils;import JAVA.security.NoSuchAlgorithmException;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;import org.Apache.Tomcat.util.codec.binary.Base64;import com.module.boots.exception.CommonRuntimeException;/** * AES加密解密 * @author:溪云阁 * @date:2020年6月4日 */public class AesUtils {private static final String KEY_ALGORITHM = "AES";private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";// 默认的加密算法/*** AES 加密操作* @author 溪云阁* @param content 待加密内容* @param password 加密密码* @return String 返回Base64转码后的加密数据*/public static String encrypt(String content, String password) {try {// 创建密码器final Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 设置为UTF-8编码final byte[] byteContent = content.getBytes("utf-8");// 初始化为加密模式的密码器cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));// 加密final byte[] result = cipher.doFinal(byteContent);// 通过Base64转码返回return Base64.encodeBase64String(result);}catch (final Exception ex) {throw new CommonRuntimeException(ex.fillInStackTrace());}}/*** AES 解密操作* @author 溪云阁* @param content* @param password* @return String*/public static String decrypt(String content, String password) {try {// 实例化final Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 使用密钥初始化,设置为解密模式cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));// 执行操作final byte[] result = cipher.doFinal(Base64.decodeBase64(content));// 采用UTF-8编码转化为字符串return new String(result, "utf-8");}catch (final Exception ex) {throw new CommonRuntimeException(ex.fillInStackTrace());}}/*** 生成加密秘钥* @author 溪云阁* @param password 加密的密码* @return SecretKeySpec*/private static SecretKeySpec getSecretKey(final String password) {// 返回生成指定算法密钥生成器的 KeyGenerator 对象KeyGenerator kg = null;try {kg = KeyGenerator.getInstance(KEY_ALGORITHM);// AES 要求密钥长度为 128kg.init(128, new SecureRandom(password.getBytes()));// 生成一个密钥final SecretKey secretKey = kg.generateKey();// 转换为AES专用密钥return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);}catch (final NoSuchAlgorithmException ex) {throw new CommonRuntimeException(ex.fillInStackTrace());}}public static void main(String[] args) {final String str = "V9JofCHn02eyXRiDb1VuseRSuOgEQftROwudMPWwMAO2Wk5K7aYZ4Vtm6xiTn5i5";System.out.println(decrypt(str, "xy934yrn9342u0ry4br8cn-9u2"));}}编写加密注解/** * All rights Reserved, Designed By 林溪 * Copyright:Copyright(C) 2016-2020 * Company溪云阁 . */package com.module.boots.api.de;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 返回对body加密,针对类跟方法 * @author:溪云阁 * @date:2020年6月4日 */@Target({ ElementType.METHOD, ElementType.TYPE })@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface ResponseEncrypt {/*** 返回对body加密,默认是true* @author 溪云阁* @return boolean*/boolean value() default true;}


推荐阅读