Http basic authentication header: Learn with Java code sample

Http basic authentication header is a popular mechanism for authentication, specially when it comes to internal applications. With Java, we can handle this header.

HTTP basic authentication with headers is one of the username & password based methods of securing access to web sites, web applications and web services. Purpose of this article is to analyze the details of this approach by explaining how to encode a pair of username & password as a basic authentication header string as well as to decode the authentication string generated from the web clients like browser or soapIU; and the example is implemented with Java.

Isn't username and password send to server?

When username and password is entered into the pop-up in Web browser (or by similar manner in other web clients) those are not send to server as they are, but send after encoding in a way that the receiving server side can decode and extract the username and password to check the validity. This encoding approach is not secure as the encryption approaches like AES.

Sample request with basic authentication header for username="Aladdin" and password="open sesame" looks as below.

GET /myweb/index.html HTTP/1.1
Host: localhost
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

Web clients create a string by concatenating the username and password with a colon (":") as username:password. Then it is encoded in base 64 and is sent to the server, so that the server can do the reverse to extract username and password.

Example Program

Code example imports a class named Imported class named org.apache.commons.codec.binary.Base64 from commons-codec-1.6 available at http://commons.apache.org/codec/download_codec.cgi. Please download it yourself and add the commons-codec-1.6.jar file to the CLASSPATH.

package org.kamal.http.basicauth;

import org.apache.commons.codec.binary.Base64;

public class HttpBasicAuthenticationHeader {

    public static void main(String[] args) {

        final String username = "Aladdin";
        final String password = "open sesame";

        System.out.println("Input\t: username [" + username + "]," +
                " password [" + password + "]");

        final String encodedText = createEncodedText(username, password);
        System.out.println("Encoded Text : " + encodedText);

        final String[] userDetails = decode(encodedText);
        System.out.println("Decoded\t: username [" + userDetails[0] + 
                "], password [" +  userDetails[1] + "]");

    }

    private static String[] decode(final String encoded) {
        final byte[] decodedBytes 
                = Base64.decodeBase64(encoded.getBytes());
        final String pair = new String(decodedBytes);
        final String[] userDetails = pair.split(":", 2);
        return userDetails;
    }

    private static String createEncodedText(final String username, 
                                            final String password) {
        final String pair = username + ":" + password;
        final byte[] encodedBytes = Base64.encodeBase64(pair.getBytes());
        return new String(encodedBytes);
    }
}

Output of the program:

Input : username [Aladdin], password [open sesame]
Encoded Text : QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Decoded : username [Aladdin], password [open sesame]

As you can see the above program can encode and decode as expected.

Constraints

As per the related RFC (http://www.ietf.org/rfc/rfc2617.txt); username can not contain any colons, but password has no such restrictions. So it is easy to select the username by splitting the string till the first colon is reached.

Risks involved

This encoded string is passed to the server in plain text. Even though the username and password are hidden in a way; as you may have already guessed, it is not safe at all to use http basic authentication as decoding is straightforward and quite simple. So when ever this approach is used, it is advised to use a secure channel like HTTPS rather than HTTP.

COMMENTS

BLOGGER: 9
Loading...

Read More...

Name

About,2,Adsense,3,Ant,1,Apache,3,Axis,3,Blogger,1,Books,1,CentOS,2,Chrome,2,CSS,2,Database,3,Earn Online,3,Eclipse,10,Facebook,1,Firefox,10,Gmail,4,GNU/Linux,9,Google,26,GWT,8,Hardware,2,IE,5,Interesting,15,Internet,14,Java,49,Javascript,7,JBoss,1,Jenkins,1,Log4j,2,Me,6,Microsoft,2,Miscellaneous,1,News,11,Opinion,10,OSGi,1,PHP,1,Productivity,3,Programming,36,Puzzle,3,Security,4,Software,41,Sports,9,Spring,2,Story,6,Subversion,3,TDD,4,Tech,2,Tips,1,Tomcat,6,Tutorial,13,Ubuntu,4,Web application,14,Web Design,2,Web services,3,Windows,10,Yahoo,1,Zip,2,
ltr
item
Digizol: Http basic authentication header: Learn with Java code sample
Http basic authentication header: Learn with Java code sample
Http basic authentication header is a popular mechanism for authentication, specially when it comes to internal applications. With Java, we can handle this header.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJBKNeOLn2oAUBv0x4-5Z4_4QPjoDRfvqJ1ZmLvAmuGrEBLKuZH9a1ZA6tRw7ukLj95Bkf409Y_sqQ8pfiJQQaKO8_52xEc64sHbV4G-0LWOBSCDakNbgVtsnauJq1-9i3zYe80Q/s1600/http+basic+auth+headers+www.digizol.com.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJBKNeOLn2oAUBv0x4-5Z4_4QPjoDRfvqJ1ZmLvAmuGrEBLKuZH9a1ZA6tRw7ukLj95Bkf409Y_sqQ8pfiJQQaKO8_52xEc64sHbV4G-0LWOBSCDakNbgVtsnauJq1-9i3zYe80Q/s72-c/http+basic+auth+headers+www.digizol.com.jpg
Digizol
https://www.digizol.com/2012/06/http-basic-authentication-java-decode.html
https://www.digizol.com/
https://www.digizol.com/
https://www.digizol.com/2012/06/http-basic-authentication-java-decode.html
true
7440473
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy