so when a user(client) sends the request through gateway it goes to the secured service which also calls another service which is secured in its call. iv set up the security by oauth and using feign interface to send calls to this service and need to add a header which includes the access token. I tried many ways but all the classes there seem to be deprecated. pls help me
@Component
@Data
public class UserContextFeignInterceptor implements RequestInterceptor { //adds interceptor to feign clients
@Override
public void apply(RequestTemplate template) {
template.header("tmx-correlation-id", UserContextHolder.getContext().getCorrelationId());
template.header("tmx-auth-token", UserContextHolder.getContext().getAuthToken());
template.header("tmx-user-id", UserContextHolder.getContext().getUserId());
template.header("tmx-organization-id", UserContextHolder.getContext().getOrganizationId());
template.header("Authentication",);
}
}
@FeignClient(name = "organization-service", configuration = FeignConfiguration.class) //add the config of which interceptor to use
public interface OrganizationFeignClient {
@LoadBalanced//spring cloud load balancer(client side)
@GetMapping("v1/organization/{organizationId}")
Organization getOrganization(@PathVariable("organizationId") String organizationId);
}
in this how do i add the current access token and what about if the call is schedules and token expires?
I would approach this problem this way
Then configure your Feign client to use this interceptor:
feignClient.setBearerToken(authToken);now for the scheduled tasks where the token may expire, you would need to refresh the token before making the Feign call. You can use an
OAuth2RefreshTokenGrantto get a new access token. I would say take a look at OAuth 2.0 Client Credentials flow which uses a token without an expiry for microservice-to-microservice calls.-- Hope this helps