java - 在 tomcat server.xml 中配置 SSL 密码,如 ${keystore.password}

标签 java ssl tomcat8

我想在 Tomcat server.xml 中配置 SSL 密码,如 ${keystore.password}。对于 Tomcat 7,我已经编写了这样做的代码并且运行良好。但在 Tomcat 8 中它不起作用,因为 IntrospectionUtils 正在从 tomcat-coyote.jar 中移出。

如果有人在 Tomcat 8 中做过同样的事情,请告诉我如何进行。

下面是在 Tomcat 7 中运行的代码示例。要运行,请从下面的代码创建一个 jar,然后在 catalina.proerties 中添加两行:

org.apache.tomcat.util.digester.PROPERTY_SOURCE=com.sumit.logic.Decrypt
keystore.password=9Gm4M64FG+JJhNBtk+bUWg==

代码:

package com.comviva.logic;

import java.io.IOException;
import javax.crypto.Cipher;
import org.apache.tomcat.util.IntrospectionUtils;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.comviva.util.Util;

    public class Decrypt implements IntrospectionUtils.PropertySource {

        static Log log = LogFactory.getLog(Decrypt.class);
        static int count =0;
        static String encrypted_password=null;
        private static byte[] key = {
            0x74, 0x68, 0x69, 0x73, 0x49, 0x73, 0x41, 0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x4b, 0x65, 0x79
        };//"thisIsASecretKey";


        public static String decrypt(String strToDecrypt)
        {
            try
            {
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
                final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
                cipher.init(Cipher.DECRYPT_MODE, secretKey);
                final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
                return decryptedString;
            }
            catch (Exception e)
            {
                log.error("Error while decrypting", e);
                return null;
            }

        }

        @Override
        public String getProperty(String arg0) {

            System.out.println("Count is " + count++);

            String fname=this.getClass().getClassLoader().getResource("").getFile();
            fname=fname.substring(0, fname.length()-4);
            String OS = System.getProperty("os.name").toLowerCase();
            if(OS.indexOf("win")>=0)
            {
                log.info("System Type is :"+OS);
                fname=fname.concat("conf\\catalina.properties\n");
                System.out.print("File Name is :"+fname);
            }
            else
            {
                log.info("System Type is :"+OS);
                fname=fname.concat("conf/catalina.properties\n");
                System.out.print("File Name is :"+fname);
            }
            log.info("File is read successfully");
            try {
                Util utility=new Util(fname);
                log.info("Hello");

                encrypted_password=utility.getValue(arg0);
                log.info("ecrypted password is :" + encrypted_password);
                System.out.println("ecrypted password is :" + encrypted_password);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return decrypt(encrypted_password);
        }

    }

最佳答案

通过在 CATALINA_OPTS 中传递以下参数,我能够在 Tomcat 8 中做到这一点

-Dorg.apache.tomcat.util.digester.PROPERTY_SOURCE=com.sumit.logic.Decrypt 

并且 IntrospectionUtils 被移动到“tomcat-util.jar”

关于java - 在 tomcat server.xml 中配置 SSL 密码,如 ${keystore.password},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30598753/

相关文章:

javascript - 如何在 Android 上设置 React Native 导航?

java - 欧拉项目 14 : Issue with array indexing in a novel solution

c# - 如何使用 SslStream 连接进行 ActiveMQ 通信

php - 在 apache AWS 中添加多个 SSL 证书

java - tomcat-embed-core 8.5.4 上缺少 AbstractServletInputStream

java - 如何排除 Jars 被添加到 WAR 文件中

java - 如何在 Jasper 中找出 String[] 的大小

ruby-on-rails - 如何在 Sidekiq 上为 Redis 6 启用 TLS?

适用于 Java 8 的 Tomcat 8 Maven 插件

tomcat - OpenShift 安装 Tomcat 8