Class CsrfProtected
- All Implemented Interfaces:
Element
You add this element before the routes that should be protected, just like any other element:
public class MySite extends Site {
public void setup() {
before(new CsrfProtected());
post("/transfer", c -> { ... });
}
}
The state-changing request methods POST, PUT,
PATCH and DELETE will be verified, while the ones that
only read aren't, since they establish the token instead. On a page
whose token was established this way, the route:inputs: filtered
tag will include the token in the forms that it generates, while the
context:csrfToken template value provides the token on its own
for the requests that JavaScript performs.
A request that doesn't provide a valid token will be refused, which
means that the refused(rife.engine.Context, rife.engine.exceptions.CsrfTokenException) hook produces the response and that the
processing stops before the protected route runs. By default this sends
403 Forbidden with a short message, but you can override the
hook to tailor the response, for instance to render a template or to
redirect to a page that reloads:
before(new CsrfProtected() {
protected void refused(Context c, CsrfTokenException e) {
c.setStatus(Context.SC_FORBIDDEN);
c.print(c.template("reload"));
}
});- Since:
- 1.10
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Set<RequestMethod>The state-changing request methods that are verified by default, which arePOST,PUT,PATCHandDELETE. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance that verifies thestate-changing request methods.CsrfProtected(Set<RequestMethod> protectedMethods) Creates a new instance that verifies the provided request methods. -
Method Summary
Modifier and TypeMethodDescriptionvoidProcess the providedContextwith this element.protected voidrefused(Context c, CsrfTokenException e) Provides the response for a request whose token couldn't be verified, called before the protected route runs.
-
Field Details
-
DEFAULT_PROTECTED_METHODS
The state-changing request methods that are verified by default, which arePOST,PUT,PATCHandDELETE.- Since:
- 1.10
- See Also:
-
-
Constructor Details
-
CsrfProtected
public CsrfProtected()Creates a new instance that verifies thestate-changing request methods.- Since:
- 1.10
- See Also:
-
CsrfProtected
Creates a new instance that verifies the provided request methods.The methods that aren't provided will not be verified, since they only establish the token. If you want to protect
GETas well, for instance:new CsrfProtected(EnumSet.of(RequestMethod.GET, RequestMethod.POST))
- Parameters:
protectedMethods- the request methods that are verified- Since:
- 1.10
- See Also:
-
-
Method Details
-
process
Description copied from interface:ElementProcess the providedContextwith this element. -
refused
Provides the response for a request whose token couldn't be verified, called before the protected route runs.This sends
403 Forbiddenwith a short message by default. You can override this method to tailor the response and the element will stop the processing afterwards, so that the protected route is never reached. A hook that interrupts the processing itself, by callingredirectorrespond, is equally fine.- Parameters:
c- the context of the refused requeste- the exception that describes why the token was refused, eitherCsrfTokenMissingExceptionorCsrfTokenInvalidException- Since:
- 1.10
-