Here you can find the source of base64EncodeBasicCredentials(String username, String password)
Parameter | Description |
---|---|
username | username to encode |
password | password to encode |
username:password
public static String base64EncodeBasicCredentials(String username, String password)
//package com.java2s; //License from project: Apache License import javax.xml.bind.DatatypeConverter; import java.nio.charset.StandardCharsets; public class Main { /**//from ww w .j ava 2 s .c om * Base64-encodes the specified username and password for Basic Authorization for HTTP requests or upstream proxy * authorization. The format of Basic auth is "username:password" as a base64 string. * * @param username username to encode * @param password password to encode * @return a base-64 encoded string containing <code>username:password</code> */ public static String base64EncodeBasicCredentials(String username, String password) { String credentialsToEncode = username + ':' + password; // using UTF-8, which is the modern de facto standard, and which retains compatibility with US_ASCII for ASCII characters, // as required by RFC 7616, section 3: http://tools.ietf.org/html/rfc7617#section-3 byte[] credentialsAsUtf8Bytes = credentialsToEncode.getBytes(StandardCharsets.UTF_8); return DatatypeConverter.printBase64Binary(credentialsAsUtf8Bytes); } }