加密tomcat密钥库密码

问题描述 投票:0回答:4

是否有一个选项可以加密 tomcat server.xml中的 keystorePass 值? 我不希望它是纯文本

    <Connector port="8403" //...
        keystorePass="myPassword" /> 
tomcat server.xml
4个回答
17
投票

有一种比仅使用 XML 编码更好的方法。

创建一个加密类来加密和解密您的密码。

并覆盖 Http11Nio2Protocol 类,类似于下面的代码。

 public class Http11Nio2Protocol extends org.apache.coyote.http11.Http11Nio2Protocol {

@Override
public void setKeystorePass(String s) {
    try {
        super.setKeystorePass(new EncryptService().decrypt(s));
    } catch (final Exception e){
        super.setKeystorePass("");
    }
}

}

注意:EncryptService是我们自己的加密类。

并在 server.xml 的协议属性中配置覆盖的类,如下所示。

<Connector port="8443" protocol="<com.mypackage.overridden_Http11Nio2Protocol_class>"
           maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
           clientAuth="false" sslProtocol="TLS" 
          keystoreFile="conf/.ssl/keystore.jks"        
           keystorePass="<encrypted_password>"/>

希望这有帮助。


12
投票

如果有人可以访问您的 server.xml,出现的 keystorePass 的纯文本值只是您的担忧之一。

如果有人可以从那里访问,他们可能会造成更大的伤害。在这里加密密码实际上只是将问题转移到其他地方,因为这样就有人可以找到该加密密钥的加密密钥(有点像俄罗斯娃娃)。

如果你想加密密码,你必须覆盖Connector 实现解密加密密码,以便真正的密码是 tomcat 可以访问或可用。


10
投票

面临同样的问题。客户要求“隐藏”所有密码。

因此,通过审核的最简单方法 - 来自 Tomcat Wiki

转到页面 http://coderstoolbox.net/string/#!encoding=xml&action=encode&charset=none 并对传递到 XML 视图的进行编码。

因此 -

<Connector>
元素看起来像:

<Connector
  port="8443"
  protocol="HTTP/1.1"
  SSLEnabled="true"
  enableLookups="false"
  disableUploadTimeout="true"
  scheme="https"
  secure="true"
  clientAuth="want"
  sslProtocol="TLS"
  keystoreFile="conf/.ssl/keystore.jks"
  keyAlias="tomcat"
  keystorePass="&#99;&#104;&#105;&#107;&#115;"
  truststoreFile="conf/.ssl/trustedstore.jks"
  truststorePass="&#99;&#104;&#105;&#107;&#115;"
/>

2
投票

我们也面临类似的问题,但我们创建了自己的加密和解密逻辑来解决这个问题。这是代码

/* class is used to generate encrypted password */

public class ClientForPasswordGeneration {

    public static void main(String[] args) {
        //final String secretKey = "ssshhhhhhhhhhh!!!!";
        final String secretKey = PasswordKey.getEncryptionKey();
        GenerateLogic object = new GenerateLogic();

        String password = PasswordField.readPassword("Enter password: ");

        String encryptPassword = object.encrypt(password, secretKey);
        System.out.println("Encrypted Password:");
        System.out.println(encryptPassword);

    }

}

又一堂课

class EraserThread implements Runnable {
    private boolean stop;

    /**
     * @param The
     *            prompt displayed to the user
     */
    public EraserThread(String prompt) {
        System.out.print(prompt);
    }

    /**
     * Begin masking...display asterisks (*)
     */
    public void run() {
        stop = true;
        while (stop) {

            System.out.print("\010*");

            try {

                Thread.currentThread().sleep(1);
                // System.out.println("current thread::" + Thread.currentThread());
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }
        }
    }

    /**
     * Instruct the thread to stop masking
     */
    public void stopMasking() {
        this.stop = false;
    }

}

生成哈希代码的逻辑

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class GenerateLogic {
    private static SecretKeySpec secretKey;
    private static byte[] key;

    public static void setKey(String myKey) {
        MessageDigest sha = null;
        try {
            key = myKey.getBytes("UTF-8");
            sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key, 16);
            secretKey = new SecretKeySpec(key, "AES");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static String encrypt(String strToEncrypt, String secret) {
        try {
            setKey(secret);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
        } catch (Exception e) {
            System.out.println("Error while encrypting: " + e.toString());
        }
        return null;
    }

    public static String decrypt(String strToDecrypt) {
        try {
            //System.out.println("decryptedString methods");
            //String secret = "ssshhhhhhhhhhh!!!!";
            String secret = PasswordKey.getEncryptionKey();
            setKey(secret);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            //System.out.println("testing string values::" + new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))));
            return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
        } catch (Exception e) {
            System.out.println("Error while decrypting: " + e.toString());
        }
        return null;
    }

    public static void main(String[] args) {
        final String secretKey = "ssshhhhhhhhhhh!!!!";

        String originalString = "changeit";
        String encryptedString = GenerateLogic.encrypt(originalString, secretKey);
        String decryptedString = GenerateLogic.decrypt(encryptedString);

        System.out.println(originalString);
        System.out.println(encryptedString);
        System.out.println(decryptedString);
    }

}

这是我们扩展类 org.apache.coyote.http11.Http11Nio2Protocol 的地方,该类存在于 tomcat-coyote-8.0.29.jar 中,该 jar 存在于 tomcat 8 的 lib 文件夹中。因此,在编译这些类 tomcat-coyote-8.0 时.29.jar 应该存在。

public class Http11Nio2Protocol extends org.apache.coyote.http11.Http11Nio2Protocol {

    @Override
    public void setKeystorePass(String s) {
        try {
            super.setKeystorePass(new GenerateLogic().decrypt(s));
        } catch (final Exception e) {
            super.setKeystorePass("");
        }
    }

}

这是用户必须在 cmd 中输入密码的地方,该密码应该被散列

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PasswordField {

    /**
     * @param prompt
     *            The prompt to display to the user
     * @return The password as entered by the user
     */
    public static String readPassword(String prompt) {
        EraserThread et = new EraserThread(prompt);
        Thread mask = new Thread(et);
        mask.start();

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String password = "";

        try {
            password = in.readLine();

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        // stop masking
        et.stopMasking();
        // return the password entered by the user
        return password;
    }
}

这是您保存密码密钥的地方。你应该改变它。

public class PasswordKey {

    private static String ENCRYPTION_KEY = "myKeysecretkey";

    protected static String getEncryptionKey()
    {
        return ENCRYPTION_KEY;
    }

}

在cmd中使用以下命令编译上述类以生成类文件。请记住 tomcat-coyote-8.0.29.jar 应位于所有 java 文件所在的同一文件夹中。

javac  -cp ".;tomcat-coyote-8.0.29.jar" *.java

在cmd中使用此命令使用生成的类文件制作一个jar

jar -cvf  PasswordEncryptor.jar  *.class

这将创建一个 jar 文件PasswordEncryptor.jar

将生成的PasswordEncryptor.jar粘贴到Tomcat8的lib文件夹中。即 apache-tomcat-8.5.9\lib

现在转到此位置并输入以下命令以生成哈希密码。

java -cp ".;PasswordEncryptor.jar" ClientForPasswordGeneration

现在转到 apache-tomcat-8.5。

© www.soinside.com 2019 - 2024. All rights reserved.