问题描述
- keytool生成证书,上传到azure中,但仍旧,显示认证失败
-
public class TestClient { private static ComputeManagementClient computeManagementClient; private static ManagementClient client; private static URI uri; private static String subscriptionId; private static String keyStoreLocation; private static String keyStorePassword; private static KeyStoreType keyStoreType; public static Configuration createConfiguration() throws IOException,URISyntaxException{ uri = new URI("https://management.core.windows.net/"); subscriptionId="1dd1940e-9979-476c-a5dc-79a7d31686fd"; keyStoreLocation="C:\Program Files\Java\jdk1.8.0_40\bin\AzureKeyStore.jks"; keyStorePassword="123456"; keyStoreType= KeyStoreType.fromString("jks"); Configuration configuration = ManagementConfiguration.configure(uri, subscriptionId, keyStoreLocation, keyStorePassword,keyStoreType); configuration.setProperty("testprefix.com.microsoft.windowsazure.services.core.Configuration.connectTimeout","3"); configuration.setProperty("testprefix.com.microsoft.windowsazure.services.core.Configuration.readTimeout","7"); return configuration; } /** * https://management.core.windows.net/1dd1940e-9979-476c-a5dc-79a7d31686fd/services/resourceextensions * @param args * @throws Exception */ public static void main(String[] args) throws Exception { computeManagementClient = ComputeManagementService.create(createConfiguration()); client = ManagementService.create(createConfiguration()); // get the list of regions LocationsListResponse response = client.getLocationsOperations().list(); ArrayList<LocationsListResponse.Location> locations = response.getLocations(); // write them out for( int i=0; i<locations.size(); i++){ System.out.println(locations.get(i).getDisplayName()); } HostedServiceListResponse hostedServiceListResponse = computeManagementClient.getHostedServicesOperations().list(); System.out.println("statusCode:"+hostedServiceListResponse.getStatusCode()); VirtualMachineExtensionListResponse virtualMachineExtensionListResponse = computeManagementClient.getVirtualMachineExtensionsOperations().list(); System.out.println("................response........................"); System.out.println("requestId:" + virtualMachineExtensionListResponse.getRequestId()); System.out.println("statusCode:"+virtualMachineExtensionListResponse.getStatusCode()); ArrayList<VirtualMachineExtensionListResponse.ResourceExtension> list= virtualMachineExtensionListResponse.getResourceExtensions(); if( list != null && !list.isEmpty() ){ for( VirtualMachineExtensionListResponse.ResourceExtension resourceExtension : list){ System.out.println("resourceExtension:"+resourceExtension.getName()); } } } }
但依然报http 403错误
This XML file does not appear to have any style information associated with it. The document tree is shown below.ForbiddenError
The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.
解决方案
您好,看起来您使用的是Azure Management Library for Java,请问您使用的是China Azure还是Global Azure?
我不确定您的访问终结点是中国版Azure还是Global,因此,我建议您可以使用Gaurav Mantri这种方式去访问您的证书:
//Get key
private static KeyStore getKeyStore(String keyStoreName, String password) throws IOException
{
KeyStore ks = null;
FileInputStream fis = null;
try {
ks = KeyStore.getInstance("JKS");
char[] passwordArray = password.toCharArray();
fis = new java.io.FileInputStream(keyStoreName);
ks.load(fis, passwordArray);
fis.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if (fis != null) {
fis.close();
}
}
return ks;
}
// Get SSLSocketFactory
private static SSLSocketFactory getSSLSocketFactory(String keyStoreName, String password) throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, IOException {
KeyStore ks = getKeyStore(keyStoreName, password);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyManagerFactory.init(ks, password.toCharArray());
SSLContext context = SSLContext.getInstance("TLS");
context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
return context.getSocketFactory();
}
//
//You can use it in this method
private static String processGetRequest(URL url, String keyStore, String keyStorePassword) throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException {
SSLSocketFactory sslFactory = getSSLSocketFactory(keyStore, keyStorePassword);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setSSLSocketFactory(sslFactory);
con.setRequestMethod("GET");
con.addRequestProperty("x-ms-version", "2012-03-01");
InputStream responseStream = (InputStream) con.getContent();
String response = getStringFromInputStream(responseStream);
responseStream.close();
return response;
}
也可以参考这篇文章:http://gauravmantri.com/2013/08/25/consuming-windows-azure-service-management-api-in-java/
也建议您使用Fiddle去抓获下真正的错误信息。
Regards,
Will
如果您想进一步了解Windows Azure, Windows Azure 官网欢迎您的访问
解决方案二:
Hi,
需要补充的是,如果使用的是中国版的Azure我们还需要多出一项工作,CNNIC颁发的证书没有导入JDK的KeyStore,因此不被JDK所信任,我们需要通过程序的方式将其导入,具体参考这篇文章:http://blog.csdn.net/faye0412/article/details/6883879 。
Best Regards,
Jambor
如果您想进一步了解Windows Azure, Windows Azure 官网欢迎您的访问