当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java KeyStore containsAlias()用法及代码示例


java.security.KeyStore类的containsAlias()方法用于检查请求的alies的存在。

用法:

public final boolean containsAlias(String alias)
  throws KeyStoreException

参数:此方法将别名的名称作为要检查其条目的String类型的参数来查找。


返回值:此方法返回表明结果的布尔值。

异常:如果您不初始化此 key 库,则此方法将引发KeyStoreException。

注意:由于不存在“ privatekey” key 库,因此本文中的所有程序都无法在在线IDE上运行。您可以在系统上的Java编译器上检查此代码。要检查此代码,请在系统上创建 key 库“ privatekey”,并设置自己的 key 库密码以访问该 key 库。

下面是说明containsAlias()方法的示例:

示例1:

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.security.cert.*; 
import java.util.*; 
import java.io.*; 
  
public class GFG { 
    public static void main(String[] argv) 
    { 
        try { 
  
            // creating the object of MessageDigest 
            // and getting instance 
            // By usng getInstance() method 
            KeyStore sr = KeyStore.getInstance("JKS"); 
  
            // Keystore password is require 
            // to access Keystore 
            char[] pass = ("123456").toCharArray(); 
  
            // creating and intializing 
            // object of InputStream 
            InputStream is 
                = new FileInputStream( 
                    "f:/java/private key.store"); 
  
            // intializing keystore object 
            sr.load(is, pass); 
  
            // checking the presence of the aliases 
            // using containsAlias() method 
            boolean status 
                = sr.containsAlias("ftpkey"); 
  
            // display the result 
            if (status) 
                System.out.println("This aliases is present"); 
            else
                System.out.println("This aliases is absent"); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (KeyStoreException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (FileNotFoundException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (IOException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (CertificateException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
输出:
This aliases is present

示例2:对于KeyStoreException

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.security.cert.*; 
import java.util.*; 
import java.io.*; 
  
public class GFG { 
    public static void main(String[] argv) 
    { 
        try { 
  
            // creating the object of MessageDigest 
            // and getting instance 
            // By usng getInstance() method 
            KeyStore sr = KeyStore.getInstance("JKS"); 
  
            // intializing keystore object 
            // sr.load(is, pass); 
  
            // checking the presence of the aliases 
            // using containsAlias() method 
            boolean status = sr.containsAlias("gfg"); 
  
            // display the result 
            if (status) 
                System.out.println("This aliases is present"); 
            else
                System.out.println("This aliases is absent"); 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (KeyStoreException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
输出:
Exception thrown :
 java.security.KeyStoreException:
 Uninitialized keystore

参考: https://docs.oracle.com/javase/9/docs/api/java/security/KeyStore.html#containsAlias-java.lang.String-



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 KeyStore containsAlias() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。