猿问

Spring Boot 应用程序不会覆盖特定属性

当我执行我的 Spring Boot 应用程序时,我覆盖了生命周期中一个名为postConstruct的方法,当我想设置一些特定的属性时,例如server.ssl.key-store-password和server.ssl.trust-store-password这里是我的代码:


@PostConstruct

    private void postConstruct() {

        try {

            Map<String, String> encryptedPass = new HashMap<String,String>();


                System.getProperties().setProperty("server.ssl.key-store-password","decryptedpass1");

                System.getProperties().setProperty("server.ssl.trust-store-password","decryptedpass2");

                logger.info("########decryptedpass1 "+System.getProperty("server.ssl.key-store-password")); //return decryptedpass1

                logger.info("########decryptedpass2 "+System.getProperty("server.ssl.trust-store-password"));//return decryptedpass2

            } catch (Exception e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }


            }

控制台上打印得很好,但我有一个例外java.security.UnrecoverableKeyException: Password verification failed!那么我怎样才能覆盖真正的(application.propoerties)


达令说
浏览 140回答 1
1回答

慕斯709654

您必须使用相同的算法来加密/解密密码,为此,这个算法可以为您工作://encrypt password&nbsp;String ALGORITHM = "PBKDF2WithHmacSHA256";&nbsp;String KEYPATH = "/home/apiuser/toto";&nbsp;String SECRET = "SECRET";&nbsp;int ITERATIONCOUNT = 65536;&nbsp;int KEYSIZE = 256;&nbsp;String password = "pass";&nbsp; &nbsp;SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);&nbsp; &nbsp; PBEKeySpec spec = new PBEKeySpec(SECRET.toCharArray(), salt.getBytes(), ITERATIONCOUNT, KEYSIZE);&nbsp; &nbsp; SecretKey secretKey = factory.generateSecret(spec);&nbsp; &nbsp; SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");&nbsp; &nbsp; Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");&nbsp; &nbsp; cipher.init(Cipher.ENCRYPT_MODE, secret);&nbsp; &nbsp; byte[] ivBytes = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();&nbsp; &nbsp; Map<String,String> encryptedPasswords = new HashMap<String,String>();&nbsp; &nbsp; byte[] encryptedTextBytes = cipher.doFinal(password.getBytes("UTF-8"));&nbsp; &nbsp; &nbsp; &nbsp; byte[] finalByteArray = new byte[ivBytes.length + encryptedTextBytes.length];&nbsp; &nbsp; &nbsp; &nbsp; System.arraycopy(ivBytes, 0, finalByteArray, 0, ivBytes.length);&nbsp; &nbsp; &nbsp; &nbsp; System.arraycopy(encryptedTextBytes, 0, finalByteArray, ivBytes.length, encryptedTextBytes.length);&nbsp; &nbsp; &nbsp; &nbsp; String encryptedpass= DatatypeConverter.printBase64Binary(finalByteArray);public static String generateSalt() {&nbsp; &nbsp; &nbsp; &nbsp; return KeyGenerators.string().generateKey();&nbsp; &nbsp; }&nbsp; &nbsp;//decrypt password&nbsp; &nbsp;String ALGORITHM = "PBKDF2WithHmacSHA256";&nbsp; &nbsp; String SECRET = "SECRET";&nbsp; &nbsp; String key = "salt_key";&nbsp; &nbsp; String encryptedPasswords = "encodedpass";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; encryptedPassword = encryptedPasswords.get(key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (encryptedPassword.length() <= IV_LENGTH) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception("The input string is not long enough to contain the initialisation bytes and data.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] byteArray = DatatypeConverter.parseBase64Binary(encryptedPassword);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] ivBytes = new byte[IV_LENGTH];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.arraycopy(byteArray, 0, ivBytes, 0, 16);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] encryptedTextBytes = new byte[byteArray.length - ivBytes.length];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.arraycopy(byteArray, IV_LENGTH, encryptedTextBytes, 0, encryptedTextBytes.length);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PBEKeySpec spec = new PBEKeySpec(SECRET.toCharArray(), key.getBytes(), 65536, 256);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SecretKey secretKey = factory.generateSecret(spec);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] decryptedTextBytes = cipher.doFinal(encryptedTextBytes);&nbsp;
随时随地看视频慕课网APP

相关分类

Java
我要回答