Basic Authentication in Spring WebClient

In this short post we will see how to setup Basic Authentication in Spring WebClient while invoking external APIs.

Overview

WebClient is a non-blocking HTTP client with fluent functional style API. It is part of Spring Webflux module that was introduced in Spring 5. WebClient replaces the RestTemplate to invoke external APIs with non-blocking.

WebClient provides different ways of injecting HTTP headers, query params etc while making external call. In this example we will check how to specify Basic Authentication in Webclient.

Basic Authentication in WebClient

Until Spring 5.1, basic authentication was setup using a custom ExchangeFilterFunction. This way of setting up Basic auth was only available while creating WebClient since it relies on WebClient filters.

private WebClient client = WebClient.builder()
            .filter(ExchangeFilterFunctions
                .basicAuthentication(username, token))
            .build();

Alternatively if we want to provide the Basic auth while calling the API, we have to set the Authorization header manually which is not great!

webClient.get()
            .uri("/customers")
            .header("Authorization", "Basic " + Base64Utils
                    .encodeToString((username + ":" + token).getBytes(UTF_8)))
            .retrieve()
            .bodyToFlux(String.class);

Thankfully, Spring provided some helper methods to make this easy and consistent in Spring 5.1.

Basic Authentication in Spring 5.1 and above

The above way of setting Basic authentication using custom ExchangeFilterFunction is deprecated in Spring 5.1. A new method setBasicAuth is introduced in HttpHeaders class that can be used to set basic authentication.

Below we set use defaultHeaders in WebClient builder to setup Basic auth while creating WebClient instance:

private WebClient client = WebClient.builder()
            .defaultHeaders(header -> header.setBasicAuth(userName, password))
            .build();

Alternately the basic auth can also be setup while calling any API:

Mono<String> response = client.get()
                    .url("/customers")
                    .headers(headers -> headers.setBasicAuth(userName, password))
                    .retrieve()
                    .bodyToFlux(String.class);

Two variants of setBasicAuth methods are available in HttpHeaders.

void    setBasicAuth(String username, String password)
        //Set the value of the Authorization header to Basic Authentication based on the given username and password.

void    setBasicAuth(String username, String password, Charset charset)
        //Set the value of the Authorization header to Basic Authentication based on the given username and password.

Bonus tip – Setting Bearer Token in WebClient

Similar to Basic Auth, we can also setup the Bearer token in WebClient using new method setBearerAuth in HttpHeaders class:

void    setBearerAuth(String token)
        //Set the value of the Authorization header to the given Bearer token.

The process would be exactly similar to setting up the Basic Auth.

Conclusion

The setBasicAuth method in HttpHeaders class makes it easy setting up basic authentication in WebClient Spring WebFlux. The Basic Auth can be setup while building the WebClient or alternatively during invoking APIs using get(), post() etc.



via ViralPatel.net https://ift.tt/2GzfAyG

[Fix] Firefox Sync Not Working for All Preferences (Options)

https://ift.tt/eA8V8J Firefox Sync is a very useful feature of Mozilla Firefox web browser. It allows users to sync their bookmarks (favorites), add-ons,...

Read the full article at AskVG.com

via AskVG https://ift.tt/2SMdEYI

Google’s New Shopping Platform is Now Live in the USA

As Google started observing itself being threatened by Amazon’s product searching ability with its wide scope everywhere, it has taken pains of launching its own and new shopping platform for its users in competition with its emerging rival recently to wipe their efforts of gaining more adverts off. Signed in users can access the platform that has just gone live. Most of the products come from the retailers Google has aligned itself with.

As announced earlier in May at Google Marketing, the new Google Shopping platform has gone live just in the United States so far. Although the engine has been in practice with its test of experiencing in different markets of the world including France, but it has not started functioning anywhere outside the US yet, reports suggest. However, this very exercise rather suggests that sooner or later the users would probably start getting benefits of it in their own regions if the company with its new feature goes live everywhere to benefit itself.

They have adopted the strategy of hinting users with different personalized products or items according to their earlier web search for their instant attraction. The users can purchase products through the Google site, a third-party retailer’s site, or in-store; according to the original statement by the giant.

As announced previously, the tech giant is not intent on initiating its own warehouses or shipping, that means the company will not be selling products directly. Despite the indirect purchase, it will offer things with its guarantee, however. Moreover, the users will also be able to claim a refund or return of the item if the product they had purchased is not accurate or timely.

The surge of the event is supposedly the outcome of Google’s vulnerability over the apparent loss of the different adverts of various companies along with their ad budgets, as due to Amazon’s fame, they are taking their ad budgets there, leaving Google to ponder on introducing new stratagems so that they can get it back. The move is thought to be persuasive enough for their retrieval to the tech giant.

Earlier, when in May 2019 the company had announced the remaking of its shopping experience, it was struggling again to gain product searches from the Amazon. In addition to it, the likes on Instagram were also another emerging danger to its marketing as they were fresh E-commerce exertions.

It is thought that the company has taken a huge risk of competing with Amazon, which is certainly an obvious platform for shopaholics around the globe for purchasing products ranging from home appliances to clothes, books, and to several items of regular or irregular use. It then, becomes suggestive of the fact that it will take some time for this feature to being penetrable among customers of different products, but the reason of Google being the most famous and fitting search engine is also an alarm for Amazon, hence implying that there is no failure in the risk of introducing such a platform for its users who are far-flung on the globe.

The post Google’s New Shopping Platform is Now Live in the USA appeared first on Gtricks.



via Gtricks https://ift.tt/2SDeqae

Getting Started with GraphQL and Spring Boot

[Review] Which DTH Service Provides Maximum HD Channels in India?

https://ift.tt/eA8V8J UPDATED on 25 July, 2019: Addition of 4 HD channels (&Xplor HD, Nat Geo Wild HD, Colors Tamil HD, Zee Keralam...

Read the full article at AskVG.com

via AskVG https://ift.tt/2r22Lrs

[Bug] 2 “General” Tabs in Recycle Bin Properties in All Windows Versions

https://ift.tt/eA8V8J We have discovered and reported so many bugs present in all Windows versions whether it's the latest Windows 10 operating system...

Read the full article at AskVG.com

via AskVG https://ift.tt/2JOF4KF

[Fix] File Explorer Automatically Scrolls / Jumps to Top of Folder in Windows 10

https://ift.tt/eA8V8J Today in this article, we are going to address a very annoying and irritating issue present in Windows 10. Many Windows...

Read the full article at AskVG.com

via AskVG https://ift.tt/2M8to6M

[Software Update] Download Microsoft PowerToys 0.81.0 for Windows 10 and Windows 11

UPDATE: New PowerToys version 0.81.0 available for Windows 10 and Windows 11 which comes with a new utility “Advanced Paste”, various improv...