当前位置: 首页 > 知识库问答 >
问题:

JASYPT与Spring Boot问题加密强口令并在应用程序中使用

华鹭洋
2023-03-14

使用Jasypt的pom.xml如下所示:

 <!--Jasypt-->

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

        </dependency>

我的Main有以下注释:

@EnableEncryptableProperties

我的数据库配置类:配置

@PropertySource("classpath:application.yml")
@PropertySource("classpath:external.properties")
//@EncryptablePropertySource("classpath:external.properties")
//@EncryptablePropertySource("classpath:application.yml")
@EnableTransactionManagement
public class DBConfiguration {

    @Value("${spring.datasource.pps-db.driver-class-name}")
    private String driver;

    @Value("${spring.datasource.pps-db.password}")
    private String password;
java -cp ~/.m2/repository/org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="Aa123456"  password=secret algorithm=PBEWithMD5AndDES

----ENVIRONMENT-----------------

Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 11.0.5+10-LTS 



----ARGUMENTS-------------------

input: Aa123456
password: secret
algorithm: PBEWithMD5AndDES

----OUTPUT----------------------

c+E+8syd2Y1Tp1oNkJ2Xqk/9Pqt9l92B
java -cp ~/.m2/repository/org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="A12$$T@@!"  password=secret algorithm=PBEWithMD5AndDESA1
-**bash: !": event not found**
import org.jasypt.util.text.AES256TextEncryptor;

import java.security.NoSuchAlgorithmException;

    public class JasyptPasswordEcryptor {
        public static void main(String[] args) throws NoSuchAlgorithmException {

            String password = "A12$$T@@!";


            AES256TextEncryptor encryptor = new AES256TextEncryptor();

            encryptor.setPassword("secret");
            String myEncryptedText = encryptor.encrypt(password);
            System.out.println("Encrypted: "+myEncryptedText);

            String plainText = encryptor.decrypt(myEncryptedText);
            System.out.println("Decrypted: "+plainText);
        }
    }

结果是:加密:QGPenan7MPKRC4OPCHN8ZTSMOIX8IMX0Pt/Hi7X6YVDTE/BIS/PTAID76TEJ6TG解密:A12$$T@@!

当我在我的to应用程序中应用我的新加密密码时。yml:password:ENC(QGPenan7mpkrc4opchn8ztsmoix8imx0pt/hi7x6yvdte/bis/ptaaid76tej6tg)

然后我尝试运行我的应用程序(mvn spring-boot:run):我得到一个异常…我不明白问题是什么..(我认为这与java.security,,,没有AES256TextEncryptor算法或类似的东西有关)

Caused by: org.springframework.cache.Cache$ValueRetrievalException: Value for key 'spring.datasource.pps-db.password' could not be loaded using 'com.ulisesbocchio.jasyptspringboot.caching.CachingDelegateEncryptablePropertySource$$Lambda$400/0x0000000800721440@2dec0f40'
    at org.springframework.cache.concurrent.ConcurrentMapCache.lambda$get$0 (ConcurrentMapCache.java:149)
    at java.util.concurrent.ConcurrentHashMap.computeIfAbsent (ConcurrentHashMap.java:1705)
    at org.springframework.cache.concurrent.ConcurrentMapCache.get (ConcurrentMapCache.java:144)
    at com.ulisesbocchio.jasyptspringboot.caching.CachingDelegateEncryptablePropertySource.getProperty (CachingDelegateEncryptablePropertySource.java:34)
    at com.ulisesbocchio.jasyptspringboot.wrapper.EncryptableMapPropertySourceWrapper.getProperty (EncryptableMapPropertySourceWrapper.java:31)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty (PropertySourcesPropertyResolver.java:85)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty (PropertySourcesPropertyResolver.java:62)
    at org.springframework.core.env.AbstractEnvironment.getProperty (AbstractEnvironment.java:539)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty (PropertySourcesPlaceholderConfigurer.java:137)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty (PropertySourcesPlaceholderConfigurer.java:133)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty (PropertySourcesPropertyResolver.java:85)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getPropertyAsRawString (PropertySourcesPropertyResolver.java:74)
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue (PropertyPlaceholderHelper.java:151)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders (PropertyPlaceholderHelper.java:124)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders (AbstractPropertyResolver.java:237)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders (AbstractPropertyResolver.java:211)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0 (PropertySourcesPlaceholderConfigurer.java:175)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue (AbstractBeanFactory.java:851)
  at java.lang.Thread.run (Thread.java:834)
Caused by: org.springframework.cache.Cache$ValueRetrievalException: Value for key 'spring.datasource.pps-db.password' could not be loaded using 'com.ulisesbocchio.jasyptspringboot.caching.CachingDelegateEncryptablePropertySource$$Lambda$401/0x000000080071f840@618d748'
    at org.springframework.cache.concurrent.ConcurrentMapCache.lambda$get$0 (ConcurrentMapCache.java:149)

