当前位置: 首页>>代码示例>>Java>>正文


Java SecurityUtils类代码示例

本文整理汇总了Java中com.google.api.client.util.SecurityUtils的典型用法代码示例。如果您正苦于以下问题:Java SecurityUtils类的具体用法?Java SecurityUtils怎么用?Java SecurityUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


SecurityUtils类属于com.google.api.client.util包,在下文中一共展示了SecurityUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: privateKeyFromPkcs8

import com.google.api.client.util.SecurityUtils; //导入依赖的package包/类
/**
 * The method validates an input string of private key and generate a java PrivateKey
 * object. The method is non-blocking.
 * @param privateKeyPem The private key in string format.
 * @return The private key in java PrivateKey object format.
 * @throws IOException When input key is not valid.
 */
public static PrivateKey privateKeyFromPkcs8(String privateKeyPem) throws IOException {
    StringReader reader = new StringReader(privateKeyPem);
    Section section = PemReader.readFirstSectionAndClose(reader, PRIVATE_KEY);
    if (section == null) {
        throw new IOException("Invalid PKCS8 data.");
    }
    try {
        byte[] decodedKey = section.getBase64DecodedBytes();
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey);
        KeyFactory keyFactory = SecurityUtils.getRsaKeyFactory();
        return keyFactory.generatePrivate(keySpec);
    } catch (Exception e) {
        throw new IOException("Unexpected exception reading PKCS data", e);
    }
}
 
开发者ID:vmware,项目名称:photon-model,代码行数:23,代码来源:GCPUtils.java

示例2: isValid

import com.google.api.client.util.SecurityUtils; //导入依赖的package包/类
public boolean isValid() {
    projectId = getSinkVar("project_id").trim();
    datasetId = getSinkVar("dataset_id").trim();
    serviceAccountId = getSinkVar("service_account_id").trim();
    pk12KeyBase64 = getSinkVar("pk12base64").trim();

    // Init key
    try {
        byte[] keyBytes = Base64.decodeBase64(pk12KeyBase64);
        ByteArrayInputStream bis = new ByteArrayInputStream(keyBytes);
        pk12 = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), bis, "notasecret", "privatekey", "notasecret");
        LOG.info("Loaded PK12 key");
    } catch (Exception e) {
        LOG.error("Failed to load private key", e);
        return false;
    }

    return !projectId.isEmpty() && !datasetId.isEmpty() && !serviceAccountId.isEmpty();
}
 
开发者ID:RobinUS2,项目名称:cloudpelican-lsd,代码行数:20,代码来源:BigQuerySinkBolt.java

示例3: authorize

import com.google.api.client.util.SecurityUtils; //导入依赖的package包/类
private Credential authorize() throws IOException, GeneralSecurityException
{
    PrivateKey securityKey = SecurityUtils.loadPrivateKeyFromKeyStore
    (
    		SecurityUtils.getPkcs12KeyStore(), 
            ClassUtils.getDefaultClassLoader().getResourceAsStream(credentials_.getLoginCredentials().getCredential()), 
            "notasecret", "privatekey", "notasecret"
    );
    
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), 
            new FileReader(new File(SystemUtils2.getApplicationDataPath(), credentials_.getLoginCredentials().getIdentity())));
    
    GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId((String) clientSecrets.getWeb().get("client_email"))
            .setServiceAccountScopes(Arrays.asList(ComputeScopes.COMPUTE, ComputeScopes.DEVSTORAGE_FULL_CONTROL))
            .setServiceAccountPrivateKey(securityKey)
            .build();

    return credential;
}
 
开发者ID:alessandroleite,项目名称:dohko,代码行数:23,代码来源:GoogleCompute.java

示例4: Parser

import com.google.api.client.util.SecurityUtils; //导入依赖的package包/类
/**
 * Initialize the parser by setting up the BigQuery client ready to take take commands.
 * 
 * @throws GeneralSecurityException
 * @throws IOException
 */
