1. 程式人生 > 實用技巧 >SpringBoot專案整合jasypt

SpringBoot專案整合jasypt

  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("/decrypt")
    public CryptConfig decrypt() {
        String username = encrypt.encrypt("root");
        String password = encrypt.encrypt("root123");
        CryptConfig cryptConfig = new CryptConfig();
        cryptConfig.setPassword(password);
        cryptConfig.setUserName(username);
        return cryptConfig;
    }

    @GetMapping("/encrypt")
    public CryptConfig encrypt() {
        return this.cryptConfig;
    }
}

@Data
@Configuration
@ConfigurationProperties(prefix = "crypt")
class CryptConfig {

    private String userName;

    private String password;

}

更多東西可以參考:https://www.cnblogs.com/larrydpk/p/12037857.html