分享

SpringBoot项目整合jasypt

 昵称70747151 2020-07-03
  1. 依赖引入pom.xml
<!-- jasypt核心依赖 -->
<dependency>
   <groupId>com.github.ulisesbocchio</groupId>
   <artifactId>jasypt-spring-boot-starter</artifactId>
   <version>2.1.1</version> <!-- jasypt2.1.1与spring-boot2.2.6的兼容性是最好的,避免踩坑,泪呀 -->
</dependency>

<!-- jasypt-maven插件,不影响基本功能 -->
<plugin>
  <groupId>com.github.ulisesbocchio</groupId>
  <artifactId>jasypt-maven-plugin</artifactId>
  <version>3.0.3</version>
</plugin>
  1. 配置参数application.properties
jasypt.encryptor.password=lE1rl5K$
crypt.user-name=ENC(qvh/QiJYOHNNiJWqhek5Xw==)
crypt.password=ENC(oriTNJoCp5lQ0Tyj5JJmzQ==)
kkk=DEC(123456)
  1. 测试代码
package com.yang.ftpdemo.controller;

import lombok.Data;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

@RestController
@RequestMapping("/crypt")
public class CryptController {

    @Resource
    private StringEncryptor encrypt;

    @Resource
    private CryptConfig cryptConfig;

    @GetMapping("/encrypt")
    public CryptConfig encrypt() {
        String username = encrypt.encrypt("root");
        String password = encrypt.encrypt("root123");

        CryptConfig crypt = new CryptConfig();
        crypt.setPassword(password);
        crypt.setUserName(username);
        return crypt;
    }

    @GetMapping("/decrypt")
    public CryptConfig decrypt() {
        CryptConfig crypt = new CryptConfig();
        BeanUtils.copyProperties(this.cryptConfig, crypt);
        return crypt;
    }
}

@Data
@Configuration
@ConfigurationProperties(prefix = "crypt")
class CryptConfig {
    private String userName;
    private String password;
}
语言 方法
5184 0yUOt
g6cYq
  • 开早餐店赚钱吗「百年老店」老板分享经验
  • 6229 2005/05/20 04:53:36

      本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
      转藏 分享 献花(0

      0条评论

      发表

      请遵守用户 评论公约

      类似文章 更多