private Parser() throws GeneralSecurityException, IOException {
	final HttpTransport http = GoogleNetHttpTransport.newTrustedTransport();
	final JsonFactory jsonFactory = new JacksonFactory();
	final KeyStore keyStore = SecurityUtils.getPkcs12KeyStore();
	final InputStream key = getClass().getClassLoader().getResourceAsStream(keyFile);
	final PrivateKey p12 = SecurityUtils.loadPrivateKeyFromKeyStore(keyStore, key, "notasecret", "privatekey", "notasecret");

	final GoogleCredential.Builder credential = new GoogleCredential.Builder();
	credential.setTransport(http);
	credential.setJsonFactory(jsonFactory);
	credential.setServiceAccountId(account);
	credential.setServiceAccountScopes(Arrays.asList(BigqueryScopes.BIGQUERY));
	credential.setServiceAccountPrivateKey(p12);

	final Bigquery.Builder bq = new Bigquery.Builder(http, jsonFactory, credential.build());
	bq.setApplicationName(project);
	client = bq.build();
}
 
开发者ID:mallocator,项目名称:Elasticsearch-BigQuery-River,代码行数:25,代码来源:BigQueryRiver.java

示例5: getCredentialFromPrivateKeyServiceAccount

import com.google.api.client.util.SecurityUtils; //导入依赖的package包/类
/**
 * Initializes OAuth2 credential from a private keyfile, as described in
 * <a href="https://code.google.com/p/google-api-java-client/wiki/OAuth2#Service_Accounts"
 * > OAuth2 Service Accounts</a>.
 *
 * @param serviceAccountEmail Email address of the service account associated with the keyfile.
 * @param privateKeyFile Full local path to private keyfile.
 * @param scopes List of well-formed desired scopes to use with the credential.
 */
public static Credentials getCredentialFromPrivateKeyServiceAccount(
    String serviceAccountEmail, String privateKeyFile, List<String> scopes)
    throws IOException, GeneralSecurityException {
  String clientId = null;
  String privateKeyId = null;
  PrivateKey privateKey =
      SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
        new FileInputStream(privateKeyFile), "notasecret", "privatekey", "notasecret");
  return new ServiceAccountCredentials(clientId, serviceAccountEmail, privateKey, privateKeyId,
      scopes);
}
 
开发者ID:dmmcerlean,项目名称:cloud-bigtable-client,代码行数:21,代码来源:CredentialFactory.java

示例6: createCredentialForServiceAccount

import com.google.api.client.util.SecurityUtils; //导入依赖的package包/类
/**
 * see https://developers.google.com/accounts/docs/OAuth2ServiceAccount
 * https://developers.google.com/console/help/#activatingapis 		<---- look for [Service accounts]!!!!
 * https://developers.google.com/accounts/docs/OAuth2ServiceAccount <---- look for [Service accounts]!!!!
 * @param httpTransport
 * @param jsonFactory
 * @return
 * @throws IOException
 * @throws GeneralSecurityException
 */
@SuppressWarnings("resource")
public static GoogleCredential createCredentialForServiceAccount(final HttpTransport httpTransport,
																 final JsonFactory jsonFactory,
																 final GoogleAPIServiceAccountClientData serviceAccountClientId) throws IOException,
																									    			 				    GeneralSecurityException {
	// Load the private key from the p12 store
	PrivateKey serviceAccountPrivateKey = null;
	@Cleanup InputStream privateKeyIS = null;
	if (serviceAccountClientId.getP12KeyPath().isLoadedFromClassPath()) {
		privateKeyIS = Thread.currentThread().getContextClassLoader().getResourceAsStream(serviceAccountClientId.getP12KeyPath().getFilePathAsString());
	} else {
		File p12KeyFile = new File(serviceAccountClientId.getP12KeyPath().getFilePathAsString());
		privateKeyIS = new FileInputStream(p12KeyFile);
	}
	if (privateKeyIS == null) throw new IllegalStateException(Throwables.message("Could NOT load the P12 key file from {} using {} loader",
																				 serviceAccountClientId.getP12KeyPath().getFilePathAsString(),
																				 serviceAccountClientId.getP12KeyPath().getResourcesLoaderType()));	
	serviceAccountPrivateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
																		privateKeyIS, 
																		"notasecret",
																		"privatekey",
																		"notasecret");
	// create the credential
	GoogleCredential credential = new GoogleCredential.Builder()
											    .setTransport(httpTransport)
											    .setJsonFactory(jsonFactory)
											    .setServiceAccountId(serviceAccountClientId.getClientEmail().asString())	
											    .setServiceAccountPrivateKey(serviceAccountPrivateKey)
											    .setServiceAccountScopes(serviceAccountClientId.getScopes())	// see https://developers.google.com/gmail/api/auth/scopes
											    .setServiceAccountUser(serviceAccountClientId.getEndUserEmail().asString())
											    .build();	
	credential.refreshToken();
	return credential;
}
 
