How to invoke a REST service protected by BasicAuth using Spring Open Feign?

Invoking REST services from Spring is much easier if you use Spring Open Feign.

It allows you to invoke REST services declaratively and saves a lot of code.

Here is the post explaining the basic concept of Open Feign:

How to call a REST service declaratively using Open Feign?

It is a very simple example with no authentication involved.

I went on to protect the REST service I invoked using Open Feign, with Basic Authentication.

And then when I tried to hit a REST service which internally used Open Feign to invoke the now protected REST service, I got the below error:

That is because I didn’t customize Open Feign to deal with authentication.

Below is the FeignClient code as explained in the above link:

package com.springboot.openfeign;

import java.util.List;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(name = "personclient", url = "http://localhost:8090/")
public interface PersonClient {

	@RequestMapping(method = RequestMethod.GET, value = "/persons")
	List<Person> getPersons();
}

To enable authentication , add authorization header as a parameter to the above getPersons() method. Here is the updated code:

package com.springboot.openfeign;

import java.util.List;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(name = "personclient", url = "http://localhost:8090/")
public interface PersonClient {

	@RequestMapping(method = RequestMethod.GET, value = "/persons")
	List<Person> getPersons(@RequestHeader("Authorization") String header);
}

I have added a @RequestHeader annotation with ‘Authorization’ as a parameter to it.

To pass this authorization header while invoking the above rest service , build the basic authentication header as below:

package com.springboot.openfeign;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestConsumer {

	@Autowired
	private PersonClient client;

	@GetMapping("/getAllPersons")
	public List<Person> getAllPersons() {

		String username = "admin";
		String password = "admin";

		byte[] encodedBytes = Base64Utils.encode((username + ":" + password).getBytes());

		String authHeader = "Basic " + new String(encodedBytes);

		return client.getPersons(authHeader);
	}
}

After making the above changes , I hit the service again:

I got the response!

Here is the github url for the entire code of feign client:

https://github.com/vijaysrj/openfeign

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s