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 authenticated request on an OAuth 2.0 client, you would need to simulate some kind of grant flow with an authorization server.
287
+
However, Spring Security's OAuth 2.0 Client test support can help remove much of this boilerplate.
288
+
289
+
If your client uses OIDC to authenticate, then you can use the `oidcLogin()` `RequestPostProcessor` to configure a `MockMvc` request with an authenticated user.
290
+
The simplest of these would look something like this:
291
+
292
+
[source,java]
293
+
----
294
+
mvc.perform(get("/endpoint").with(oidcLogin()));
295
+
----
296
+
297
+
What this will do is create a mock `OidcUser`, passing it correctly through any authentication APIs so that it's available for your controllers and so on.
298
+
It contains a mock `OidcUserInfo`, a mock `OidcIdToken`, and a mock `Collection` of granted authorities.
299
+
Also, <<a mock `OAuth2AuthorizedClient`,testing-oauth2-client>> associated with the user is registered to an `HttpSessionOAuth2AuthorizedClientRepository`.
300
+
301
+
By default, the user info has no claims, and the id token has the `sub` claim, like so:
302
+
303
+
[source,json]
304
+
----
305
+
{
306
+
"sub" : "user"
307
+
}
308
+
----
309
+
310
+
And the resulting `OidcUser`, were it tested, would pass in the following way:
Or, you can supply all detail via an instance of `OidcUser` like so:
350
+
351
+
[source,java]
352
+
----
353
+
mvc.perform(get("/endpoint")
354
+
.with(oidcLogin().oidcUser(new MyOidcUser())));
355
+
----
356
+
357
+
[[testing-oauth2-login]]
358
+
==== Testing OAuth 2.0 Login
359
+
360
+
Or, if your client uses OAuth 2.0 to authenticate, but not OIDC, then you can use the `oauth2Login()` `RequestPostProcessor` to configure a `MockMvc` request with an authenticated user.
361
+
The simplest of these would look something like this:
What this will do is create a mock `OAuth2User`, passing it correctly through any authentication APIs so that it's available for your controllers and so on.
369
+
It contains a mock set of attributes and a mock `Collection` of granted authorities.
370
+
Also, <<a mock `OAuth2AuthorizedClient`,testing-oauth2-client>> associated with the user is registered to an `HttpSessionOAuth2AuthorizedClientRepository`.
371
+
372
+
By default, the set of attributes contains only `sub`:
373
+
374
+
[source,json]
375
+
----
376
+
{
377
+
"sub" : "user"
378
+
}
379
+
----
380
+
381
+
And the resulting `OAuth2User`, were it tested, would pass in the following way:
Independent of how your user authenticates, there may be other OAuth 2.0 tokens that the request will need in order to communicate with resource servers, say in an integration test.
430
+
431
+
If you need to express an OAuth 2.0 client in your test, then you can use the `oauth2Client()` `RequestPostProcessor` to configure a `MockMvc` request with an authorized client.
432
+
The simplest of these would look something like this:
===== `ClientRegistrationRepository` and `OAuth2AuthorizedClientRepository`
482
+
483
+
Under many circumstances, you will need to supply a registration id so that it can be looked up by exchange filter functions or `@RegisteredOAuth2AuthorizedClient` annotations.
484
+
For this reason, `oauth2Client()` ships with a convenience method:
This, however, doesn't know about your application's `ClientRegistrationRepository`, so calling this does not look up your "facebook" client registration for you.
492
+
493
+
To configure a test with an actual `ClientRegistration` from your `ClientRegistrationRepository` you can do:
Also, `oauth2Client()` doesn't know about your application's `OAuth2AuthorizedClientRepository`, which is what Spring Security uses to resolve `@RegisteredOAuth2AuthorizedClient` annotations.
508
+
To make it available in your controllers, your app will need to be using an `HttpSessionOAuth2AuthorizedClientRepository` so that the token can be retrieved in a thread-safe way.
509
+
510
+
You can isolate this configuration to your test via a test configuration like the following:
511
+
512
+
[source,java]
513
+
----
514
+
@TestConfiguration
515
+
static class TestAuthorizedClientRepositoryConfig {
return new HttpSessionOAuth2AuthorizedClientRepository();
519
+
}
520
+
}
521
+
----
522
+
523
+
[[testing-jwt]]
524
+
==== Testing JWT Authentication
285
525
286
526
In order to make an authorized request on a resource server, you need a bearer token.
527
+
287
528
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
529
All of this can be quite daunting, especially when this isn't the focus of your test.
289
530
@@ -399,6 +640,102 @@ mvc
399
640
400
641
Note that as an alternative to these, you can also mock the `JwtDecoder` bean itself with a `@MockBean` annotation.
401
642
643
+
[[testing-opaque-token]]
644
+
==== Testing Opaque Token Authentication
645
+
646
+
Or, if your resource server is configured for opaque tokens, then this would mean that the bearer token needs to be registered with and verified against an authorization server.
647
+
This can be just as distracting as creating a signed JWT.
648
+
649
+
There are two simple ways that you can overcome this difficulty and allow your tests to focus on authorization and not on representing bearer tokens.
650
+
Let's take a look:
651
+
652
+
===== `opaqueToken()` `RequestPostProcessor`
653
+
654
+
The first way is via a `RequestPostProcessor`.
655
+
The simplest of these would look something like this:
What this will do is create a mock `OAuth2AuthenticatedPrincipal`, passing it correctly through any authentication APIs so that it's available for your authorization mechanisms to verify.
663
+
664
+
By default, the set of attributes that it creates is like this:
665
+
666
+
[source,json]
667
+
----
668
+
{
669
+
"sub" : "user",
670
+
"scope" : "read"
671
+
}
672
+
----
673
+
674
+
And the resulting `OAuth2AuthenticatedPrincipal`, were it tested, would pass in the following way:
0 commit comments