开发者ID:opendata-euskadi,项目名称:r01fb,代码行数:45,代码来源:GoogleAPI.java

示例7: buildCredential

import com.google.api.client.util.SecurityUtils; //导入依赖的package包/类
public static GoogleCredential buildCredential(CloudCredential gcpCredential, HttpTransport httpTransport) throws IOException, GeneralSecurityException {
    PrivateKey pk = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
            new ByteArrayInputStream(Base64.decodeBase64(getServiceAccountPrivateKey(gcpCredential))), "notasecret", "privatekey", "notasecret");
    return new GoogleCredential.Builder().setTransport(httpTransport)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(getServiceAccountId(gcpCredential))
            .setServiceAccountScopes(SCOPES)
            .setServiceAccountPrivateKey(pk)
            .build();
}
 
开发者ID:hortonworks,项目名称:cloudbreak,代码行数:11,代码来源:GcpStackUtil.java

示例8: deleteNetwork

import com.google.api.client.util.SecurityUtils; //导入依赖的package包/类
@AfterSuite
@Parameters("vpcName")
public void deleteNetwork(@Optional("it-vpc") String vpcName) throws Exception {
    springTestContextPrepareTestInstance();
    String serviceAccountPrivateKey = ResourceUtil.readBase64EncodedContentFromResource(applicationContext, defaultP12File);
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
            new ByteArrayInputStream(Base64.decodeBase64(serviceAccountPrivateKey)), "notasecret", "privatekey", "notasecret");
    JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    GoogleCredential googleCredential = new GoogleCredential.Builder().setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId(defaultServiceAccountId)
            .setServiceAccountScopes(Collections.singletonList(ComputeScopes.COMPUTE))
            .setServiceAccountPrivateKey(privateKey)
            .build();

    Compute compute = new Builder(httpTransport, jsonFactory, null)
            .setApplicationName(defaultName)
            .setHttpRequestInitializer(googleCredential)
            .build();

    Delete delete = compute.networks().delete(defaultProjectId, vpcName);

    Operation operation = delete.execute();
    if (operation.getHttpErrorStatusCode() != null) {
        throw new IllegalStateException("gcp operation failed: " + operation.getHttpErrorMessage());
    }
}
 
开发者ID:hortonworks,项目名称:cloudbreak,代码行数:29,代码来源:GcpDeleteVpcTest.java

示例9: checkTagsGcp

import com.google.api.client.util.SecurityUtils; //导入依赖的package包/类
protected static void checkTagsGcp(ApplicationContext applicationContext, String applicationName, String projectId, String serviceAccountId, String p12File,
        String availabilityZone, List<String> instanceIdList, Map<String, String> tagsToCheckMap) throws Exception {
    String serviceAccountPrivateKey = ResourceUtil.readBase64EncodedContentFromResource(applicationContext, p12File);
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
            new ByteArrayInputStream(Base64.decodeBase64(serviceAccountPrivateKey)), "notasecret", "privatekey", "notasecret");
    JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();

    GoogleCredential googleCredential = new GoogleCredential.Builder().setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId(serviceAccountId)
            .setServiceAccountScopes(Collections.singletonList(ComputeScopes.COMPUTE))
            .setServiceAccountPrivateKey(privateKey)
            .build();

    Compute compute = new Builder(httpTransport, jsonFactory, null)
            .setApplicationName(applicationName)
            .setHttpRequestInitializer(googleCredential)
            .build();

    Instances instances = compute.instances();

    for (String id : instanceIdList) {
        Get response = instances.get(projectId, availabilityZone, id);
        com.google.api.services.compute.model.Instance instance = response.execute();
        Tags gcpTags = instance.getTags();
        Map<String, String> extractedTags = new HashMap<>();
        List<String> tagList = gcpTags.getItems();

        for (String i : tagList) {
            String[] tmpTagList = i.split("-");
            if (tmpTagList.length > 1) {
                extractedTags.put(tmpTagList[0], tmpTagList[1]);
            }
        }
        checkTags(tagsToCheckMap, extractedTags);
        extractedTags.clear();
    }

}
 
开发者ID:hortonworks,项目名称:cloudbreak,代码行数:41,代码来源:TagsUtil.java

示例10: generateRsaKey

