inter micro-service request responds with Forbidden status in spring cloud application

久未见 提交于 2019-11-29 18:17:27

I think you use very strange approach to solve your problem.

I suggest you the following solution:

  1. Create FeignClient service.

@FeignClient(name = "hello-service", url = "http://hello-service")
public interface HelloService {

    @RequestMapping(method = RequestMethod.GET, value = "/hello")
    String hello(@PathVariable("name") String name);

}
  1. Add oauth2FeignRequestInterceptor into SpringBoot Application class

@Bean
    public RequestInterceptor oauth2FeignRequestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();

                requestTemplate.header("Authorization", "bearer " + details.getTokenValue());
            }
        };
    }
  1. Add several annotation into your SpringBoot Application class

@EnableOAuth2Client
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableFeignClients
public class HelloWorldStarter

That's all hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!