Alex Rivera | Logout

How to apply a global filter in playframework

Asked 2011-01-07T08:53:11.523
10

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.

Edit
Report

2 Answers

6

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.

answered 2011-01-07T18:59:21.367
1

You can simply use PlayPlugin for this issue. See here for more details.

answered 2011-01-10T07:11:13.737

Your Answer