import com.google.api.client.util.SecurityUtils; //导入依赖的package包/类
private void generateRsaKey() throws IOException, GeneralSecurityException {
  String content = new String(Files.readAllBytes(Paths.get(serviceAccountPrivateKeyPath)));
  JSONObject privateKeyJson = new JSONObject(content);
  String privateKeyPkcs8 = (String) privateKeyJson.get("private_key");
  Reader reader = new StringReader(privateKeyPkcs8);
  Section section = PemReader.readFirstSectionAndClose(reader, "PRIVATE KEY");
  byte[] bytes = section.getBase64DecodedBytes();
  PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
  KeyFactory keyFactory = SecurityUtils.getRsaKeyFactory();
  serviceAccountPrivateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
}
 
开发者ID:android-pay,项目名称:s2ap-quickstart-java,代码行数:12,代码来源:WobCredentials.java

示例11: createNetwork

import com.google.api.client.util.SecurityUtils; //导入依赖的package包/类
@Test
@Parameters({"networkName", "description", "publicInAccount", "resourceGroupName", "vpcName", "vpcSubnet", "subnetCIDR", "networkType"})
public void createNetwork(String networkName, @Optional("") String description, @Optional("false") boolean publicInAccount,
        @Optional("europe-west1") String subnetRegion, @Optional("it-vpc") String vpcName,
        @Optional("it-vpc-subnet") String vpcSubnet, @Optional("10.0.36.0/24") String subnetCIDR, NetworkType networkType) throws Exception {
    String serviceAccountPrivateKey = ResourceUtil.readBase64EncodedContentFromResource(applicationContext, defaultP12File);
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
            new ByteArrayInputStream(Base64.decodeBase64(serviceAccountPrivateKey)), "notasecret", "privatekey", "notasecret");
    JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    GoogleCredential googleCredential = new GoogleCredential.Builder().setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId(defaultServiceAccountId)
            .setServiceAccountScopes(Collections.singletonList(ComputeScopes.COMPUTE))
            .setServiceAccountPrivateKey(privateKey)
            .build();

    Compute compute = new Builder(httpTransport, jsonFactory, null)
            .setApplicationName(defaultName)
            .setHttpRequestInitializer(googleCredential)
            .build();

    Network gcpNetwork = new Network();
    gcpNetwork.setName(vpcName);
    if (!LAGACY_NETWORK.equals(networkType)) {
        gcpNetwork.setAutoCreateSubnetworks(false);
    }

    Networks.Insert networkInsert = compute.networks().insert(defaultProjectId, gcpNetwork);

    Operation networkInsertResponse = networkInsert.execute();

    if (networkInsertResponse.getHttpErrorStatusCode() != null) {
        throw new IllegalStateException("gcp network operation failed: " + networkInsertResponse.getHttpErrorMessage());
    }

    waitOperation(compute, networkInsertResponse);

    if (EXISTING_SUBNET_IN_EXISTING_NETWORK.equals(networkType)) {
        Subnetwork gcpSubnet = new Subnetwork();
        gcpSubnet.setName(vpcSubnet);
        gcpSubnet.setIpCidrRange(subnetCIDR);
        gcpSubnet.setNetwork(String.format("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", defaultProjectId, vpcName));
        Insert subNetworkInsert = compute.subnetworks().insert(defaultProjectId, subnetRegion, gcpSubnet);
        Operation subNetInsertResponse = subNetworkInsert.execute();
        if (subNetInsertResponse.getHttpErrorStatusCode() != null) {
            throw new IllegalStateException("gcp subnetwork operation failed: " + subNetInsertResponse.getHttpErrorMessage());
        }
    }

    NetworkRequest networkRequest = new NetworkRequest();
    networkRequest.setName(networkName);
    networkRequest.setDescription(description);
    if (NEW_SUBNET_IN_EXISTING_NETWORK.equals(networkType)) {
        networkRequest.setSubnetCIDR(subnetCIDR);
    }
    Map<String, Object> map = new HashMap<>();
    map.put("networkId", vpcName);
    if (EXISTING_SUBNET_IN_EXISTING_NETWORK.equals(networkType)) {
        map.put("subnetId", vpcSubnet);
    }
    networkRequest.setParameters(map);
    networkRequest.setCloudPlatform("GCP");
    String id = getCloudbreakClient().networkEndpoint().postPrivate(networkRequest).getId().toString();
    getItContext().putContextParam(CloudbreakITContextConstants.NETWORK_ID, id, true);
}
 
