"Jax-rs rest client implements Basic authentication mechanism" CHSZS, copyright, without consent, may not be reproduced. Blogger Home: Http://blog.csdn.net/chszs
Many SaaS API projects provide basic authentication mechanisms for HTTP access. Therefore, this article provides a basic access authentication for the Jax-rs rest client. Some HTTP client libraries provide Basic authentication filters that make it easier to use these libraries.
1. Basic Authentication mechanism
Simply put, the Basic authentication mechanism is the authentication mechanism implemented on the server side, requiring only the client to provide the user name and password, but it has some special requirements for the HTTP header format, as follows:
HTTP header format for Basic authentication mechanism
Authorization:basic
"Authorization" is the name of the header, and its value is such as "Basic yxbpomtles03y2izody4zwi5mmm2zzflzmy3nzy1ywexzdhmnme0oq==".
In the value, basic is the keyword, it cannot use BASE64 encoding, it follows a space, and then is the "user name + colon + password" (that is, the user name: password) of the BASE64 encoded string.
2. Previous versions of Java 8 build Basic authentication
Before Java 8, to build Basic authentication, you can use the Javax.xml.bind.DatatypeConverter tool class. Like what:
String username = "myusername";String password = "myPassword";String usernameAndPassword = username + ":" + password;String authorizationHeaderName = "Authorization";String authorizationHeaderValue = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary( usernameAndPassword.getBytes() );
3. Java 8 Building Basic Authentication
Java 8 to build Basic authentication, you can use the Java.util.Base64 tool class. Like what:
String username = "myusername";String password = "myPassword";String usernameAndPassword = username + ":" + password;String authorizationHeaderName = "Authorization";String authorizationHeaderValue = "Basic " + java.util.Base64.getEncoder().encodeToString( usernameAndPassword.getBytes() );
4. Authentication built through the Jax-rs Rest client library
To use authentication built with the Jax-rs Rest client library, you can:
Import static Org.junit.assert.*;import Javax.ws.rs.client.client;import Javax.ws.rs.client.clientbuilder;import Javax.ws.rs.client.entity;import Javax.ws.rs.core.multivaluedhashmap;import Javax.ws.rs.core.MultivaluedMap; Import Javax.ws.rs.core.response;import Org.junit.test;public class Basicauthenticationtest {@Test public void Test Basicauthentication () {//define Basic Authentication credential values String username = "MyUserName"; String password = "MyPassword"; String Usernameandpassword = Username + ":" + password; String authorizationheadername = "Authorization"; String authorizationheadervalue = "Basic" + Java.util.Base64.getEncoder (). Encodetostring ( Usernameandpassword.getbytes ()); Build the form for a POST request multivaluedmap< String, string> formparameters = new Multivaluedhashmap ( ); Formparameters.add ("Field1", "fieldValue1"); Formparameters.add ("Field2", "fieldValue2"); //Perform a POST request String Restresource = "https://restserver:8080/app-name/rest-api/"; Client client = Clientbuilder.newclient (); Response res = Client.target (restresource). Path ("login")//API Module path. Request ("Applicat Ion/json ")//Expected response MIME type. Header (Authorizationheadername, Authorizationheadervalue)//The Basic Authentication Header goes here. Post (Entity.form (formparameters)); Perform a post with the form values Asserttrue (res.getstatus () = = 200); }}
5. Summary
- For JDK 6 or later, you can use the Javax.xml.bind.DatatypeConverter class's Printbase64binary (byte[]) static method to encode a byte array into a BASE64 string.
- Java 8 provides the Java.util.Base64 class, which is designed to handle codecs such as basic, URLs, filenames, and mime.
Copyright NOTICE: This article for Bo Master Chszs original article, without Bo Master permission not reproduced.
Jax-rs Rest client implements Basic authentication mechanism