当前位置: 首页 > news >正文

网站首页的功能需求分析购买手表网站

网站首页的功能需求分析,购买手表网站,做公众号需要网站,h5网站模板下载前言#xff1a;     在我们数据库中#xff0c;存在很多的敏感数据#xff0c;如用户表中#xff0c;存在用户电话、身份证号、邮箱等属于用户的敏感信息#xff0c;我们通常在存入数据库后#xff0c;将其进行加密存储#xff0c;以此来保证数据安全性。     …前言     在我们数据库中存在很多的敏感数据如用户表中存在用户电话、身份证号、邮箱等属于用户的敏感信息我们通常在存入数据库后将其进行加密存储以此来保证数据安全性。     加密的方式有很多如摘要加密、对称加密、非对称加密等这里不做描述。因为我们的数据加密存储后查询出来还需解密进行显示我们使用对称加密的方式来实现加密。     常用的对称加密有DES、AES、3DES以及国密算法SM4等。前者我们比较推荐使用AES那么我们这次的对称加密实现将使用Aes以及SM4。     而对于代码来说我们在存储时需要加密查询时又需要解密如果每个方法我们都去这样操作冗余代码会非常的多是非常不合适的。因为我们集成的是Mybatis-plus框架我们可以自定义TypeHandler字段类型处理器来实现我们这一需求。类型处理器TypeHandler扮演着 JavaType 与 JdbcType 之间转换的桥梁角色。它们用于在执行 SQL 语句时将 Java 对象的值设置到 PreparedStatement 中或者从 ResultSet 或 CallableStatement 中取出值。在类型处理器中我们针对特定字段集成加解密的逻辑即可。     本项目代码已上传至gitee Omni-Admin 1. 定义EncryptHandler字段类型处理器。 package com.omni.admin.mybatis;import com.omni.admin.context.SpringContext; import com.omni.admin.encryption.EncryptionTypeEnum; import com.omni.admin.encryption.symmetricalencryption.SymmetricalEncryption; import com.omni.admin.encryption.symmetricalencryption.SymmetricalEncryptionFactory; import com.omni.admin.properties.EncryptionProperties; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;/*** 数据加解密拦截*/ public class EncryptHandler extends BaseTypeHandlerString {Overridepublic void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {ps.setString(i, encrypt(parameter));}Overridepublic String getNullableResult(ResultSet rs, String columnName) throws SQLException {return decrypt(rs.getString(columnName));}Overridepublic String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {return decrypt(rs.getString(columnIndex));}Overridepublic String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {return decrypt(cs.getString(columnIndex));}/*** 加密* */private String encrypt(String params){EncryptionProperties encryptionProperties SpringContext.getBean(EncryptionProperties.class);Boolean enableStorage encryptionProperties.getEnableStorage();if (Boolean.FALSE.equals(enableStorage)) return params;SymmetricalEncryption symmetricalEncryption getSymmetricalEncryption(encryptionProperties.getStorageEncryption());return symmetricalEncryption.encrypt(params);}/*** 解密*/private String decrypt(String params){EncryptionProperties encryptionProperties SpringContext.getBean(EncryptionProperties.class);Boolean enableStorage encryptionProperties.getEnableStorage();if (Boolean.FALSE.equals(enableStorage)) return params;SymmetricalEncryption symmetricalEncryption getSymmetricalEncryption(encryptionProperties.getStorageEncryption());return symmetricalEncryption.decrypt(params);}private SymmetricalEncryption getSymmetricalEncryption(EncryptionTypeEnum encryptionTypeEnum){return SymmetricalEncryptionFactory.getSymmetricalEncryption(encryptionTypeEnum);} } 2. 使用该处理器 我们以用户实体为例当中有电话、身份证号和你邮箱。在TableField注解中加入EncryptHandler类型处理器即可。另外还需在TableName注解中添加autoResultMap true属性这样在查询时才能进行解密。 EqualsAndHashCode(callSuper true) Data TableName(value omni_user_info,autoResultMap true) public class UserInfo extends BaseEntity {/*** 用户电话*/Sensitize(rule SensitizeRuleEnums.MOBILE_PHONE)TableField(value F_PHONE,typeHandler EncryptHandler.class)private String phone;/*** 用户邮箱*/TableField(value F_EMAIL,typeHandler EncryptHandler.class)private String email;/*** 用户身份证*/TableField(value F_ID_CARD,typeHandler EncryptHandler.class)private String idCard;}3. 查看效果 我们可在看到数据库中目前存储的就是密文了。 4. 加密的实现 这里我们考虑到设计以及后续的拓展性我们对称加密的实现将会有多个使用工厂模式来设计编码。 4.1 创建对称机密接口类SymmetricalEncryption 该接口空中提供了加密接口和解密接口由子类去实现。 package com.omni.admin.encryption.symmetricalencryption;public interface SymmetricalEncryption {/*** 加密* param plaintext 明文* param key 密钥* return 加密后的值*/String encrypt(String plaintext, String key);/*** 加密* param plaintext 明文* return 加密后的值*/String encrypt(String plaintext);/*** 解密* param ciphertext 密文* param key 密钥* return 解密后的明文*/String decrypt(String ciphertext, String key);/*** 解密* param ciphertext 密文* return 解密后的明文*/String decrypt(String ciphertext); } 4.2 AES加解密实现SymmetricalEncryptionAes类 maven依赖 dependencygroupIdorg.bouncycastle/groupIdartifactIdbcprov-jdk15on/artifactIdversion1.70/version/dependency4.3 加解密工具类EncryptUtils package com.omni.admin.utils;import com.omni.admin.exception.ExceptionStatus; import com.omni.admin.exception.OmniException; import org.apache.commons.codec.binary.Base64; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory;import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.Security;public class EncryptUtils {static{Security.addProvider(new BouncyCastleProvider());}private static final Logger log LoggerFactory.getLogger(EncryptUtils.class);public static String sm4Encrypt(String plainString, String key) {return encrypt(plainString,key,SM4);}public static String aesEncrypt(String plainString, String key) {return encrypt(plainString,key,AES);}public static String sm4Decrypt(String plainString, String key) {return decrypt(plainString,key,SM4);}public static String aesDecrypt(String plainString, String key) {return decrypt(plainString,key,AES);}private static String encrypt(String plainString, String key,String algorithm) {try {// 创建密钥规范SecretKeySpec secretKeySpec new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), algorithm);// 获取Cipher对象实例BC中SM4默认使用ECB模式和PKCS5Padding填充方式因此下列模式和填充方式无需指定Cipher cipher Cipher.getInstance(algorithm /ECB/PKCS5Padding);// 初始化Cipher为加密模式cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);// 获取加密byte数组byte[] cipherBytes cipher.doFinal(plainString.getBytes(StandardCharsets.UTF_8));// 输出为Base64编码return Base64.encodeBase64String(cipherBytes);} catch (Exception e) {log.error(e.getMessage(), e);throw new OmniException(ExceptionStatus.ENCRYPT_ERROR);}}private static String decrypt(String ciphertext, String key,String algorithm) {try {// 创建密钥规范SecretKeySpec secretKeySpec new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), algorithm);// 获取Cipher对象实例Java8中AES默认使用ECB模式和PKCS5Padding填充方式因此下列模式和填充方式无需指定Cipher cipher Cipher.getInstance(String.format(%s/ECB/PKCS5Padding, algorithm));// 初始化Cipher为解密模式cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);// 获取加密byte数组byte[] cipherBytes cipher.doFinal(Base64.decodeBase64(ciphertext));// 输出为字符串return new String(cipherBytes);} catch (Exception e) {log.error(e.getMessage(), e);throw new OmniException(ExceptionStatus.ENCRYPT_ERROR);}} } package com.omni.admin.encryption.symmetricalencryption; import com.omni.admin.utils.EncryptUtils;public class SymmetricalEncryptionAes implements SymmetricalEncryption {public static final String KEY cf410532f92a47de;Overridepublic String encrypt(String plaintext, String key) {return EncryptUtils.aesEncrypt(plaintext,key);}Overridepublic String encrypt(String plaintext) {return encrypt(plaintext, KEY);}Overridepublic String decrypt(String ciphertext, String key) {return EncryptUtils.aesDecrypt(ciphertext,key);}Overridepublic String decrypt(String ciphertext) {return decrypt(ciphertext, KEY);} }4.4 国密算法SymmetricalEncryptionSM4类 package com.omni.admin.encryption.symmetricalencryption;import com.omni.admin.utils.EncryptUtils;public class SymmetricalEncryptionSM4 implements SymmetricalEncryption {public static final String KEY cf410532f92a47de;Overridepublic String encrypt(String plaintext, String key) {return EncryptUtils.sm4Encrypt(plaintext,key);}Overridepublic String encrypt(String plaintext) {return encrypt(plaintext, KEY);}Overridepublic String decrypt(String ciphertext, String key) {return EncryptUtils.sm4Decrypt(ciphertext,key);}Overridepublic String decrypt(String ciphertext) {return decrypt(ciphertext, KEY);} }4.5 加解密工厂类SymmetricalEncryptionFactory package com.omni.admin.encryption.symmetricalencryption;import com.omni.admin.encryption.EncryptionTypeEnum; import com.omni.admin.exception.ExceptionStatus; import com.omni.admin.exception.OmniException;public class SymmetricalEncryptionFactory {private SymmetricalEncryptionFactory() {}public static SymmetricalEncryption getSymmetricalEncryption(EncryptionTypeEnum encryptionType) {if (EncryptionTypeEnum.AES.equals(encryptionType)){return new SymmetricalEncryptionAes();}if (EncryptionTypeEnum.SM4.equals(encryptionType)){return new SymmetricalEncryptionSM4();}throw new OmniException(ExceptionStatus.NOT_FOUND_SIGNATURE);} } 4.6 枚举类 package com.omni.admin.encryption;import lombok.Getter;Getter public enum EncryptionTypeEnum {MD5(md5),SHA256(sha256),SM3(sm3),AES(AES),SM4(SM4);private final String type;EncryptionTypeEnum(String type) {this.type type;} } 5. 加解密类型的配置化EncryptionProperties package com.omni.admin.properties;import com.omni.admin.encryption.EncryptionTypeEnum; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration;Data Configuration ConfigurationProperties(prefix omni.encryption) public class EncryptionProperties {/*** 是否开启数据签名*/private Boolean enableSign true;/*** 是否开启存储加密*/private Boolean enableStorage true;/*** 签名加密算法*/private EncryptionTypeEnum signAlgorithm EncryptionTypeEnum.SM3;/*** 存储加密算法*/private EncryptionTypeEnum storageEncryption EncryptionTypeEnum.SM4; }
http://www.hkea.cn/news/14386142/

