13

tomcat server.xmlでkeystorePass値を暗号化するオプションはありますか? プレーンテキストにしたくない

    <Connector port="8403" //...
        keystorePass="myPassword" /> 
4

7 に答える 7

16

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の protocol 属性でオーバーライドされたクラスを構成します。

<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>"/>

お役に立てれば。

于 2016-04-12T04:50:18.877 に答える
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;"
/>
于 2014-06-17T08:26:04.943 に答える
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);
    }

}

これは、Tomcat 8 の lib フォルダーにある tomcat-coyote-8.0.29.jar にあるクラス org.apache.coyote.http11.Http11Nio2Protocol を拡張した場所です。 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.9\conf に移動し、server.xml を編集します。

証明書の keytorpasss でハッシュされたパスワードを使用する

<Connector port="9443" protocol="Http11Nio2Protocol" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" 
        keystoreFile="C:\Certificates\SSLCert.cert" keystorePass="nOS74yuWW4s18TsL2UJ51A=="/>

プロトコルがカスタム クラス名であることに注意してください。

これがあなたを助けることを願っています。

ありがとう

于 2016-12-27T09:16:54.123 に答える