mirror of
https://gitee.com/dromara/MaxKey.git
synced 2025-12-07 17:38:32 +08:00
Resource fix
Resource fix LOG4J2 PatternLayout debug change
This commit is contained in:
parent
c80205883f
commit
166b8362d3
@ -1,3 +1,4 @@
|
||||
package org.maxkey.crypto.jose.keystore;
|
||||
/*******************************************************************************
|
||||
* Copyright 2014 The MITRE Corporation
|
||||
* and the MIT Kerberos and Internet Trust Consortium
|
||||
@ -14,29 +15,26 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
******************************************************************************/
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.maxkey.crypto.jose.keystore;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.text.ParseException;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.CharStreams;
|
||||
import com.nimbusds.jose.jwk.JWK;
|
||||
import com.nimbusds.jose.jwk.JWKSet;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.text.ParseException;
|
||||
import java.util.List;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* .
|
||||
* @author jricher
|
||||
*
|
||||
*/
|
||||
public class JWKSetKeyStore {
|
||||
|
||||
private static final Logger _logger = LoggerFactory.getLogger(JWKSetKeyStore.class);
|
||||
private JWKSet jwkSet;
|
||||
|
||||
private Resource location;
|
||||
@ -58,22 +56,26 @@ public class JWKSetKeyStore {
|
||||
if (location.exists() && location.isReadable()) {
|
||||
|
||||
try {
|
||||
_logger.debug("JWK location " + location.getURL());
|
||||
// read in the file from disk
|
||||
String s = CharStreams.toString(new InputStreamReader(location.getInputStream(), Charsets.UTF_8));
|
||||
String s = CharStreams
|
||||
.toString(new InputStreamReader(location.getInputStream(), Charsets.UTF_8));
|
||||
|
||||
// parse it into a jwkSet object
|
||||
jwkSet = JWKSet.parse(s);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("Key Set resource could not be read: " + location);
|
||||
} catch (ParseException e) {
|
||||
throw new IllegalArgumentException("Key Set resource could not be parsed: " + location); }
|
||||
throw new IllegalArgumentException("Key Set resource could not be parsed: " + location);
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new IllegalArgumentException("Key Set resource could not be read: " + location);
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new IllegalArgumentException("Key store must be initialized with at least one of a jwkSet or a location.");
|
||||
throw new IllegalArgumentException(
|
||||
"Key store must be initialized with at least one of a jwkSet or a location.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -105,11 +107,13 @@ public class JWKSetKeyStore {
|
||||
*/
|
||||
public void setLocation(Resource location) {
|
||||
this.location = location;
|
||||
|
||||
initializeJwkSet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of keys in this keystore. This is a passthrough to the underlying JWK Set
|
||||
* Get the list of keys in this keystore. This is a passthrough to the
|
||||
* underlying JWK Set
|
||||
*/
|
||||
public List<JWK> getKeys() {
|
||||
if (jwkSet == null) {
|
||||
@ -118,6 +122,4 @@ public class JWKSetKeyStore {
|
||||
return jwkSet.getKeys();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,33 +1,34 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
package org.maxkey.crypto.keystore;
|
||||
|
||||
import java.security.KeyStore;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
|
||||
|
||||
/**
|
||||
* .
|
||||
* @author Crystal.Sea
|
||||
*
|
||||
*/
|
||||
public class KeyStoreLoader implements InitializingBean {
|
||||
private final static Logger _logger = LoggerFactory.getLogger(KeyStoreLoader.class);
|
||||
private static final Logger _logger = LoggerFactory.getLogger(KeyStoreLoader.class);
|
||||
|
||||
private KeyStore keyStore;
|
||||
|
||||
private String entityName;
|
||||
private String keystoreFile;
|
||||
private Resource keystoreFile;
|
||||
private String keystorePassword;
|
||||
|
||||
private String keystoreType = "JKS";
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@ -44,12 +45,10 @@ public class KeyStoreLoader implements InitializingBean{
|
||||
/**
|
||||
* @param keystoreFile the keystoreFile to set
|
||||
*/
|
||||
public void setKeystoreFile(String keystoreFile) {
|
||||
public void setKeystoreFile(Resource keystoreFile) {
|
||||
this.keystoreFile = keystoreFile;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param keystorePassword the keystorePassword to set
|
||||
*/
|
||||
@ -59,6 +58,7 @@ public class KeyStoreLoader implements InitializingBean{
|
||||
|
||||
/**
|
||||
* <EFBFBD><EFBFBD>ȡKeyStore<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getKeystorePassword() {
|
||||
@ -67,8 +67,10 @@ public class KeyStoreLoader implements InitializingBean{
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
_logger.debug("Load KeyStore from file "+ResourceUtils.getFile(keystoreFile).getPath());
|
||||
keyStore =KeyStoreUtil.loadKeyStore(ResourceUtils.getFile(keystoreFile), keystorePassword.toCharArray(), KeyStoreType.JKS);
|
||||
_logger.debug("Load KeyStore from file " + keystoreFile.getURL());
|
||||
keyStore = KeyStoreUtil.loadKeyStore(
|
||||
keystoreFile, keystorePassword.toCharArray(),
|
||||
KeyStoreType.JKS);
|
||||
_logger.debug("Load KeyStore success . ");
|
||||
|
||||
Enumeration<String> temp = keyStore.aliases();
|
||||
@ -78,9 +80,8 @@ public class KeyStoreLoader implements InitializingBean{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* .
|
||||
* @return the entityName
|
||||
*/
|
||||
public String getEntityName() {
|
||||
@ -101,5 +102,4 @@ public class KeyStoreLoader implements InitializingBean{
|
||||
return keystoreType;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -4,7 +4,6 @@
|
||||
|
||||
package org.maxkey.crypto.keystore;
|
||||
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
@ -12,6 +11,7 @@ import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.Key;
|
||||
import java.security.KeyPair;
|
||||
@ -39,12 +39,12 @@ import org.maxkey.crypto.Base64Utils;
|
||||
import org.maxkey.crypto.cert.CryptoException;
|
||||
import org.maxkey.crypto.cert.X509CertUtils;
|
||||
import org.maxkey.crypto.cert.X509V3CertGen;
|
||||
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* Provides utility methods for loading/saving keystores. The Bouncy Castle provider must be registered before
|
||||
* using this class to create or load BKS or UBER type keystores.
|
||||
* Provides utility methods for loading/saving keystores. The Bouncy Castle
|
||||
* provider must be registered before using this class to create or load BKS or
|
||||
* UBER type keystores.
|
||||
*/
|
||||
public final class KeyStoreUtil {
|
||||
|
||||
@ -56,8 +56,7 @@ public final class KeyStoreUtil{
|
||||
/**
|
||||
* Private to prevent construction.
|
||||
*/
|
||||
private KeyStoreUtil()
|
||||
{
|
||||
private KeyStoreUtil() {
|
||||
// Nothing to do
|
||||
}
|
||||
|
||||
@ -68,33 +67,27 @@ public final class KeyStoreUtil{
|
||||
* @return The keystore
|
||||
* @throws KeyStoreException No implementation found
|
||||
*/
|
||||
private static KeyStore getKeyStoreImpl(KeyStoreType keyStoreType)throws KeyStoreException
|
||||
{
|
||||
private static KeyStore getKeyStoreImpl(KeyStoreType keyStoreType) throws KeyStoreException {
|
||||
KeyStore keyStore = null;
|
||||
if (keyStoreType == KeyStoreType.PKCS12)
|
||||
{
|
||||
// Prefer BC for PKCS #12 for now; the BC and SunJSSE 1.5+ implementations are incompatible in how
|
||||
// they handle empty/missing passwords; BC works consistently with char[0] on load and store (does
|
||||
// not accept nulls), SunJSSE throws division by zero with char[0] on load and store, works with
|
||||
if (keyStoreType == KeyStoreType.PKCS12) {
|
||||
// Prefer BC for PKCS #12 for now; the BC and SunJSSE 1.5+ implementations are
|
||||
// incompatible in how
|
||||
// they handle empty/missing passwords; BC works consistently with char[0] on
|
||||
// load and store (does
|
||||
// not accept nulls), SunJSSE throws division by zero with char[0] on load and
|
||||
// store, works with
|
||||
// null on load, does not work with null on store.
|
||||
// Checked with BC 1.{29,40}, SunJSSE 1.5.0_0{3,4,14}, 1.6.0 (OpenJDK)
|
||||
try
|
||||
{
|
||||
try {
|
||||
keyStore = KeyStore.getInstance(keyStoreType.name(), "BC");
|
||||
}
|
||||
catch (NoSuchProviderException ex)
|
||||
{
|
||||
} catch (NoSuchProviderException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (keyStore == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (keyStore == null) {
|
||||
try {
|
||||
keyStore = KeyStore.getInstance(keyStoreType.name());
|
||||
}
|
||||
catch (KeyStoreException e)
|
||||
{
|
||||
} catch (KeyStoreException e) {
|
||||
AVAILABLE_TYPES.put(keyStoreType, Boolean.FALSE);
|
||||
throw e;
|
||||
}
|
||||
@ -111,105 +104,82 @@ public final class KeyStoreUtil{
|
||||
* @throws CryptoException Problem encountered creating the keystore
|
||||
* @throws IOException An I/O error occurred
|
||||
*/
|
||||
public static KeyStore createKeyStore(KeyStoreType keyStoreType)
|
||||
throws CryptoException, IOException
|
||||
{
|
||||
public static KeyStore createKeyStore(KeyStoreType keyStoreType) throws CryptoException, IOException {
|
||||
KeyStore keyStore = null;
|
||||
try
|
||||
{
|
||||
try {
|
||||
keyStore = getKeyStoreImpl(keyStoreType);
|
||||
keyStore.load(null, null);
|
||||
}
|
||||
catch (GeneralSecurityException ex)
|
||||
{
|
||||
throw new CryptoException(
|
||||
"Could not create "+ keyStoreType+" keystore.", ex);
|
||||
} catch (GeneralSecurityException ex) {
|
||||
throw new CryptoException("Could not create " + keyStoreType + " keystore.", ex);
|
||||
}
|
||||
return keyStore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load keystore entries from PEM reader into a new PKCS #12 keystore. The reader is not closed.
|
||||
* Load keystore entries from PEM reader into a new PKCS #12 keystore. The
|
||||
* reader is not closed.
|
||||
*
|
||||
* @param reader reader to read entries from
|
||||
* @return new PKCS #12 keystore containing read entries, possibly empty
|
||||
* @throws CryptoException Problem encountered creating the keystore
|
||||
* @throws IOException An I/O error occurred
|
||||
*/
|
||||
public static KeyStore loadEntries(PEMParser reader,String password)throws CryptoException, IOException
|
||||
{
|
||||
public static KeyStore loadEntries(PEMParser reader, String password) throws CryptoException, IOException {
|
||||
LinkedHashSet<KeyPair> keyPairs = new LinkedHashSet<KeyPair>();
|
||||
LinkedHashSet<Certificate> certs = new LinkedHashSet<Certificate>();
|
||||
KeyStore keyStore = createKeyStore(KeyStoreType.PKCS12);
|
||||
|
||||
Object obj;
|
||||
while ((obj = reader.readObject()) != null)
|
||||
{
|
||||
if (obj instanceof KeyPair)
|
||||
{
|
||||
while ((obj = reader.readObject()) != null) {
|
||||
if (obj instanceof KeyPair) {
|
||||
keyPairs.add((KeyPair) obj);
|
||||
}
|
||||
else if (obj instanceof Certificate)
|
||||
{
|
||||
} else if (obj instanceof Certificate) {
|
||||
certs.add((Certificate) obj);
|
||||
}
|
||||
}
|
||||
|
||||
// Add key pairs
|
||||
for (KeyPair keyPair : keyPairs)
|
||||
{
|
||||
for (KeyPair keyPair : keyPairs) {
|
||||
Certificate keyPairCert = null;
|
||||
for (Iterator<Certificate> it = certs.iterator(); it.hasNext();)
|
||||
{
|
||||
for (Iterator<Certificate> it = certs.iterator(); it.hasNext();) {
|
||||
Certificate cert = it.next();
|
||||
if (cert.getPublicKey().equals(keyPair.getPublic()))
|
||||
{
|
||||
if (cert.getPublicKey().equals(keyPair.getPublic())) {
|
||||
keyPairCert = cert;
|
||||
it.remove();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (keyPairCert != null)
|
||||
{
|
||||
if (keyPairCert != null) {
|
||||
String alias = "keypair";
|
||||
if (keyPairCert instanceof X509Certificate)
|
||||
{
|
||||
if (keyPairCert instanceof X509Certificate) {
|
||||
alias = X509CertUtils.getCertificateAlias((X509Certificate) keyPairCert);
|
||||
}
|
||||
|
||||
KeyStore.PrivateKeyEntry entry =
|
||||
new KeyStore.PrivateKeyEntry(keyPair.getPrivate(), new Certificate[] { keyPairCert });
|
||||
KeyStore.PrivateKeyEntry entry = new KeyStore.PrivateKeyEntry(keyPair.getPrivate(),
|
||||
new Certificate[] { keyPairCert });
|
||||
KeyStore.PasswordProtection prot = new KeyStore.PasswordProtection(password.toCharArray());
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
alias = findUnusedAlias(keyStore, alias);
|
||||
keyStore.setEntry(alias, entry, prot);
|
||||
}
|
||||
catch (KeyStoreException e)
|
||||
{
|
||||
} catch (KeyStoreException e) {
|
||||
throw new CryptoException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add remaining certificates as trusted certificate entries
|
||||
for (Certificate cert : certs)
|
||||
{
|
||||
for (Certificate cert : certs) {
|
||||
String alias = "certificate";
|
||||
if (cert instanceof X509Certificate)
|
||||
{
|
||||
if (cert instanceof X509Certificate) {
|
||||
alias = X509CertUtils.getCertificateAlias((X509Certificate) cert);
|
||||
}
|
||||
|
||||
KeyStore.TrustedCertificateEntry entry = new KeyStore.TrustedCertificateEntry(cert);
|
||||
try
|
||||
{
|
||||
try {
|
||||
keyStore.setEntry(alias, entry, null);
|
||||
}
|
||||
catch (KeyStoreException e)
|
||||
{
|
||||
} catch (KeyStoreException e) {
|
||||
throw new CryptoException(e);
|
||||
}
|
||||
}
|
||||
@ -223,20 +193,15 @@ public final class KeyStoreUtil{
|
||||
* @param keyStoreType the keystore type
|
||||
* @return true if the keystore type is available, false otherwise
|
||||
*/
|
||||
public static boolean isAvailable(KeyStoreType keyStoreType)
|
||||
{
|
||||
public static boolean isAvailable(KeyStoreType keyStoreType) {
|
||||
Boolean available;
|
||||
if ((available = AVAILABLE_TYPES.get(keyStoreType)) != null)
|
||||
{
|
||||
if ((available = AVAILABLE_TYPES.get(keyStoreType)) != null) {
|
||||
return available;
|
||||
}
|
||||
try
|
||||
{
|
||||
try {
|
||||
// Populate AVAILABLE_TYPES
|
||||
getKeyStoreImpl(keyStoreType);
|
||||
}
|
||||
catch (KeyStoreException e)
|
||||
{
|
||||
} catch (KeyStoreException e) {
|
||||
// Ignore
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -248,15 +213,12 @@ public final class KeyStoreUtil{
|
||||
*
|
||||
* @return available keystore types
|
||||
*/
|
||||
public static KeyStoreType[] getAvailableTypes()
|
||||
{
|
||||
public static KeyStoreType[] getAvailableTypes() {
|
||||
// TODO: populate only once
|
||||
KeyStoreType[] known = KeyStoreType.values();
|
||||
ArrayList<KeyStoreType> available = new ArrayList<KeyStoreType>();
|
||||
for (KeyStoreType type : known)
|
||||
{
|
||||
if (isAvailable(type))
|
||||
{
|
||||
for (KeyStoreType type : known) {
|
||||
if (isAvailable(type)) {
|
||||
available.add(type);
|
||||
}
|
||||
}
|
||||
@ -271,47 +233,33 @@ public final class KeyStoreUtil{
|
||||
* @param cKeyStorePassword Password of the keystore
|
||||
* @return The keystore
|
||||
* @throws CryptoException Problem encountered loading the keystore
|
||||
* @throws FileNotFoundException If the keystore file does not exist, is a directory rather than a regular
|
||||
* file, or for some other reason cannot be opened for reading
|
||||
* @throws FileNotFoundException If the keystore file does not exist, is a
|
||||
* directory rather than a regular file, or for
|
||||
* some other reason cannot be opened for reading
|
||||
*/
|
||||
public static KeyStore loadKeyStore(File fKeyStore, char[] cKeyStorePassword, KeyStoreType keyStoreType)
|
||||
throws CryptoException, FileNotFoundException
|
||||
{
|
||||
public static KeyStore loadKeyStore(
|
||||
File fKeyStore, char[] cKeyStorePassword, KeyStoreType keyStoreType)
|
||||
throws CryptoException, FileNotFoundException {
|
||||
KeyStore keyStore = null;
|
||||
try
|
||||
{
|
||||
try {
|
||||
keyStore = getKeyStoreImpl(keyStoreType);
|
||||
}
|
||||
catch (KeyStoreException ex)
|
||||
{
|
||||
} catch (KeyStoreException ex) {
|
||||
throw new CryptoException("Could not create " + keyStoreType + " keystore.", ex);
|
||||
}
|
||||
|
||||
FileInputStream fis = new FileInputStream(fKeyStore);
|
||||
try
|
||||
{
|
||||
try {
|
||||
keyStore.load(fis, cKeyStorePassword);
|
||||
}
|
||||
catch (GeneralSecurityException ex)
|
||||
{
|
||||
} catch (GeneralSecurityException ex) {
|
||||
throw new CryptoException("Could not load keystore as type ''" + keyStoreType + "''.", ex);
|
||||
}
|
||||
catch (FileNotFoundException ex)
|
||||
{
|
||||
} catch (FileNotFoundException ex) {
|
||||
throw ex;
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
} catch (IOException ex) {
|
||||
throw new CryptoException("Could not load keystore as type ''" + keyStoreType + "''.", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
} finally {
|
||||
try {
|
||||
fis.close();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
} catch (IOException ex) {
|
||||
// Ignore
|
||||
ex.printStackTrace();
|
||||
}
|
||||
@ -320,7 +268,39 @@ public final class KeyStoreUtil{
|
||||
return keyStore;
|
||||
}
|
||||
|
||||
public static KeyStore loadKeyStore(String keyStoreB64Encoded, char[] cKeyStorePassword, KeyStoreType keyStoreType){
|
||||
public static KeyStore loadKeyStore(
|
||||
Resource keystoreFile, char[] cKeyStorePassword, KeyStoreType keyStoreType)
|
||||
throws CryptoException, IOException {
|
||||
KeyStore keyStore = null;
|
||||
try {
|
||||
keyStore = getKeyStoreImpl(keyStoreType);
|
||||
} catch (KeyStoreException ex) {
|
||||
throw new CryptoException("Could not create " + keyStoreType + " keystore.", ex);
|
||||
}
|
||||
|
||||
InputStream fis = keystoreFile.getInputStream();
|
||||
try {
|
||||
keyStore.load(fis, cKeyStorePassword);
|
||||
} catch (GeneralSecurityException ex) {
|
||||
throw new CryptoException("Could not load keystore as type ''" + keyStoreType + "''.", ex);
|
||||
} catch (FileNotFoundException ex) {
|
||||
throw ex;
|
||||
} catch (IOException ex) {
|
||||
throw new CryptoException("Could not load keystore as type ''" + keyStoreType + "''.", ex);
|
||||
} finally {
|
||||
try {
|
||||
fis.close();
|
||||
} catch (IOException ex) {
|
||||
// Ignore
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return keyStore;
|
||||
}
|
||||
|
||||
public static KeyStore loadKeyStore(String keyStoreB64Encoded, char[] cKeyStorePassword,
|
||||
KeyStoreType keyStoreType) {
|
||||
KeyStore keyStore;
|
||||
try {
|
||||
//
|
||||
@ -364,6 +344,7 @@ public final class KeyStoreUtil{
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static KeyStore base642KeyStore(String keyStoreBase64, String password) {
|
||||
byte[] keyStoreByte = Base64Utils.decoderBase64(keyStoreBase64);
|
||||
return bytes2KeyStore(keyStoreByte, "JKS", password);
|
||||
@ -377,39 +358,29 @@ public final class KeyStoreUtil{
|
||||
* @return The keystore
|
||||
* @throws CryptoException Problem encountered loading the keystore
|
||||
*/
|
||||
public static KeyStore loadKeyStore(String sPkcs11Provider, char[] cKeyStorePassword)
|
||||
throws CryptoException
|
||||
{
|
||||
public static KeyStore loadKeyStore(String sPkcs11Provider, char[] cKeyStorePassword) throws CryptoException {
|
||||
KeyStore keyStore = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (Security.getProvider(sPkcs11Provider) == null)
|
||||
{
|
||||
try {
|
||||
if (Security.getProvider(sPkcs11Provider) == null) {
|
||||
throw new CryptoException("The ''" + sPkcs11Provider + "'' provider is not present.");
|
||||
}
|
||||
keyStore = KeyStore.getInstance(KeyStoreType.PKCS11.name(), sPkcs11Provider);
|
||||
}
|
||||
catch (GeneralSecurityException ex)
|
||||
{
|
||||
throw new CryptoException(
|
||||
"Could not create "+KeyStoreType.PKCS11+" keystore.", ex);
|
||||
} catch (GeneralSecurityException ex) {
|
||||
throw new CryptoException("Could not create " + KeyStoreType.PKCS11 + " keystore.", ex);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
keyStore.load(null, cKeyStorePassword);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
} catch (Exception ex) {
|
||||
throw new CryptoException("Could not load keystore as type ''" + KeyStoreType.PKCS11 + "''.", ex);
|
||||
}
|
||||
|
||||
return keyStore;
|
||||
}
|
||||
|
||||
|
||||
/** *//**
|
||||
/** */
|
||||
/**
|
||||
* <p>
|
||||
* get a Certificate from keyStore
|
||||
* </p>
|
||||
@ -429,7 +400,8 @@ public final class KeyStoreUtil{
|
||||
return certificate;
|
||||
}
|
||||
|
||||
/** *//**
|
||||
/** */
|
||||
/**
|
||||
* <p>
|
||||
* 根据密钥库获得私<EFBFBD>?
|
||||
* </p>
|
||||
@ -440,15 +412,13 @@ public final class KeyStoreUtil{
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static PrivateKey getPrivateKey(KeyStore keyStore, String alias, String password)
|
||||
throws Exception {
|
||||
public static PrivateKey getPrivateKey(KeyStore keyStore, String alias, String password) throws Exception {
|
||||
PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
|
||||
return privateKey;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** *//**
|
||||
/** */
|
||||
/**
|
||||
* <p>
|
||||
* 根据证书获得公钥
|
||||
* </p>
|
||||
@ -461,7 +431,8 @@ public final class KeyStoreUtil{
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
/** *//**
|
||||
/** */
|
||||
/**
|
||||
* <p>
|
||||
* 获得证书
|
||||
* </p>
|
||||
@ -470,8 +441,7 @@ public final class KeyStoreUtil{
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static Certificate loadCertificateFromFile(String certificatePath)
|
||||
throws Exception {
|
||||
public static Certificate loadCertificateFromFile(String certificatePath) throws Exception {
|
||||
CertificateFactory certificateFactory = CertificateFactory.getInstance(X509);
|
||||
FileInputStream in = new FileInputStream(certificatePath);
|
||||
Certificate certificate = certificateFactory.generateCertificate(in);
|
||||
@ -479,7 +449,8 @@ public final class KeyStoreUtil{
|
||||
return certificate;
|
||||
}
|
||||
|
||||
/** *//**
|
||||
/** */
|
||||
/**
|
||||
* <p>
|
||||
* 根据密钥库获得证<EFBFBD>?
|
||||
* </p>
|
||||
@ -490,15 +461,14 @@ public final class KeyStoreUtil{
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static Certificate getCertificate(KeyStore keyStore, String alias, String password)
|
||||
throws Exception {
|
||||
public static Certificate getCertificate(KeyStore keyStore, String alias, String password) throws Exception {
|
||||
Certificate certificate = keyStore.getCertificate(alias);
|
||||
return certificate;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* import X509Certificate trustCert to keyStore
|
||||
*
|
||||
* @param keyStore
|
||||
* @param trustCert
|
||||
* @return KeyStore
|
||||
@ -510,11 +480,13 @@ public final class KeyStoreUtil{
|
||||
sMatchAlias = X509CertUtils.matchCertificate(keyStore, trustCert);
|
||||
System.out.println("sMatchAlias : " + sMatchAlias);
|
||||
if (sMatchAlias != null) {
|
||||
System.out.println("The certificate already exists in the Keystore under alias ''"+sMatchAlias+"''.\nDo you still want to import it?");
|
||||
System.out.println("The certificate already exists in the Keystore under alias ''" + sMatchAlias
|
||||
+ "''.\nDo you still want to import it?");
|
||||
} else {
|
||||
KeyStore[] keyStores = { keyStore };
|
||||
if (X509CertUtils.establishTrust(keyStores, trustCert) == null) {
|
||||
System.out.println("Could not establish a trust path for the certficate.\nThe certficate information will now be displayed after\nwhich you may confirm whether or not you trust the\ncertificate.");
|
||||
System.out.println(
|
||||
"Could not establish a trust path for the certficate.\nThe certficate information will now be displayed after\nwhich you may confirm whether or not you trust the\ncertificate.");
|
||||
}
|
||||
|
||||
String sCertAlias = X509CertUtils.getCertificateAlias(trustCert).toLowerCase();
|
||||
@ -537,6 +509,7 @@ public final class KeyStoreUtil{
|
||||
|
||||
/**
|
||||
* import X509Certificate trustCert to keyStore
|
||||
*
|
||||
* @param keyStore
|
||||
* @param trustCert
|
||||
* @return KeyStore
|
||||
@ -548,11 +521,13 @@ public final class KeyStoreUtil{
|
||||
sMatchAlias = X509CertUtils.matchCertificate(keyStore, trustCert);
|
||||
System.out.println("sMatchAlias : " + sMatchAlias);
|
||||
if (sMatchAlias != null) {
|
||||
System.out.println("The certificate already exists in the Keystore under alias ''"+sMatchAlias+"''.\nDo you still want to import it?");
|
||||
System.out.println("The certificate already exists in the Keystore under alias ''" + sMatchAlias
|
||||
+ "''.\nDo you still want to import it?");
|
||||
} else {
|
||||
KeyStore[] keyStores = { keyStore };
|
||||
if (X509CertUtils.establishTrust(keyStores, trustCert) == null) {
|
||||
System.out.println("Could not establish a trust path for the certficate.\nThe certficate information will now be displayed after\nwhich you may confirm whether or not you trust the\ncertificate.");
|
||||
System.out.println(
|
||||
"Could not establish a trust path for the certficate.\nThe certficate information will now be displayed after\nwhich you may confirm whether or not you trust the\ncertificate.");
|
||||
}
|
||||
|
||||
// Delete old entry first
|
||||
@ -580,36 +555,31 @@ public final class KeyStoreUtil{
|
||||
* @param cKeyStorePassword The password to protect the keystore with
|
||||
* @return the saved keystore ready for further use
|
||||
* @throws CryptoException Problem encountered saving the keystore
|
||||
* @throws FileNotFoundException If the keystore file exists but is a directory rather than a regular
|
||||
* file, does not exist but cannot be created, or cannot be opened for any other reason
|
||||
* @throws FileNotFoundException If the keystore file exists but is a directory
|
||||
* rather than a regular file, does not exist but
|
||||
* cannot be created, or cannot be opened for any
|
||||
* other reason
|
||||
* @throws IOException An I/O error occurred
|
||||
*/
|
||||
public static KeyStore saveKeyStore(KeyStore keyStore, File fKeyStoreFile, char[] cKeyStorePassword)
|
||||
throws CryptoException, IOException
|
||||
{
|
||||
throws CryptoException, IOException {
|
||||
FileOutputStream fos = new FileOutputStream(fKeyStoreFile);
|
||||
try
|
||||
{
|
||||
try {
|
||||
keyStore.store(fos, cKeyStorePassword);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
} catch (IOException ex) {
|
||||
throw new CryptoException("Could not save keystore.", ex);
|
||||
}
|
||||
catch (GeneralSecurityException ex)
|
||||
{
|
||||
} catch (GeneralSecurityException ex) {
|
||||
throw new CryptoException("Could not save keystore.", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
} finally {
|
||||
fos.close();
|
||||
}
|
||||
|
||||
// As of GNU classpath 0.92, we need to reload GKR keystores after storing them, otherwise
|
||||
// "masked envelope" IllegalStateExceptions occur when trying to access things in the stored keystore
|
||||
// As of GNU classpath 0.92, we need to reload GKR keystores after storing them,
|
||||
// otherwise
|
||||
// "masked envelope" IllegalStateExceptions occur when trying to access things
|
||||
// in the stored keystore
|
||||
// again.
|
||||
if (KeyStoreType.valueOf(keyStore.getType()) == KeyStoreType.GKR)
|
||||
{
|
||||
if (KeyStoreType.valueOf(keyStore.getType()) == KeyStoreType.GKR) {
|
||||
keyStore = loadKeyStore(fKeyStoreFile, cKeyStorePassword, KeyStoreType.GKR);
|
||||
}
|
||||
|
||||
@ -624,17 +594,12 @@ public final class KeyStoreUtil{
|
||||
* @return alias that is not in use in the keystore
|
||||
* @throws KeyStoreException
|
||||
*/
|
||||
public static String findUnusedAlias(KeyStore keyStore, String alias)
|
||||
throws KeyStoreException
|
||||
{
|
||||
if (keyStore.containsAlias(alias))
|
||||
{
|
||||
public static String findUnusedAlias(KeyStore keyStore, String alias) throws KeyStoreException {
|
||||
if (keyStore.containsAlias(alias)) {
|
||||
int i = 1;
|
||||
while (true)
|
||||
{
|
||||
while (true) {
|
||||
String nextAlias = alias + " (" + i + ")";
|
||||
if (!keyStore.containsAlias(nextAlias))
|
||||
{
|
||||
if (!keyStore.containsAlias(nextAlias)) {
|
||||
alias = nextAlias;
|
||||
break;
|
||||
}
|
||||
@ -642,6 +607,7 @@ public final class KeyStoreUtil{
|
||||
}
|
||||
return alias;
|
||||
}
|
||||
|
||||
public static KeyStore clone(KeyStore keyStore, String password) {
|
||||
try {
|
||||
KeyStore cloneKeyStore = KeyStore.getInstance(keyStore.getType());
|
||||
@ -700,14 +666,14 @@ public final class KeyStoreUtil{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static KeyStore setKeyEntry(KeyStore keyStore,String keyEntry,KeyPair keyPair,Certificate certificate,String password) throws KeyStoreException{
|
||||
public static KeyStore setKeyEntry(KeyStore keyStore, String keyEntry, KeyPair keyPair, Certificate certificate,
|
||||
String password) throws KeyStoreException {
|
||||
Certificate[] certChain = new Certificate[1];
|
||||
certChain[0] = certificate;
|
||||
keyStore.setKeyEntry(keyEntry, (Key) keyPair.getPrivate(), password.toCharArray(), certChain);
|
||||
return keyStore;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
|
||||
@ -732,91 +698,103 @@ public final class KeyStoreUtil{
|
||||
}
|
||||
/**
|
||||
* @param args
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// new
|
||||
// ByteArrayInputStream(Base64.decodeBase64(b64EncodedKeystore.getBytes()))
|
||||
|
||||
try {
|
||||
// load a keyStore
|
||||
File file = new File("C:\\cert\\idp-keystore.jks");
|
||||
String keystorePassword = "secret";
|
||||
KeyStore ks = KeyStoreUtil.loadKeyStore(file,
|
||||
keystorePassword.toCharArray(), KeyStoreType.JKS);
|
||||
|
||||
String b64 = KeyStoreUtil.keyStore2Base64(ks,keystorePassword);
|
||||
System.out.println(b64);
|
||||
Enumeration<String> temp = ks.aliases();
|
||||
int i = 0;
|
||||
while (temp.hasMoreElements()) {
|
||||
System.out.println("KeyStore alias name " + (i++) + " : "
|
||||
+ temp.nextElement());
|
||||
}
|
||||
|
||||
System.out.println("==================================");
|
||||
// load X509Certificate
|
||||
|
||||
// one from pem file
|
||||
Reader reader =new FileReader(new File("C:\\cert\\onelogin.pem"));
|
||||
X509Certificate certPem = X509CertUtils.loadCertFromReader(reader);
|
||||
|
||||
System.out.println("====loadCertificateFromPEMReader:"+certPem.getIssuerDN());
|
||||
|
||||
// two from bin file
|
||||
File fileCert = new File("C:\\cert\\clientCert.cert");
|
||||
InputStream isCert = new FileInputStream(fileCert);
|
||||
X509Certificate trustCert = X509CertUtils.loadCertFromInputStream(isCert);
|
||||
|
||||
ks = KeyStoreUtil.importTrustCertificate(ks, trustCert);
|
||||
|
||||
System.out
|
||||
.println("generatePEMEncoded==================================");
|
||||
System.out.println(X509CertUtils.generatePEMEncoded(trustCert));
|
||||
|
||||
// output keystore to file
|
||||
KeyStoreUtil.saveKeyStore(ks, new File(
|
||||
"C:\\cert\\ClientRegistrarKeyStore12.jks"),
|
||||
keystorePassword.toCharArray());
|
||||
|
||||
String pemString ="-----BEGIN CERTIFICATE-----"+'\n';
|
||||
pemString += "MIIEHjCCAwagAwIBAgIBATANBgkqhkiG9w0BAQUFADBnMQswCQYDVQQGEwJVUzET"+'\n';
|
||||
pemString += "MBEGA1UECAwKQ2FsaWZvcm5pYTEVMBMGA1UEBwwMU2FudGEgTW9uaWNhMREwDwYD"+'\n';
|
||||
pemString += "VQQKDAhPbmVMb2dpbjEZMBcGA1UEAwwQYXBwLm9uZWxvZ2luLmNvbTAeFw0xMjEx"+'\n';
|
||||
pemString += "MDEwNzUzMTJaFw0xNzExMDEwNzUzMTJaMGcxCzAJBgNVBAYTAlVTMRMwEQYDVQQI"+'\n';
|
||||
pemString += "DApDYWxpZm9ybmlhMRUwEwYDVQQHDAxTYW50YSBNb25pY2ExETAPBgNVBAoMCE9u"+'\n';
|
||||
pemString += "ZUxvZ2luMRkwFwYDVQQDDBBhcHAub25lbG9naW4uY29tMIIBIjANBgkqhkiG9w0B"+'\n';
|
||||
pemString += "AQEFAAOCAQ8AMIIBCgKCAQEAsVV3NROfDQBtSmsyZjdHKre1BMzmnjdyM5vViZV+"+'\n';
|
||||
pemString += "OMjLU/aVejupyeNi6i6fqgBzU8a6vz3bXBnL4I8CAZYuRKxz57O2iTMTHLs6cAIT"+'\n';
|
||||
pemString += "FTXSfSn/3gxgaOTNfvFXtwSD5yMaxAZckhHCTqVQgUgLLV+JApTSnW22NFadJ8aM"+'\n';
|
||||
pemString += "hbajNCbpgIW0CFeiSlbojHzpeZewi8cTgjPDBbxwOeR8VUC6bMWsseqEyxUuHH9E"+'\n';
|
||||
pemString += "TmO2pd9m5EKFpqZWlxGqa9qc6e89kpEhbIRpRjPWqSIjeDrsJllAmglsfD5MpnBq"+'\n';
|
||||
pemString += "bHXx4BK9cziv6TWMyF0MZ+CnfBWl5JCJaWBFQCs5bG0m8QIDAQABo4HUMIHRMAwG"+'\n';
|
||||
pemString += "A1UdEwEB/wQCMAAwHQYDVR0OBBYEFG6SGHTIayKeDRRGEkIdVBeRwjcFMIGRBgNV"+'\n';
|
||||
pemString += "HSMEgYkwgYaAFG6SGHTIayKeDRRGEkIdVBeRwjcFoWukaTBnMQswCQYDVQQGEwJV"+'\n';
|
||||
pemString += "UzETMBEGA1UECAwKQ2FsaWZvcm5pYTEVMBMGA1UEBwwMU2FudGEgTW9uaWNhMREw"+'\n';
|
||||
pemString += "DwYDVQQKDAhPbmVMb2dpbjEZMBcGA1UEAwwQYXBwLm9uZWxvZ2luLmNvbYIBATAO"+'\n';
|
||||
pemString += "BgNVHQ8BAf8EBAMCBPAwDQYJKoZIhvcNAQEFBQADggEBAGkBjaIhHusWRmY0O16+"+'\n';
|
||||
pemString += "WoKC7l5Re2C+bz+tyuSLlDcuHniAsyhbYG8xvEJSOnxpeFbS/a4ko80wSsd+sUXJ"+'\n';
|
||||
pemString += "FR3Z40W0JNT6ELn5Tf51b+cbm3erucMxKIDiMsQBcO/nHHBQs25kTXeKBjLnR/9u"+'\n';
|
||||
pemString += "i3+naVemnRb1cvffenAPpm12yKqWWcKgN19mE2vdrw0y/GoirFFtO/STdkDPKuYu"+'\n';
|
||||
pemString += "6wubRBeURNzqims0xe4/vPFE7iN50bjgKcuPn6LMaIDrLJVkwMC09MNsr0Dgmqgt"+'\n';
|
||||
pemString += "hBdnEqXkhdE8F/VneHn5xLSfExC662OaU6jqDASBvN15mrLGaQ+Ou9qOsCFi7wg6"+'\n';
|
||||
pemString += "8QI="+'\n';
|
||||
pemString += "-----END CERTIFICATE-----"+'\n';
|
||||
|
||||
System.out.println(pemString);
|
||||
X509Certificate x509Certificate =
|
||||
X509CertUtils.loadCertFromPEM(pemString);
|
||||
System.out.println(x509Certificate.getIssuerDN());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (KeyStoreException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}*/
|
||||
*
|
||||
* public static void main(String[] args) {
|
||||
*
|
||||
* // new //
|
||||
* ByteArrayInputStream(Base64.decodeBase64(b64EncodedKeystore.getBytes()))
|
||||
*
|
||||
* try { // load a keyStore File file = new
|
||||
* File("C:\\cert\\idp-keystore.jks"); String keystorePassword =
|
||||
* "secret"; KeyStore ks = KeyStoreUtil.loadKeyStore(file,
|
||||
* keystorePassword.toCharArray(), KeyStoreType.JKS);
|
||||
*
|
||||
* String b64 = KeyStoreUtil.keyStore2Base64(ks,keystorePassword);
|
||||
* System.out.println(b64); Enumeration<String> temp = ks.aliases();
|
||||
* int i = 0; while (temp.hasMoreElements()) {
|
||||
* System.out.println("KeyStore alias name " + (i++) + " : " +
|
||||
* temp.nextElement()); }
|
||||
*
|
||||
* System.out.println("=================================="); // load
|
||||
* X509Certificate
|
||||
*
|
||||
* // one from pem file Reader reader =new FileReader(new
|
||||
* File("C:\\cert\\onelogin.pem")); X509Certificate certPem =
|
||||
* X509CertUtils.loadCertFromReader(reader);
|
||||
*
|
||||
* System.out.println("====loadCertificateFromPEMReader:"+certPem.getIssuerDN());
|
||||
*
|
||||
* // two from bin file File fileCert = new
|
||||
* File("C:\\cert\\clientCert.cert"); InputStream isCert = new
|
||||
* FileInputStream(fileCert); X509Certificate trustCert =
|
||||
* X509CertUtils.loadCertFromInputStream(isCert);
|
||||
*
|
||||
* ks = KeyStoreUtil.importTrustCertificate(ks, trustCert);
|
||||
*
|
||||
* System.out
|
||||
* .println("generatePEMEncoded==================================");
|
||||
* System.out.println(X509CertUtils.generatePEMEncoded(trustCert));
|
||||
*
|
||||
* // output keystore to file KeyStoreUtil.saveKeyStore(ks, new
|
||||
* File( "C:\\cert\\ClientRegistrarKeyStore12.jks"),
|
||||
* keystorePassword.toCharArray());
|
||||
*
|
||||
* String pemString ="-----BEGIN CERTIFICATE-----"+'\n'; pemString
|
||||
* +=
|
||||
* "MIIEHjCCAwagAwIBAgIBATANBgkqhkiG9w0BAQUFADBnMQswCQYDVQQGEwJVUzET"+'\n';
|
||||
* pemString +=
|
||||
* "MBEGA1UECAwKQ2FsaWZvcm5pYTEVMBMGA1UEBwwMU2FudGEgTW9uaWNhMREwDwYD"+'\n';
|
||||
* pemString +=
|
||||
* "VQQKDAhPbmVMb2dpbjEZMBcGA1UEAwwQYXBwLm9uZWxvZ2luLmNvbTAeFw0xMjEx"+'\n';
|
||||
* pemString +=
|
||||
* "MDEwNzUzMTJaFw0xNzExMDEwNzUzMTJaMGcxCzAJBgNVBAYTAlVTMRMwEQYDVQQI"+'\n';
|
||||
* pemString +=
|
||||
* "DApDYWxpZm9ybmlhMRUwEwYDVQQHDAxTYW50YSBNb25pY2ExETAPBgNVBAoMCE9u"+'\n';
|
||||
* pemString +=
|
||||
* "ZUxvZ2luMRkwFwYDVQQDDBBhcHAub25lbG9naW4uY29tMIIBIjANBgkqhkiG9w0B"+'\n';
|
||||
* pemString +=
|
||||
* "AQEFAAOCAQ8AMIIBCgKCAQEAsVV3NROfDQBtSmsyZjdHKre1BMzmnjdyM5vViZV+"+'\n';
|
||||
* pemString +=
|
||||
* "OMjLU/aVejupyeNi6i6fqgBzU8a6vz3bXBnL4I8CAZYuRKxz57O2iTMTHLs6cAIT"+'\n';
|
||||
* pemString +=
|
||||
* "FTXSfSn/3gxgaOTNfvFXtwSD5yMaxAZckhHCTqVQgUgLLV+JApTSnW22NFadJ8aM"+'\n';
|
||||
* pemString +=
|
||||
* "hbajNCbpgIW0CFeiSlbojHzpeZewi8cTgjPDBbxwOeR8VUC6bMWsseqEyxUuHH9E"+'\n';
|
||||
* pemString +=
|
||||
* "TmO2pd9m5EKFpqZWlxGqa9qc6e89kpEhbIRpRjPWqSIjeDrsJllAmglsfD5MpnBq"+'\n';
|
||||
* pemString +=
|
||||
* "bHXx4BK9cziv6TWMyF0MZ+CnfBWl5JCJaWBFQCs5bG0m8QIDAQABo4HUMIHRMAwG"+'\n';
|
||||
* pemString +=
|
||||
* "A1UdEwEB/wQCMAAwHQYDVR0OBBYEFG6SGHTIayKeDRRGEkIdVBeRwjcFMIGRBgNV"+'\n';
|
||||
* pemString +=
|
||||
* "HSMEgYkwgYaAFG6SGHTIayKeDRRGEkIdVBeRwjcFoWukaTBnMQswCQYDVQQGEwJV"+'\n';
|
||||
* pemString +=
|
||||
* "UzETMBEGA1UECAwKQ2FsaWZvcm5pYTEVMBMGA1UEBwwMU2FudGEgTW9uaWNhMREw"+'\n';
|
||||
* pemString +=
|
||||
* "DwYDVQQKDAhPbmVMb2dpbjEZMBcGA1UEAwwQYXBwLm9uZWxvZ2luLmNvbYIBATAO"+'\n';
|
||||
* pemString +=
|
||||
* "BgNVHQ8BAf8EBAMCBPAwDQYJKoZIhvcNAQEFBQADggEBAGkBjaIhHusWRmY0O16+"+'\n';
|
||||
* pemString +=
|
||||
* "WoKC7l5Re2C+bz+tyuSLlDcuHniAsyhbYG8xvEJSOnxpeFbS/a4ko80wSsd+sUXJ"+'\n';
|
||||
* pemString +=
|
||||
* "FR3Z40W0JNT6ELn5Tf51b+cbm3erucMxKIDiMsQBcO/nHHBQs25kTXeKBjLnR/9u"+'\n';
|
||||
* pemString +=
|
||||
* "i3+naVemnRb1cvffenAPpm12yKqWWcKgN19mE2vdrw0y/GoirFFtO/STdkDPKuYu"+'\n';
|
||||
* pemString +=
|
||||
* "6wubRBeURNzqims0xe4/vPFE7iN50bjgKcuPn6LMaIDrLJVkwMC09MNsr0Dgmqgt"+'\n';
|
||||
* pemString +=
|
||||
* "hBdnEqXkhdE8F/VneHn5xLSfExC662OaU6jqDASBvN15mrLGaQ+Ou9qOsCFi7wg6"+'\n';
|
||||
* pemString += "8QI="+'\n'; pemString += "-----END
|
||||
* CERTIFICATE-----"+'\n';
|
||||
*
|
||||
* System.out.println(pemString); X509Certificate x509Certificate =
|
||||
* X509CertUtils.loadCertFromPEM(pemString);
|
||||
* System.out.println(x509Certificate.getIssuerDN()); } catch
|
||||
* (IOException e) { e.printStackTrace(); } catch (KeyStoreException
|
||||
* e) { e.printStackTrace(); } catch (Exception e) {
|
||||
* e.printStackTrace(); }
|
||||
*
|
||||
* }
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@ -32,7 +32,7 @@ public class ConfigurerFreeMarker implements ApplicationContextAware {
|
||||
Map<String, Object> map = this.applicationContext.getBeansWithAnnotation(FreemarkerTag.class);
|
||||
for (String key : map.keySet()) {
|
||||
configuration.setSharedVariable(key, map.get(key));
|
||||
_logger.debug("FreeMarker Template "+key);
|
||||
_logger.trace("FreeMarker Template "+key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -60,7 +60,8 @@ public class InitApplicationContext extends HttpServlet {
|
||||
* InitApplicationContext.
|
||||
*/
|
||||
public InitApplicationContext() {
|
||||
this.applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
|
||||
this.applicationContext =
|
||||
WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
|
||||
}
|
||||
|
||||
public InitApplicationContext(ConfigurableApplicationContext applicationContext) {
|
||||
@ -71,20 +72,19 @@ public class InitApplicationContext extends HttpServlet {
|
||||
* loadCaches.
|
||||
*/
|
||||
public void loadCaches() {
|
||||
_logger.info(
|
||||
"----------------------------------------------------------------------------------------------------");
|
||||
_logger.info("-----------------------------------------------------------");
|
||||
_logger.info("Load Caches ");
|
||||
|
||||
try {
|
||||
if (applicationContext.containsBean("cacheFactory")) {
|
||||
CacheFactory cacheFactory = applicationContext.getBean("cacheFactory", CacheFactory.class);
|
||||
CacheFactory cacheFactory =
|
||||
applicationContext.getBean("cacheFactory", CacheFactory.class);
|
||||
cacheFactory.start();
|
||||
}
|
||||
} catch (BeansException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
_logger.info(
|
||||
"----------------------------------------------------------------------------------------------------");
|
||||
_logger.info("-----------------------------------------------------------");
|
||||
|
||||
}
|
||||
|
||||
@ -94,30 +94,42 @@ public class InitApplicationContext extends HttpServlet {
|
||||
public void listDataBaseVariables() {
|
||||
if (applicationContext.containsBean("dataSource")) {
|
||||
try {
|
||||
_logger.debug(
|
||||
"----------------------------------------------------------------------------------------------------");
|
||||
_logger.debug("-----------------------------------------------------------");
|
||||
_logger.debug("List DatabaseMetaData Variables ");
|
||||
Connection connection = ((javax.sql.DataSource) applicationContext.getBean("dataSource"))
|
||||
Connection connection =
|
||||
((javax.sql.DataSource) applicationContext.getBean("dataSource"))
|
||||
.getConnection();
|
||||
|
||||
java.sql.DatabaseMetaData databaseMetaData = connection.getMetaData();
|
||||
_logger.debug("DatabaseProductName : " + databaseMetaData.getDatabaseProductName());
|
||||
_logger.debug("DatabaseProductVersion: " + databaseMetaData.getDatabaseProductVersion());
|
||||
_logger.debug("DatabaseMajorVersion : " + databaseMetaData.getDatabaseMajorVersion());
|
||||
_logger.debug("DatabaseMinorVersion : " + databaseMetaData.getDatabaseMinorVersion());
|
||||
_logger.debug("supportsTransactions : " + databaseMetaData.supportsTransactions());
|
||||
_logger.debug("DefaultTransaction : " + databaseMetaData.getDefaultTransactionIsolation());
|
||||
_logger.debug("MaxConnections : " + databaseMetaData.getMaxConnections());
|
||||
_logger.debug("DatabaseProductName : "
|
||||
+ databaseMetaData.getDatabaseProductName());
|
||||
_logger.debug("DatabaseProductVersion: "
|
||||
+ databaseMetaData.getDatabaseProductVersion());
|
||||
_logger.debug("DatabaseMajorVersion : "
|
||||
+ databaseMetaData.getDatabaseMajorVersion());
|
||||
_logger.debug("DatabaseMinorVersion : "
|
||||
+ databaseMetaData.getDatabaseMinorVersion());
|
||||
_logger.debug("supportsTransactions : "
|
||||
+ databaseMetaData.supportsTransactions());
|
||||
_logger.debug("DefaultTransaction : "
|
||||
+ databaseMetaData.getDefaultTransactionIsolation());
|
||||
_logger.debug("MaxConnections : "
|
||||
+ databaseMetaData.getMaxConnections());
|
||||
_logger.debug("");
|
||||
_logger.debug("JDBCMajorVersion : " + databaseMetaData.getJDBCMajorVersion());
|
||||
_logger.debug("JDBCMinorVersion : " + databaseMetaData.getJDBCMinorVersion());
|
||||
_logger.debug("DriverName : " + databaseMetaData.getDriverName());
|
||||
_logger.debug("DriverVersion : " + databaseMetaData.getDriverVersion());
|
||||
_logger.debug("JDBCMajorVersion : "
|
||||
+ databaseMetaData.getJDBCMajorVersion());
|
||||
_logger.debug("JDBCMinorVersion : "
|
||||
+ databaseMetaData.getJDBCMinorVersion());
|
||||
_logger.debug("DriverName : "
|
||||
+ databaseMetaData.getDriverName());
|
||||
_logger.debug("DriverVersion : "
|
||||
+ databaseMetaData.getDriverVersion());
|
||||
_logger.debug("");
|
||||
_logger.debug("DBMS URL : " + databaseMetaData.getURL());
|
||||
_logger.debug("UserName : " + databaseMetaData.getUserName());
|
||||
_logger.debug(
|
||||
"----------------------------------------------------------------------------------------------------");
|
||||
_logger.debug("DBMS URL : "
|
||||
+ databaseMetaData.getURL());
|
||||
_logger.debug("UserName : "
|
||||
+ databaseMetaData.getUserName());
|
||||
_logger.debug("-----------------------------------------------------------");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -129,13 +141,15 @@ public class InitApplicationContext extends HttpServlet {
|
||||
*/
|
||||
public void listProperties() {
|
||||
if (applicationContext.containsBean("propertySourcesPlaceholderConfigurer")) {
|
||||
_logger.debug(
|
||||
"----------------------------------------------------------------------------------------------------");
|
||||
_logger.debug("List Properties Variables ");
|
||||
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = ((PropertySourcesPlaceholderConfigurer) applicationContext
|
||||
_logger.trace("-----------------------------------------------------------");
|
||||
_logger.trace("List Properties Variables ");
|
||||
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer =
|
||||
((PropertySourcesPlaceholderConfigurer) applicationContext
|
||||
.getBean("propertySourcesPlaceholderConfigurer"));
|
||||
properties = (Properties) propertySourcesPlaceholderConfigurer.getAppliedPropertySources()
|
||||
.get(PropertySourcesPlaceholderConfigurer.LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME).getSource();
|
||||
properties = (Properties) propertySourcesPlaceholderConfigurer
|
||||
.getAppliedPropertySources()
|
||||
.get(PropertySourcesPlaceholderConfigurer.LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME)
|
||||
.getSource();
|
||||
Set<Object> keyValue = properties.keySet();
|
||||
SortedSet<String> keyValueSet = new TreeSet<String>();
|
||||
// sort key
|
||||
@ -146,10 +160,9 @@ public class InitApplicationContext extends HttpServlet {
|
||||
// out
|
||||
for (Iterator<String> it = keyValueSet.iterator(); it.hasNext();) {
|
||||
String key = (String) it.next();
|
||||
_logger.debug(key + " = " + properties.get(key));
|
||||
_logger.trace(key + " = " + properties.get(key));
|
||||
}
|
||||
_logger.debug(
|
||||
"----------------------------------------------------------------------------------------------------");
|
||||
_logger.trace("-----------------------------------------------------------");
|
||||
}
|
||||
}
|
||||
|
||||
@ -157,8 +170,7 @@ public class InitApplicationContext extends HttpServlet {
|
||||
* listEnvVars.
|
||||
*/
|
||||
public void listEnvVars() {
|
||||
_logger.debug(
|
||||
"----------------------------------------------------------------------------------------------------");
|
||||
_logger.debug("-----------------------------------------------------------");
|
||||
_logger.debug("List Environment Variables ");
|
||||
Map<String, String> map = System.getenv();
|
||||
SortedSet<String> keyValueSet = new TreeSet<String>();
|
||||
@ -169,27 +181,25 @@ public class InitApplicationContext extends HttpServlet {
|
||||
// out
|
||||
for (Iterator<String> it = keyValueSet.iterator(); it.hasNext();) {
|
||||
String key = (String) it.next();
|
||||
_logger.debug(key + " = " + map.get(key));
|
||||
_logger.trace(key + " = " + map.get(key));
|
||||
}
|
||||
_logger.debug("APP_HOME" + " = " + PathUtils.getInstance().getAppPath());
|
||||
_logger.debug(
|
||||
"----------------------------------------------------------------------------------------------------");
|
||||
_logger.debug("-----------------------------------------------------------");
|
||||
}
|
||||
|
||||
/**
|
||||
* showLicense.
|
||||
*/
|
||||
public void showLicense() {
|
||||
_logger.info(
|
||||
"----------------------------------------------------------------------------------------------------");
|
||||
_logger.info("-----------------------------------------------------------");
|
||||
_logger.info("+ Single Sign On ( SSO ) ");
|
||||
_logger.info("+ MaxKey Version "+properties.getProperty("application.formatted-version"));
|
||||
_logger.info("+ MaxKey Version "
|
||||
+ properties.getProperty("application.formatted-version"));
|
||||
_logger.info("");
|
||||
_logger.info("+ Apache License 2.0");
|
||||
_logger.info("+ https://shimingxy.github.io/MaxKey/");
|
||||
_logger.info("+ email:shimingxy@163.com");
|
||||
_logger.info(
|
||||
"----------------------------------------------------------------------------------------------------");
|
||||
_logger.info("-----------------------------------------------------------");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -73,6 +73,7 @@ import org.opensaml.saml2.metadata.impl.SurNameBuilder;
|
||||
import org.opensaml.saml2.metadata.impl.TelephoneNumberBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
|
||||
import java.security.KeyStore;
|
||||
|
||||
@ -118,7 +119,7 @@ public void samlmtest(){
|
||||
try {
|
||||
KeyStoreLoader keyStoreLoader=new KeyStoreLoader();
|
||||
keyStoreLoader.setKeystorePassword("secret");
|
||||
keyStoreLoader.setKeystoreFile("D:/JavaIDE/cert/idp-keystore.jks");
|
||||
keyStoreLoader.setKeystoreFile(new FileSystemResource("D:/JavaIDE/cert/idp-keystore.jks"));
|
||||
keyStoreLoader.afterPropertiesSet();
|
||||
KeyStore trustKeyStore =keyStoreLoader.getKeyStore();
|
||||
|
||||
|
||||
@ -6,14 +6,14 @@
|
||||
<appenders>
|
||||
|
||||
<Console name="consolePrint" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss,SSS} [%t] %-5level %logger{36} - %msg%n" />
|
||||
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss,SSS} %-5level [%t] %logger{36}:%L - %msg%n" />
|
||||
</Console>
|
||||
|
||||
<!-- 输出到文件,按天或者超过128MB分割 -->
|
||||
<RollingFile name="RollingFile" fileName="logs/maxkey_mgt.log" filePattern="logs/$${date:yyyyMMdd}/maxkey-%d{yyyy-MM-dd}-%i.log.gz">
|
||||
<!-- 需要记录的级别 -->
|
||||
<!-- <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" /> -->
|
||||
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n" />
|
||||
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5level [%t] (%logger{36}:%L) - %msg%n" />
|
||||
<Policies>
|
||||
<OnStartupTriggeringPolicy />
|
||||
<TimeBasedTriggeringPolicy />
|
||||
|
||||
@ -6,14 +6,14 @@
|
||||
<appenders>
|
||||
|
||||
<Console name="consolePrint" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss,SSS} [%t] %-5level %logger{36} - %msg%n" />
|
||||
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss,SSS} %-5level [%t] %logger{36}:%L - %msg%n" />
|
||||
</Console>
|
||||
|
||||
<!-- 输出到文件,按天或者超过128MB分割 每天进行归档yyyy-MM-dd -->
|
||||
<RollingFile name="RollingFile" fileName="logs/maxkey.log" filePattern="logs/$${date:yyyyMMdd}/maxkey-%d{yyyy-MM-dd}-%i.log.gz">
|
||||
<!-- 需要记录的级别 -->
|
||||
<!-- <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" /> -->
|
||||
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n" />
|
||||
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss,SSS} %-5level [%t] %logger{36}:%L - %msg%n" />
|
||||
<Policies>
|
||||
<OnStartupTriggeringPolicy />
|
||||
<TimeBasedTriggeringPolicy />
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user