Last active
December 16, 2015 00:29
-
-
Save danielreuterwall/5347792 to your computer and use it in GitHub Desktop.
Global.java for basic auth with Play 2.0
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
import java.lang.reflect.Method; | |
import play.Application; | |
import play.Logger; | |
import play.GlobalSettings; | |
import play.Configuration; | |
import play.mvc.Http.Request; | |
import play.mvc.Action; | |
import security.basicauth.BasicAuth; | |
import security.basicauth.AnythingGoesValidator; | |
public class Global extends GlobalSettings { | |
Boolean basicAuth; | |
BasicAuth authenticator; | |
@Override | |
public void onStart(Application app) { | |
Configuration configuration = app.configuration(); | |
basicAuth = configuration.getBoolean("basicAuth.enable"); | |
if(basicAuth == null) { | |
basicAuth = false; | |
} | |
if(basicAuth) { | |
authenticator = new BasicAuth(configuration.getString("basicAuth.realm"),new AnythingGoesValidator()); | |
} | |
} | |
@Override | |
public Action onRequest(Request request, Method actionMethod) { | |
if(basicAuth && !authenticator.authenticate(request, actionMethod)) { | |
return authenticator.getUnauthorizedAction(); | |
} | |
return super.onRequest(request, actionMethod); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment