Servlet Basic Auth in an OSGi environment
When registering a servlet through the OSGI HTTP Service, it provides you with an option to provide an implementation of HTTPContext.
httpService.registerServlet(alias, new MyServlet(), initParams, null);
When we implement the HTTPContext interface we can implement three methods. Out of these three (3) handleSecurity will be called before serving a request to ermmmâĶ check for security.
public class BasicAuthSecuredContext implements HttpContext{
@Override
public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {
return false;
}
@Override
public URL getResource(String s) {
return null;
}
@Override
public String getMimeType(String s) {
return null;
}
}
So, when implementing this, Iâm borrowing a lot of content from the OSGI HTTPContext documentation and the HTTP Authentication spec. They are a must read, if you are interested in learning a lot, diving into details, etc. or you can just read the rest of this post.
First, it is a big no-no to do basic auth unless https is used. If itâs not there we let the user know itâs forbidden territory. Letâs go ahead and do that.
if (!request.getScheme().equals("https")) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return false;
}
Next, letâs check for the Authorization header. If thatâs not there we let them know, they need that shit to be there. Or we just say theyâre unauthorized. Letâs do that now.
if (request.getHeader("Authorization") == null) {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return false;
}
Ok, two tests passed. Now, we do some real work. Letâs extract the header decode it, and do ânot so properâ authentication.
protected boolean authenticated(HttpServletRequest request) {
String authzHeader = request.getHeader("Authorization");
String usernameAndPassword = new String(Base64.decodeBase64(authzHeader.substring(6).getBytes()));
int userNameIndex = usernameAndPassword.indexOf(":");
String username = usernameAndPassword.substring(0, userNameIndex);
String password = usernameAndPassword.substring(userNameIndex + 1);
// Now, do the authentication against in the way you want, ex: ldap, db stored uname/pw
// Here I will do lame hard coded credential check. HIGHLY NOT RECOMMENDED!
return ((username.equals("username") && password.equals("password"));
}
Letâs integrate this method, into the handleSecurity method. Notice how a meaningful error message is set to the response (line 14) when the security fails. This prevents the user from guessing, and they knows what exactly went wrong. Ermm, at least, if they know HTTP error codes they would know exactly what went wrong.
@Override
public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {
if (!request.getScheme().equals("https")) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return false;
}
if (request.getHeader("Authorization") == null) {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return false;
}
if (authenticated(request)) {
return true;
} else {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return false;
}
}
Thatâs it. Now, pass this object when registering the servlet,
httpService.registerServlet(alias, new MyServlet(), initParams, new BasicAuthSecuredContext());
âĶand behold the power of basic auth in OSGI servlets!
Reference: Implementing Basic Auth for a servlet in an OSGI environment from our JCG partner Mackie Mathew at the dev_religion blog.




