简体   繁体   中英

How to authenticate with a bearer token when using Spring Rest Docs

Can I use Spring RestDocs to document APIs when the authorization mode is bearer ?

A pointer to a simple example would be appreciated, it doesn't seem the be available on the Spring RestDocs pages.

Thanks

It's actually very simple.

First obtain a bearer-token and encode it so you can use it as a string.
Next add the header to your call, like: .header("Authorization", "Bearer " + "your encoded token")

TestClient.put().uri("/v1/some_endpoint/foos")
                    .header("Authorization", "Bearer " + "your encoded token")
                    .contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON)
                    .body(BodyInserters.fromValue("{"pay":"load"}))
                    .exchange()
                    .expectStatus().isOk();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM