OAuth2 with Spring WebClient

Supun Sasanka
2 min readMay 3, 2019

Spring Security 5 provides OAuth2 support for Spring Webflux’s non-blocking WebClient class. Here we are going to discuss how to configure WebClient to access OAuth2 protected REST resources.

For example I’m going use “client credentials” grant type for the configuration. But the steps are same for the any grant type.

Step 01 : Dependencies

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

Step 02 : Configure the properties

Fist of all we have to set the OAuth2 credentials in the application.properties or application.yml file.

application.yml should be configured as follows

spring:
application:
name: outh2_WebClient

security:
oauth2:
client:
provider:
authProvider:
token-uri: https://authservice.com/apicall/token
registration:
authProvider…

--

--