You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In order to make an authorized request on a resource server, you need a bearer token.
287
+
If your resource server is configured for JWTs, then this would mean that the bearer token needs to be signed and then encoded according to the JWT specification.
288
+
All of this can be quite daunting, especially when this isn't the focus of your test.
289
+
290
+
Fortunately, there are a number of simple ways that you can overcome this difficulty and allow your tests to focus on authorization and not on representing bearer tokens.
291
+
We'll look at two of them now:
292
+
293
+
===== `jwt() RequestPostProcessor`
294
+
295
+
The first way is via a `RequestPostProcessor`.
296
+
The simplest of these would look something like this:
297
+
298
+
[source,java]
299
+
----
300
+
mvc
301
+
.perform(get("/endpoint").with(jwt()));
302
+
----
303
+
304
+
What this will do is create a mock `Jwt`, passing it correctly through any authentication APIs so that it's available for your authorization mechanisms to verify.
305
+
306
+
By default, the `JWT` that it creates has the following characteristics:
307
+
308
+
[source,json]
309
+
----
310
+
{
311
+
"headers" : { "alg" : "none" },
312
+
"claims" : {
313
+
"sub" : "user",
314
+
"scope" : "read"
315
+
}
316
+
}
317
+
----
318
+
319
+
And the resulting `Jwt`, were it tested, would pass in the following way:
0 commit comments