Users will be redirected to log in using Google before they can access our home page.
In the following example, we'll use our authorization server from a previous article.
This time, we need to configure more properties, not just the ClientID and Client Secret:
spring:
security:
oauth2:
client:
registration:
custom:
client-id: fooClientIdPassword
client-secret: secret
scopes: read,foo
authorization-grant-type: authorization_code
redirect-uri-template: http://localhost:8080/login/oauth2/code/custom
provider:
custom:
authorization-uri: http://localhost:8081/spring-security-oauth-server/oauth/authorize
token-uri: http://localhost:8081/spring-security-oauth-server/oauth/token
user-info-uri: http://localhost:8088/spring-security-oauth-resource/users/extra
user-name-attribute: user_name
In this case, we also need to specify the scope, grant type and redirect URI for the OAuth2 client. We'll also provide the authorization and token URI of the Authorization Server.
Finally, we need to configure the UserInfo endpoint as well to be able to get the user authentication details.
6. Security Configuration
By default, Spring Security secures all paths. Therefore, if we have only one OAuth client, we'll be redirected to authorize this client and log in.
If multiple OAuth clients are registered, then a login page will be automatically created to choose the login method.
We can change that if we like and provide a detailed security configuration:
@EnableWebFluxSecurity
public class SecurityConfig {
@Bean
public SecurityWebFilterChain configure(ServerHttpSecurity http) throws Exception {
return http.authorizeExchange()
.pathMatchers("/about").permitAll()
.anyExchange().authenticated()
.and().oauth2Login()
.and().build();
In this example, we've secured all paths except for “/about”.
7. WebClient
We can also do more than just authenticate users using OAuth2. We can use WebClient to access OAuth2 secured resources using OAuth2AuthorizedClient.
Now, let's configure our WebClient:
@Bean
public WebClient webClient(ReactiveClientRegistrationRepository clientRegistrationRepo,
ServerOAuth2AuthorizedClientRepository authorizedClientRepo) {
ServerOAuth2AuthorizedClientExchangeFilterFunction filter =
new ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrationRepo, authorizedClientRepo);
return WebClient.builder().filter(filter).build();
Then, we can retrieve an OAuth2 secured resource:
@Autowired
private WebClient webClient;
@GetMapping("/foos/{id}")
public Mono<Foo> getFooResource(@RegisteredOAuth2AuthorizedClient("custom")
OAuth2AuthorizedClient client, @PathVariable final long id){
return webClient
.get()
.uri("http://localhost:8088/spring-security-oauth-resource/foos/{id}", id)
.attributes(oauth2AuthorizedClient(client))
.retrieve()
.bodyToMono(Foo.class);
Note that we retrieved the remote resource Foo using AccessToken from OAuth2AuthorizedClient.
8. Conclusion
In this quick article, we learned how to configure our WebFlux application to use OAuth2 Login support and how to use WebClient to access OAuth2 secured resources.
As always, the full source code is available over on GitHub.
Course – LS (cat=HTTP Client-Side) res – HTTP Client (eBook) (cat=Http Client-Side)