本文介绍了受信任的证书条目不是受密码保护的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由其他第三方提供的.cer文件.我需要使用此.cer文件创建一个saml凭证.

I have a .cer file provided from some other party. I need to create a saml credential with this .cer file.

为此,我使用以下命令将.cer文件导入到jks文件. (密码与密码相同.它从提示符处要求接受证书.我给了y,然后它说证书已添加到密钥库中)

For this, I imported .cer file to jks file using following command. ( Password is same as password. It asked from prompt to accept certificate. I gave y then it said certificate is added to keystore )

keytool -importcert -file xyz.cer -keystore test.jks -alias"testsp"

然后,我使用这个jks文件创建了如下的凭证.

Then I used this jks file to create credential as below.

    private Credential getCredential() {
          KeyStore keystore = readKeystoreFromFile("C:\\Users\\WTC\\Downloads\\icicistage\\test.jks", "password");
          Map<String, String> passwordMap = new HashMap<String, String>();
          passwordMap.put("testsp", "password");
          KeyStoreCredentialResolver resolver = new KeyStoreCredentialResolver(keystore, passwordMap);

          Criteria criteria = new EntityIDCriteria("testsp");
          CriteriaSet criteriaSet = new CriteriaSet(criteria);

          Credential credential = null;
          try {
             credential = resolver.resolveSingle(criteriaSet);
          } catch (SecurityException e) {
              e.printStackTrace();
          }
         return credential;
    }

    private static KeyStore readKeystoreFromFile(String pathToKeyStore, String keyStorePassword) {
        try {
            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            InputStream inputStream = new FileInputStream(pathToKeyStore);
            keystore.load(inputStream, keyStorePassword.toCharArray());
            inputStream.close();
            return keystore;
        } catch (Exception e) {
            throw new RuntimeException("Something went wrong reading keystore", e);
        }
    }

下面的行在try块中给了我以下错误.

The below line gives me the following error in try block.

credential = resolver.resolveSingle(criteriaSet);

credential = resolver.resolveSingle(criteriaSet);

java.lang.UnsupportedOperationException:受信任的证书条目不受密码保护

任何人都可以指导我解决此问题吗?

Can anyone please guide me to solve this issue ?

推荐答案

解决了该问题.

我们无需在密码图中输入密码.由于证书仅包含公钥.不会输入密码.

We no need to give the password in the password map. Since certificate contains only public key. It wont take the password.

从代码中删除了以下行,效果很好.

Removed the below line from code and it works fine.

           passwordMap.put("testsp", "password");

这篇关于受信任的证书条目不是受密码保护的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 03:28