Skip to content

Commit 1c77cbe

Browse files
authored
Documentation must indicate when a BodyHandler is required (vert-x3#2759)
Instructions were missing for CSRF, SockJS and FormLogin handlers. Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
1 parent a02e902 commit 1c77cbe

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

vertx-web/src/main/asciidoc/index.adoc

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,8 @@ basic authentication handler.
12821282
You will also need to setup handlers to serve your actual login page, and a handler to handle the actual login itself.
12831283
To handle the login we provide a prebuilt handler {@link io.vertx.ext.web.handler.FormLoginHandler} for the purpose.
12841284

1285+
IMPORTANT: The {@link io.vertx.ext.web.handler.FormLoginHandler} requires a {@link io.vertx.ext.web.handler.BodyHandler} to read `POST` requests content.
1286+
12851287
Here's an example of a simple app, using a redirect auth handler on the default redirect url `/loginpage`.
12861288

12871289
[source,$lang]
@@ -2241,12 +2243,11 @@ Please see the https://github.com/sockjs/sockjs-client[SockJS website] for more
22412243

22422244
=== SockJS handler
22432245

2244-
Vert.x provides an out of the box handler called {@link io.vertx.ext.web.handler.sockjs.SockJSHandler} for
2245-
using SockJS in your Vert.x-Web applications.
2246+
Vert.x provides an out-of-the-box handler called {@link io.vertx.ext.web.handler.sockjs.SockJSHandler} for using SockJS in your Vert.x-Web applications.
22462247

22472248
You should create one handler per SockJS application using {@link io.vertx.ext.web.handler.sockjs.SockJSHandler#create}.
2248-
You can also specify configuration options when creating the instance. The configuration options are described with
2249-
an instance of {@link io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions}.
2249+
You can also specify configuration options when creating the instance.
2250+
The configuration options are described with an instance of {@link io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions}.
22502251

22512252
[source,$lang]
22522253
----
@@ -2267,13 +2268,16 @@ is loaded using {@link io.vertx.ext.web.handler.sockjs.SockJSSocket#routingConte
22672268
the users and sessions accessible through {@link io.vertx.ext.web.handler.sockjs.SockJSSocket#webSession()}
22682269
and {@link io.vertx.ext.web.handler.sockjs.SockJSSocket#webUser()}.
22692270

2270-
Here's an example of a simple SockJS handler that simply echoes back any back any data that it reads:
2271+
Here's an example of a simple SockJS handler that simply echoes back any data that it reads:
22712272

22722273
[source,$lang]
22732274
----
22742275
{@link examples.WebExamples#example44}
22752276
----
22762277

2278+
IMPORTANT: The {@link io.vertx.ext.web.handler.sockjs.SockJSHandler} requires a {@link io.vertx.ext.web.handler.BodyHandler} to read `POST` requests content.
2279+
This is useful when WebSockets are not available (disabled by intermediate proxies or by configuration).
2280+
22772281
=== The client side
22782282

22792283
In client side JavaScript you use the SockJS client side library to make connections. For convenience the package is
@@ -2656,6 +2660,8 @@ CSRF or sometimes also known as XSRF is a technique by which an unauthorized sit
26562660
Vert.x-Web includes a handler {@link io.vertx.ext.web.handler.CSRFHandler} that you can use to prevent cross site
26572661
request forgery requests.
26582662

2663+
IMPORTANT: The {@link io.vertx.ext.web.handler.CSRFHandler} requires a {@link io.vertx.ext.web.handler.BodyHandler} to read the content of requests having an unsafe method (`POST`, `PUT`, ...etc.)
2664+
26592665
On each get request under this handler a cookie is added to the response with a unique token. Clients are then
26602666
expected to return this token back in a header. Since cookies are sent it is required that the cookie handler is also
26612667
present on the router.
@@ -2671,16 +2677,16 @@ header was present in the Form attributes under the same name as the header, e.g
26712677
</form>
26722678
----
26732679

2674-
It is the responsibility of the user to fill in the right value for the form field. Users who prefer to use an HTML
2675-
only solution can fill this value by fetching the the token value from the routing context under the key `X-XSRF-TOKEN`
2680+
It is the responsibility of the user to fill in the right value for the form field. Users who prefer to use an HTML only solution can fill this value by fetching the token value from the routing context under the key `X-XSRF-TOKEN`
26762681
or the header name they have chosen during the instantiation of the `CSRFHandler` object.
26772682

26782683
[source,$lang]
26792684
----
26802685
{@link examples.WebExamples#example54}
26812686
----
26822687

2683-
Note that this handler is session aware. If there is a session available the form parameter or header might be omited
2688+
Note that this handler is session aware.
2689+
If there is a session available the form parameter or header might be omitted
26842690
during the `POST` action as it will be read from the session. This also implies that tokens will only be regenerated
26852691
on session upgrades.
26862692

vertx-web/src/main/java/examples/WebExamples.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,9 @@ public void example39(Vertx vertx, AuthenticationProvider authProvider, Router r
852852

853853
// Handle the actual login
854854
// One of your pages must POST form login data
855-
router.post("/login").handler(FormLoginHandler.create(authProvider));
855+
router.post("/login")
856+
.handler(BodyHandler.create())
857+
.handler(FormLoginHandler.create(authProvider));
856858

857859
// Set a static server to serve static resources, e.g. the login page
858860
router.route().handler(StaticHandler.create());
@@ -980,6 +982,9 @@ public void example44(Vertx vertx) {
980982

981983
SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options);
982984

985+
// Required to handle SockJS traffic when WebSockets are not available
986+
router.post("/myapp/*").handler(BodyHandler.create());
987+
983988
router.route("/myapp/*")
984989
.subRouter(sockJSHandler.socketHandler(sockJSSocket -> {
985990

@@ -1274,6 +1279,8 @@ public void example53(Vertx vertx) {
12741279

12751280
public void example54(Vertx vertx, Router router) {
12761281

1282+
// Required to handle request having an unsafe method (`POST`, `PUT`, ...etc.)
1283+
router.route().handler(BodyHandler.create());
12771284
router.route().handler(CSRFHandler.create(vertx, "abracadabra"));
12781285
router.route().handler(ctx -> {
12791286

vertx-web/src/main/java/io/vertx/ext/web/handler/impl/CSRFHandlerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private void handleUnsafeMethod(RoutingContext ctx) {
267267
if (ctx.body().available()) {
268268
header = ctx.request().getFormAttribute(headerName);
269269
} else {
270-
ctx.fail(new VertxException("BodyHandler is required to process POST requests", true));
270+
ctx.fail(new VertxException("BodyHandler is required to process unsafe methods", true));
271271
return;
272272
}
273273
}

0 commit comments

Comments
 (0)