  at java.lang.reflect.Method.invoke (Method.java:566)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:543)
    at java.lang.Thread.run (Thread.java:834)
Caused by: java.lang.IllegalStateException: either 'jasypt.encryptor.password' or one of ['jasypt.encryptor.private-key-string', 'jasypt.encryptor.private-key-location'] must be provided for Password-based or Asymmetric encryption
    at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.lambda$null$5 (DefaultLazyEncryptor.java:54)
    at java.util.Optional.orElseThrow (Optional.java:408)
    at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.lambda$createDefault$6 (DefaultLazyEncryptor.java:54)
    at java.util.Optional.orElseGet (Optional.java:369)
    at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.createDefault (DefaultLazyEncryptor.java:50)
    at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.lambda$null$2 (DefaultLazyEncryptor.java:38)
    at java.util.Optional.orElseGet (O

谢谢大家,

共有1个答案

淳于博
2023-03-14

我想分享我的ipnut。

尝试加密密码时,请使用以下方法

尝试用“!”封装字符串时,,,请使用'yourpassword!!!‘

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

</dependency>
 类似资料:
  • 本文向大家介绍在SpringBoot中通过jasypt进行加密解密的方法,包括了在SpringBoot中通过jasypt进行加密解密的方法的使用技巧和注意事项,需要的朋友参考一下 1.用途 在SpringBoot中,通过jasypt可以进行加密解密. 这个是双向的, 且可以配置密钥. 2.使用: 2.1通过UT创建工具类,并认识jasypt 可以看出, 每次生成的密码是不一样的, 但是通过密钥,可

  • 我需要:基于密码的加密使用sha512消化和256位AES加密与cbc和bc标志设置。 看来算法应该是这样的:PBEWithSHA512AndAES256-CBC-BC 但是在我的本地环境中,我得到错误:org.jasypt.exceptions.encryptioninitializationexception:java.security.nosuchalgorithmexception:没有这

  • 我是kubernetes的新手,需要在openshift平台上使用k8s confimap将springboot应用程序的属性文件外部化。我已将属性文件保存在git repo中,作为“greeter.message=Spring Bootmyapplication.properties已在库伯内特斯上挂载为卷!”并使用“oc create confimap myconfig--from-file=

  • 问题内容: 寻找一种在node中加密数据(主要是字符串)并在android应用(java)中解密的方法。 在每个节点中都成功做到了这一点(在节点中进行加密/解密,在Java中进行加密/解密),但是似乎无法使其在它们之间起作用。 可能我不是以相同的方式进行加密/解密,但是每种语言的每个库对于相同的事物都有不同的名称… 任何帮助表示赞赏。 这是一些代码:Node.js 和java 原始密钥是这样创建的

  • 我试图在SpringMVC中运行SpringBoot应用程序,在SpringMVCPOM中添加SpringBoot应用程序依赖项,并扫描SpringBoot包,但我面临以下问题