When using @before, it is only used in one class. How do I apply a global filter in playframework? So that one filter is used for all controller classes.
-
This is related to my question http://stackoverflow.com/questions/4597766/easy-way-to-use-secure-module-or-similar-on-all-controllers-in-play . I tried to use a bytecode enhancer to add a `@With` annotation to all controllers, but that led me to this question: http://stackoverflow.com/questions/4608883/problem-with-classes-not-found-during-playplugin-enhance – Brad Mace Jan 08 '11 at 19:27
-
http://www.playframework.com/documentation/2.1.0/ScalaInterceptors – ThePizzle Mar 06 '13 at 18:06
4 Answers
A simple solution is to extend a base controller for all of your controllers and have the @Before in the base controller.
The other option (and the better solution, as it is more flexible) is to use the @With annotation. The example on the play documentation is
Example:
public class Secure extends Controller {
@Before
static void checkAuthenticated() {
if(!session.containsKey("user")) {
unAuthorized();
}
}
}
And on another Controller:
@With(Secure.class)
public class Admin extends Application {
...
}
This means the Admin controller will process all the interceptors (@Before, @After, @Finally) contained within the Secure controller.

- 54,176
- 10
- 96
- 129
-
2Unfortunately this still requires you to add the `@With` annotation to all controllers. Particularly in the case of the Secure plugin, it would be preferable to be able to make all controllers secure by default and then whitelist those that should be publicly accessible. I haven't figured out how to do that yet though. – Brad Mace Jan 08 '11 at 19:31
-
agreed, I don't like the solution here as every developer has to remember and human behavior says it will be forgotten at some point. – Dean Hiller Feb 17 '12 at 03:05
I did this very thing by handling incoming requests globally in the GlobalSettings class:
This describes the class: http://www.playframework.org/documentation/2.0/JavaGlobal
This describes the method you'd want to override. http://www.playframework.org/documentation/2.0/JavaInterceptors
Here's an example of how I used it in my own project (of course, this is a simplified version of what you're looking for):
@Override
public play.mvc.Action onRequest(play.mvc.Http.Request request, java.lang.reflect.Method method) {
if (request.path().startsWith("/secret/locked")) {
return new Action.Simple() {
@Override
public Result call(play.mvc.Http.Context ctx) throws Throwable {
return redirect(routes.Application.forbidden());
}
};
}
return super.onRequest(request, method);
}

- 4,461
- 2
- 34
- 33
You can simply use PlayPlugin for this issue. See here for more details.
-
Sorry I was in hurry the last time. You can find some information here: http://stackoverflow.com/questions/4515627/how-to-get-beforeactioninvocation-and-afteractioninvocation-to-be-called-in-play. Hope that I will find time to create a more comprehensive explanation. – niels Jan 15 '11 at 10:53
It's not a good solution to extend a base controller for all of your controllers and have the @Before in the base controller.
You can extends the filter or essensialfilter .e.g.
class filter1 extends Filter {}
and apply filter1 to Global

- 331
- 1
- 2
- 8