How to import a .cer certificate into a java keystore?

后端 未结 8 928
有刺的猬
有刺的猬 2020-11-22 15:38

During the development of a Java webservice client I ran into a problem. Authentication for the webservice is using a client certificate, a username and a password. The clie

8条回答
  •  感情败类
    2020-11-22 16:30

    Here's a script I used to batch import a bunch of crt files in the current directory into the java keystore. Just save this to the same folder as your certificate, and run it like so:

    ./import_all_certs.sh
    

    import_all_certs.sh

    KEYSTORE="$(/usr/libexec/java_home)/jre/lib/security/cacerts";
    
    function running_as_root()
    {
      if [ "$EUID" -ne 0 ]
        then echo "NO"
        exit
      fi
    
      echo "YES"
    }
    
    function import_certs_to_java_keystore
    {
      for crt in *.crt; do 
        echo prepping $crt 
        keytool -import -file $crt -storepass changeit -noprompt --alias alias__${crt} -keystore $KEYSTORE
        echo 
      done
    }
    
    if [ "$(running_as_root)" == "YES" ]
    then
      import_certs_to_java_keystore
    else
      echo "This script needs to be run as root!"
    fi
    

提交回复
热议问题