Spring配置文件敏感数据加密(3)

        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>


  1. 启动项目指定加密盐:
jasypt:
  encryptor:
    password: \*\*\*\*\*

  1. 进行明文加密

controller加密类

package com.zhqc.cloud.wms.ob.controller;

import groovy.util.logging.Slf4j;
import org.jasypt.encryption.StringEncryptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/\*\*
 \* @author zdd
 \*/
@RestController
@RequestMapping("/encrypted")
@Slf4j
public class EncryptedController {

    private static final Logger log = LoggerFactory.getLogger(EncryptedController.class);
    @Resource
    private StringEncryptor stringEncryptor;

    @GetMapping("/encrypt/{str}")
    public void encrypted(@PathVariable("str") String str) {
        log.info("加密 password: ENC({})", stringEncryptor.encrypt(str));
    }

    @GetMapping("/decrypt/{str}")
    public void decrypted(@PathVariable("str") String str) {
        log.info("解密 password: {}", stringEncryptor.decrypt(str));
    }

## 最后

**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数网络安全工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。**

![img](https://img-blog.csdnimg.cn/img_convert/299c3e23f871d611a6132dd0c46fc77b.png)

![img](https://img-blog.csdnimg.cn/img_convert/1d61495be31de46be1875fb0c3c0c2a6.png)

![img](https://img-blog.csdnimg.cn/img_convert/581ca9c9c7e499db7049044422ea129d.png)

![img](https://img-blog.csdnimg.cn/img_convert/c1f8f042c925a3b17483c84c87a8acff.png)

![img](https://img-blog.csdnimg.cn/img_convert/dda7730331ea863c54af800573116e79.png)

 

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上网络安全知识点!真正的体系化!**

[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618653875)

**由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!**

618653875)

**由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!**

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis-plus提供了加密拦截器`MybatisPlusInterceptor`,可以在查询和修改时对敏感数据进行加密和解密操作。以下是在Spring Boot中配置Mybatis-plus加密拦截器的步骤: 1. 首先,需要在pom.xml文件中添加mybatis-plus-boot-starter依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> ``` 2. 创建一个`MetaObjectHandler`实现类,该类可以在插入和更新时自动填充公共字段,例如创建时间、修改时间等。在该类中,需要实现`insertFill`和`updateFill`方法,并在需要自动填充的字段上添加`@TableField(fill = FieldFill.INSERT)`和`@TableField(fill = FieldFill.UPDATE)`注解。 ```java @Component public class MyMetaObjectHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class); } @Override public void updateFill(MetaObject metaObject) { this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class); } } ``` 3. 创建一个加密拦截器`EncryptInterceptor`,该拦截器可以在查询和修改时对敏感数据进行加密和解密操作。在该拦截器中,需要实现`intercept`方法,并在需要加密的字段上添加`@TableField(fill = FieldFill.INSERT_UPDATE)`注解。 ```java @Component public class EncryptInterceptor implements Interceptor { private static final String AES_KEY = "1234567890123456"; private static final String CHARSET = "UTF-8"; @Override public Object intercept(Invocation invocation) throws Throwable { Object parameter = invocation.getArgs()[1]; if (parameter instanceof MappedStatement && ((MappedStatement) parameter).getId().contains("update")) { MetaObject metaObject = SystemMetaObject.forObject(parameter); String[] propertyNames = metaObject.getSetterNames(); for (String propertyName : propertyNames) { if (metaObject.hasSetter(propertyName) && metaObject.hasGetter(propertyName)) { TableField tableField = metaObject.getOriginalObject().getClass().getDeclaredField(propertyName) .getAnnotation(TableField.class); if (tableField != null && tableField.fill() == FieldFill.UPDATE) { Object value = metaObject.getValue(propertyName); if (value != null && value instanceof String) { metaObject.setValue(propertyName, encrypt((String) value)); } } } } } return invocation.proceed(); } private String encrypt(String content) throws Exception { KeyGenerator keygen = KeyGenerator.getInstance("AES"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(AES_KEY.getBytes()); keygen.init(128, random); SecretKey secretKey = keygen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); byte[] byteContent = content.getBytes(CHARSET); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] byteRresult = cipher.doFinal(byteContent); return Base64.encodeBase64String(byteRresult); } } ``` 4. 在配置文件中配置Mybatis-plus的加密拦截器和自动填充功能: ```yaml mybatis-plus: global-config: meta-object-handler: com.example.demo.handler.MyMetaObjectHandler configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl interceptors: - com.example.demo.interceptor.EncryptInterceptor ``` 5. 在需要加密的字段上添加`@TableField(fill = FieldFill.INSERT_UPDATE)`注解,例如: ```java @TableField(fill = FieldFill.INSERT_UPDATE) private String password; ``` 通过以上步骤,就可以在Spring Boot中配置Mybatis-plus加密拦截器了。注意,在实际应用中,需要对加密算法、加密密钥等进行更加严格的保护。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值