Java中的Provider getProperty()方法及其示例

这个 getProperty() 方法 JAVA安全供应商 类用于在此属性列表中搜索具有指定键的属性。如果在该属性列表中找不到该键,则会递归地检查默认属性列表及其默认值。如果找不到属性,该方法将返回null。

null

语法:

public String getProperty(String key)

参数: 这种方法需要 属性密钥 作为参数。

返回值: 此方法返回 此属性列表中的值 使用指定的键值或 无效的 如果找不到该属性。

下面是一些例子来说明 getProperty() 方法:

例1:

// Java program to demonstrate
// getProperty() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
// Declaring int values
int i = 10 ;
try {
// creating the object of  KeyPairGenerator
KeyPairGenerator sr = KeyPairGenerator.getInstance( "DSA" , "SUN" );
// getting the Provider of the KeyPairGenerator sr
// by using method getProvider()
Provider provider = sr.getProvider();
// Declaring the variable of set<Map> type
Set<Object> set;
// getting unmodifiable Set view of the property entries
set = provider.keySet();
// Creating the object of iterator to iterate set
Iterator iter = set.iterator();
while (i > 0 ) {
// getting the mapped value in element
// using getProperty() method
String property = provider.getProperty((String)iter.next());
// printing the property of specified key
System.out.println( "value is : " + property);
i--;
}
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
}
}


输出:

value is : SHA1withDSA
value is : SHA1withDSA
value is : SHA1withDSA
value is : Software
value is : sun.security.provider.JavaKeyStore$DualFormatJKS
value is : SHA
value is : sun.security.provider.SHA
value is : sun.security.provider.JavaKeyStore$CaseExactJKS
value is : Software
value is : sun.security.provider.DSA$SHA256withDSA

例2:

// Java program to demonstrate
// getProperty() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
// Declaring int values
int i = 10 ;
try {
// creating the object of  KeyPairGenerator
KeyPairGenerator sr = KeyPairGenerator.getInstance( "DSA" , "SUN" );
// getting the Provider of the KeyPairGenerator sr
// by using method getProvider()
Provider provider = sr.getProvider();
// getting the mapped value in element
// using getProperty() method
System.out.println( "Trying to search for unspecified key" );
String property = provider.getProperty( "geeks" );
// printing the property of specified key
System.out.println( "value is : " + property);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
}
}


输出:

Trying to search for unspecified key
value is : null

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享