相关文章:

  • 河北省老区建设促进会网站手机百度云电脑版入口
  • 在谷歌上做英文网站asp.net2.0网站开发全程解析 pdf
  • 肇庆市网站建设平台兄弟们资源分享
  • 网络营销网站谷歌搜索引擎免费入口
  • 大连网络营销网站wordpress 留言 插件
  • 网站标准字体正能量视频不打马赛克
  • 徐州英文网站优化2023年电商数据报告
  • 建设银行网站用户注册不了广州新际网站建设
  • 石家庄制作网站的公司哪家好前端性能优化有哪些方法
  • 新建的网站百度搜不到电商设计的理解
  • 织梦建设两个网站 视频php开发企业网站教程
  • 网站建设时如何选择合适的服务器河西做网站公司
  • 网站关键词优化外包服务网络游戏网站开发
  • 乐清柳市网站建设公司网络连接服务
  • 最好的网站设计公腾讯云改wordpress
  • 58同城通辽做网站wordpress弹幕视频插件
  • 网站项目策划书方案帝国cms做招聘网站
  • 南海网站制作公司宁波网络推广咨询
  • 网站建设公司天成培训美工设计师
  • 新网站seo外包家庭做网站
  • 昆明 网站搭建电商网站开发 知乎
  • 网站建设的前端用什么编程自己怎么接单做网站
  • 上海网站建设的asp.net 项目成本预算系统的 网站开发
  • vs2010网站制作教程佛山网站建站
  • 个人网站免费空间网站增值业务
  • 郴州网站建设哪家比较好怎么购买网站空间
  • 网站建设哪家公司最好骏域网络科技有限公司
  • 大连哪里做网站好福建网站建设价格
  • 网站建设分为哪几种无锡网络优化推广公司
  • 为什么网络经营者要有自己的网站网站开发人员结构