← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9707: Added util method for basic auth

 

------------------------------------------------------------
revno: 9707
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-02-04 12:13:08 +0200
message:
  Added util method for basic auth
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.java	2012-12-19 11:05:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.java	2013-02-04 10:13:08 +0000
@@ -33,6 +33,8 @@
 import org.hisp.dhis.setting.SystemSettingManager;
 import org.hisp.dhis.system.util.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpHeaders;
+import org.springframework.security.crypto.codec.Base64;
 import org.springframework.stereotype.Component;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
@@ -258,4 +260,19 @@
 
         return builder.toString();
     }
+
+    /**
+     * Adds basic authentication by adding an Authorization header to the
+     * given HttpHeaders object.
+     * 
+     * @param headers the HttpHeaders object.
+     * @param username the user name.
+     * @param password the password.
+     */
+    public static void setBasicAuth( HttpHeaders headers, String username, String password )
+    {
+        String authorisation = username + ":" + password;        
+        byte[] encodedAuthorisation = Base64.encode( authorisation.getBytes() );        
+        headers.add( "Authorization", "Basic " + new String( encodedAuthorisation ) );
+    }
 }