欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot項目整合jasypt實現(xiàn)過程詳解

 更新時間:2020年08月20日 08:53:34   作者:JaxYoun  
這篇文章主要介紹了SpringBoot項目整合jasypt實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

依賴引入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>

配置參數(shù)application.properties

jasypt.encryptor.password=lE1rl5K$
crypt.user-name=ENC(qvh/QiJYOHNNiJWqhek5Xw==)
crypt.password=ENC(oriTNJoCp5lQ0Tyj5JJmzQ==)
kkk=DEC(123456)

測試代碼

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;
}

測試

瀏覽器訪問【http://localhost:8080/crypt/encrypt】得到加密結(jié)果,并且每次請求結(jié)果不一樣:

{
 "userName":"XsWOwhZIag8XBh3DFl4sqA==",
 "password":"3kD2/u+xnM1i5mO2cVMWKw=="
}

瀏覽器訪問【http://localhost:8080/crypt/decrypt】得到解密結(jié)果,每次請求結(jié)果一樣:

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

## Maven插件用法不可用,后面補充吧......

mvn jasypt:encrypt -Djasypt.encryptor.password="myPass"

可能會報如下錯誤,表示本地環(huán)境未安裝JDK的JCE模塊,它是JDK提供的加密擴展,需要到Oracle官網(wǎng)手動下載,并安裝:

下載地址【https://www.oracle.com/technetwork/cn/java/javase/downloads/jce8-download-2133166-zhs.html

[ERROR] Failed to execute goal com.github.ulisesbocchio:jasypt-maven-plugin:3.0.3:encrypt (default-cli) on project ftp-demo: Error Encrypting: Encryption raised an exception. A possible cause is you are using strong encryption algorithms and you have not installed
the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files in this Java Virtual Machine -> [Help 1]

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Springboot中使用攔截器、過濾器、監(jiān)聽器的流程分析

    Springboot中使用攔截器、過濾器、監(jiān)聽器的流程分析

    Javaweb三大組件:servlet、Filter(過濾器)、?Listener(監(jiān)聽器),這篇文章主要介紹了Springboot中使用攔截器、過濾器、監(jiān)聽器的流程分析,感興趣的朋友跟隨小編一起看看吧
    2023-12-12
  • 最新評論