All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.azure.core.credential.BasicAuthenticationCredential Maven / Gradle / Ivy

The newest version!
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.credential;

import com.azure.core.util.Base64Util;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;

/**
 * 

* The {@link BasicAuthenticationCredential} is used to authenticate and authorize requests made to * Azure services using the Basic authentication scheme. Basic Authentication is a simple authentication scheme * that uses a combination of a username and password. *

* *

* Note that Basic Authentication is generally considered less secure than other authentication methods, * such as Azure Active Directory (AAD) authentication. It is recommended to use * Azure Active Directory (Azure AD) * authentication via {@link TokenCredential} whenever possible, especially for production environments. *

* *

* Sample: Azure SAS Authentication *

* *

* The following code sample demonstrates the creation of a * {@link com.azure.core.credential.BasicAuthenticationCredential}, using username and password *

* * *
 * BasicAuthenticationCredential basicAuthenticationCredential =
 *     new BasicAuthenticationCredential("<username>", "<password>");
 * 
* * * @see com.azure.core.credential * @see com.azure.core.credential.TokenCredential */ public class BasicAuthenticationCredential implements TokenCredential { /** * Base64 encoded username-password credential. */ private final String encodedCredential; /** * Creates a basic authentication credential. * * @param username basic auth username * @param password basic auth password */ public BasicAuthenticationCredential(String username, String password) { String credential = username + ":" + password; this.encodedCredential = Base64Util.encodeToString(credential.getBytes(StandardCharsets.UTF_8)); } @Override public Mono getToken(TokenRequestContext request) { return Mono.fromCallable(() -> new AccessToken(encodedCredential, OffsetDateTime.MAX)); } @Override public AccessToken getTokenSync(TokenRequestContext request) { return new AccessToken(encodedCredential, OffsetDateTime.MAX); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy