首页 > 编程语言 > 详细

SpringBoot项目整合jasypt

时间:2020-07-01 19:28:33      阅读:60      评论:0      收藏:0      [点我收藏+]
  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;
}
  1. 测试
    浏览器访问【http://localhost:8080/crypt/encrypt】得到加密结果,并且每次请求结果不一样:
{
  "userName":"XsWOwhZIag8XBh3DFl4sqA==",
  "password":"3kD2/u+xnM1i5mO2cVMWKw=="
}

浏览器访问【http://localhost:8080/crypt/decrypt】得到解密结果,每次请求结果一样:

{
  "userName":"root",
  "password":"root123"
}

更多内容参考:https://www.cnblogs.com/larrydpk/p/12037857.html

SpringBoot项目整合jasypt

原文:https://www.cnblogs.com/JaxYoun/p/13220747.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!