Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

keystore - trusted certificate entries are not password-protected java

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

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"

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);
        }
    }

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

credential = resolver.resolveSingle(criteriaSet);

java.lang.UnsupportedOperationException: trusted certificate entries are not password-protected

Can anyone please guide me to solve this issue ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Solved the 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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...