Created
May 4, 2012 13:39
-
-
Save joergviola/2594860 to your computer and use it in GitHub Desktop.
Basic Auth a whole Play 2.0 Application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Global extends GlobalSettings { | |
@Override | |
public Action onRequest(Request arg0, Method arg1) { | |
return new Action.Simple() { | |
public Result call(Context ctx) throws Throwable { | |
String authConf = Config.getString("basic.auth"); | |
if (authConf == null) | |
return delegate.call(ctx); | |
String auth = ctx.request().getHeader("Authorization"); | |
if (auth == null) | |
return authorize(ctx); | |
String[] comps = auth.split(" "); | |
if (comps.length != 2) | |
return authorize(ctx); | |
if (!"Basic".equals(comps[0])) | |
return authorize(ctx); | |
BASE64Decoder decoder = new sun.misc.BASE64Decoder(); | |
String credentials = new String(decoder.decodeBuffer(comps[1])); | |
if (authConf.equals(credentials)) | |
return delegate.call(ctx); | |
return authorize(ctx); | |
} | |
private Result authorize(Context ctx) { | |
ctx.response().setHeader("WWW-Authenticate", | |
"Basic realm=\"Secure-Me\""); | |
return unauthorized(); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment