Java create azure web app

create a certificate

<java-install-dir>/bin/

keytool -genkey -alias <keystore-id>
-keystore <cert-store-dir>/<cert-file-name>.pfx -storepass <password>
-validity 3650 -keyalg RSA -keysize 2048 -storetype pkcs12
-dname "CN=Self Signed Certificate 20141118170652"

eg: keytool -genkey -alias javacer -keystore d:/java.pfx -storepass 123Aking -validity 3650 -keyalg RSA -keysize 2048 -storetype pkcs12
-dname "CN=Self Signed Certificate 20141118170652"

Convert the PFX file into JKS

<java-install-dir>/bin/keytool.exe -importkeystore
-srckeystore <cert-store-dir>/<cert-file-name>.pfx
-destkeystore <cert-store-dir>/<cert-file-name>.jks
-srcstoretype pkcs12 -deststoretype JKS

eg:keytool.exe -importkeystore -srckeystore d:/java.pfx
-destkeystore d:/javajks.jks
-srcstoretype pkcs12 -deststoretype JKS

to create cer file

<java-install-dir>/bin/keytool -export -alias <keystore-id>
-storetype pkcs12 -keystore <cert-store-dir>/<cert-file-name>.pfx
-storepass <password> -rfc -file <cert-store-dir>/<cert-file-name>.cer

eg: keytool -export -alias javacer
-storetype pkcs12 -keystore d:/java.pfx
-storepass 123Aking -rfc -file d:/javacer.cer

package common;
import java.net.URI;
import java.util.ArrayList;

// Imports for Exceptions
import java.io.IOException;
import java.net.URISyntaxException;
import javax.xml.parsers.ParserConfigurationException;
import com.microsoft.windowsazure.exception.ServiceException;
import org.xml.sax.SAXException;

// Imports for Azure App Service management configuration
import com.microsoft.windowsazure.Configuration;
import com.microsoft.windowsazure.management.configuration.ManagementConfiguration;

// Service management imports for App Service Web Apps creation
import com.microsoft.windowsazure.management.websites.*;
import com.microsoft.windowsazure.management.websites.models.*;

// Imports for authentication
import com.microsoft.windowsazure.core.utils.KeyStoreType;

import com.microsoft.windowsazure.management.websites.models.WebSpaceNames;

public class createazurevm {

    private static String uri = "https://management.core.windows.net/";
    private static String subscriptionId = "****9";
    private static String keyStoreLocation = "D:\javajks.jks";
    private static String keyStorePassword = "123Aking";

    // Define web app parameter values.
    private static String webAppName = "jamborjavacrate";
    private static String domainName = ".azurewebsites.net";
    private static String webSpaceName = WebSpaceNames.EASTASIAWEBSPACE;
    private static String appServicePlanName = "jamborplan";
    
    public static void main(String[] args)
            throws IOException, URISyntaxException, ServiceException,
            ParserConfigurationException, SAXException, Exception {

            // Create web app
            createWebApp();

        }  
    private static void createWebApp() throws Exception {

        // Specify configuration settings for the App Service management client.
        Configuration config = ManagementConfiguration.configure(
            new URI(uri),
            subscriptionId,
            keyStoreLocation,  // Path to the JKS file
            keyStorePassword,  // Password for the JKS file
            KeyStoreType.jks   // Flag that you are using a JKS keystore
        );

        // Create the App Service Web Apps management client to call Azure APIs
        // and pass it the App Service management configuration object.
        WebSiteManagementClient webAppManagementClient = WebSiteManagementService.create(config);

        // Create an App Service plan for the web app with the specified parameters.
        WebHostingPlanCreateParameters appServicePlanParams = new WebHostingPlanCreateParameters();
        appServicePlanParams.setName(appServicePlanName);
        appServicePlanParams.setSKU(SkuOptions.Free);
        webAppManagementClient.getWebHostingPlansOperations().create(webSpaceName, appServicePlanParams);

        // Set webspace parameters.
        WebSiteCreateParameters.WebSpaceDetails webSpaceDetails = new WebSiteCreateParameters.WebSpaceDetails();
        webSpaceDetails.setGeoRegion(GeoRegionNames.WESTUS);
        webSpaceDetails.setPlan(WebSpacePlanNames.VIRTUALDEDICATEDPLAN);
        webSpaceDetails.setName(webSpaceName);

        // Set web app parameters.
        // Note that the server farm name takes the Azure App Service plan name.
        WebSiteCreateParameters webAppCreateParameters = new WebSiteCreateParameters();
        webAppCreateParameters.setName(webAppName);
        webAppCreateParameters.setServerFarm(appServicePlanName);
        webAppCreateParameters.setWebSpace(webSpaceDetails);

        // Set usage metrics attributes.
        WebSiteGetUsageMetricsResponse.UsageMetric usageMetric = new WebSiteGetUsageMetricsResponse.UsageMetric();
        usageMetric.setSiteMode(WebSiteMode.Basic);
        usageMetric.setComputeMode(WebSiteComputeMode.Shared);

        // Define the web app object.
        ArrayList<String> fullWebAppName = new ArrayList<String>();
        fullWebAppName.add(webAppName + domainName);
        WebSite webApp = new WebSite();
        webApp.setHostNames(fullWebAppName);

        // Create the web app.
        WebSiteCreateResponse webAppCreateResponse = webAppManagementClient.getWebSitesOperations().create(webSpaceName, webAppCreateParameters);

        // Output the HTTP status code of the response; 200 indicates the request succeeded; 4xx indicates failure.
        System.out.println("----------");
        System.out.println("Web app created - HTTP response " + webAppCreateResponse.getStatusCode() + "
");

        // Output the name of the web app that this application created.
        String shinyNewWebAppName = webAppCreateResponse.getWebSite().getName();
        System.out.println("----------
");
        System.out.println("Name of web app created: " + shinyNewWebAppName + "
");
        System.out.println("----------
");
    }
}
原文地址:https://www.cnblogs.com/akingyao/p/4597887.html