开发者ID:hortonworks,项目名称:cloudbreak,代码行数:67,代码来源:GcpCreateVirtualNetworkTest.java

示例12: trustCertificatesFromJavaKeyStore

import com.google.api.client.util.SecurityUtils; //导入依赖的package包/类
/**
 * Sets the SSL socket factory based on root certificates in a Java KeyStore.
 * <p/>
 * <p>
 * Example usage:
 * </p>
 * <p/>
 * <pre>
 * trustCertificatesFromJavaKeyStore(new FileInputStream("certs.jks"), "password");
 * </pre>
 *
 * @param keyStoreStream input stream to the key store (closed at the end of this method in a
 *                       finally block)
 * @param storePass      password protecting the key store file
 * @since 1.14
 */
public Builder trustCertificatesFromJavaKeyStore(InputStream keyStoreStream, String storePass)
        throws GeneralSecurityException, IOException {
    KeyStore trustStore = SecurityUtils.getJavaKeyStore();
    SecurityUtils.loadKeyStore(trustStore, keyStoreStream, storePass);
    return trustCertificates(trustStore);
}
 
开发者ID:AndrewJack,项目名称:moment-for-android-wear,代码行数:23,代码来源:OkHttpTransport.java

示例13: trustCertificatesFromStream

import com.google.api.client.util.SecurityUtils; //导入依赖的package包/类
/**
 * Sets the SSL socket factory based root certificates generated from the specified stream using
 * {@link java.security.cert.CertificateFactory#generateCertificates(java.io.InputStream)}.
 * <p/>
 * <p>
 * Example usage:
 * </p>
 * <p/>
 * <pre>
 * trustCertificatesFromStream(new FileInputStream("certs.pem"));
 * </pre>
 *
 * @param certificateStream certificate stream
 * @since 1.14
 */
public Builder trustCertificatesFromStream(InputStream certificateStream)
        throws GeneralSecurityException, IOException {
    KeyStore trustStore = SecurityUtils.getJavaKeyStore();
    trustStore.load(null, null);
    SecurityUtils.loadKeyStoreFromCertificates(
            trustStore, SecurityUtils.getX509CertificateFactory(), certificateStream);
    return trustCertificates(trustStore);
}
 
开发者ID:AndrewJack,项目名称:moment-for-android-wear,代码行数:24,代码来源:OkHttpTransport.java

示例14: trustCertificatesFromJavaKeyStore

import com.google.api.client.util.SecurityUtils; //导入依赖的package包/类
/**
 * Sets the SSL socket factory based on root certificates in a Java KeyStore.
 *
 * <p>
 * Example usage:
 * </p>
 *
 * <pre>
 trustCertificatesFromJavaKeyStore(new FileInputStream("certs.jks"), "password");
 * </pre>
 *
 * @param keyStoreStream input stream to the key store (closed at the end of this method in a
 *        finally block)
 * @param storePass password protecting the key store file
 * @since 1.14
 */
public Builder trustCertificatesFromJavaKeyStore(InputStream keyStoreStream, String storePass)
        throws GeneralSecurityException, IOException {
    KeyStore trustStore = SecurityUtils.getJavaKeyStore();
    SecurityUtils.loadKeyStore(trustStore, keyStoreStream, storePass);
    return trustCertificates(trustStore);
}
 
开发者ID:gdgjodhpur,项目名称:gdgapp,代码行数:23,代码来源:GapiOkTransport.java

示例15: trustCertificatesFromStream

import com.google.api.client.util.SecurityUtils; //导入依赖的package包/类
/**
 * Sets the SSL socket factory based root certificates generated from the specified stream using
 *
 * <p>
 * Example usage:
 * </p>
 *
 * <pre>
 trustCertificatesFromStream(new FileInputStream("certs.pem"));
 * </pre>
 *
 * @param certificateStream certificate stream
 * @since 1.14
 */
public Builder trustCertificatesFromStream(InputStream certificateStream)
        throws GeneralSecurityException, IOException {
    KeyStore trustStore = SecurityUtils.getJavaKeyStore();
    trustStore.load(null, null);
    SecurityUtils.loadKeyStoreFromCertificates(
            trustStore, SecurityUtils.getX509CertificateFactory(), certificateStream);
    return trustCertificates(trustStore);
}
 
开发者ID:gdgjodhpur,项目名称:gdgapp,代码行数:23,代码来源:GapiOkTransport.java


注:本文中的com.google.api.client.util.SecurityUtils类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。