I'm writing my app using play framework v. 2.5.3 and use CSRF protection as it is described in official documentation.
public class Filters implements HttpFilters {
@Inject
CSRFFilter csrfFilter;
@Override
public EssentialFilter[] filters() {
return new EssentialFilter[]{csrfFilter.asJava()};
}}
Of course, it works, as long as all of requests need to be filtered, but some of them should be bypassed. How can filters be configured to bypass requests to some specified route? Thanks for your help!
You can decorate
CSRFFilter
and use a list of route paths to either include or exclude the application of the filter.The route paths will need to be in the compiled form, so a route like ´/foo/bar´ would be
/profile
but a route with dynamic components like/view/:foo/:bar
becomes/view/$foo<[^/]+>/$bar<[^/]+>
. You can list the compiled versions of the routes by going to an unmapped URL (e.g.http://localhost:9000/@foo
) when in development mode.It's brute force, and you have to keep your filters in sync with the inclusion/exclusion list, but it works.
Alternatively, you can use comments in the
routes
file to determine which routes should not have the CSRF filter applied.For a
routes
file likeThis filter implementation will not apply the CSRF filter to any action whose route is preceded by
# NOCSRF
. For this example, only/something/else
will have the CSRF filter applied to it.Your
Filters
definition then becomesDon't forget to create a binding for
MaybeCsrfFilter
!