на запрос межсервисного сервиса отвечает статус «Запрещено» в весеннем облачном приложении - PullRequest
0 голосов
/ 17 мая 2018

Я исследую микросервисную архитектуру.Я выбрал Spring Cloud Framework.

Моя прикладная схема выглядит так:

enter image description here

Также у меня есть сервер обнаружения eureka, но я решилчтобы пропустить картинку, чтобы упростить ее.

Полный исходный код примера, который вы можете найти на githib: https://github.com/gredwhite/spring-cloud

Объяснение проблемы:

hello world service:

@GetMapping("/helloWorld")
@HystrixCommand(fallbackMethod = "reliable")
public String hello() {
    return this.restTemplate.getForObject("http://hello-service/hello?name=World", String.class);
}

привет сервис:

@GetMapping("/hello")
public String hello(@RequestParam("name") String name) throws UnknownHostException, InterruptedException {           
     return "Hello " + name + "!";
 }

Когда я запустил hello service и попытался получить доступ к localhost:8082/h/hello?name=Vasya (/h -путь к контексту) - запрос успешно выполнен, и в ответе я вижу сообщение Hello Vasya. Мне нужно сказать, что аутентификация для этой службы отключена.

hello world service имеет страницу index.html, и когда я пытаюсь получить к ней доступ, поток аутентификации происходит успешно, и в конце концов это приложение входит в системууспешно.Затем я пытаюсь выполнить метод /hello из hello world service и вижу ответ:

{"timestamp":"2018-05-17T08:53:04.623+0000","status":403,"error":"Forbidden","message":"Forbidden","path":"/hw/helloWorld"}

Конфигурация Oauth2:

hello world service

@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "say-hello")
@EnableAutoConfiguration
@EnableOAuth2Sso
public class HelloWorldStarter {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldStarter.class, args);
    }


    @RestController
    @EnableDiscoveryClient
    @EnableCircuitBreaker
    public static class HelloWorldController {
        @Autowired
        private RestTemplate restTemplate;
        @Autowired
        private DiscoveryClient discoveryClient;

        @GetMapping("/helloWorld")
        @HystrixCommand(fallbackMethod = "reliable")
        public String hello() {           
            return this.restTemplate.getForObject("http://hello-service/hello?name=World", String.class);
        }

        public String reliable() {
            return "Could not get response from service";
        }
    }

    @org.springframework.context.annotation.Configuration
    public static class Configuration {
        @Bean
        @LoadBalanced
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
}

application.yml:

spring:
  application:
    name: hello-world-service
server:
  port: 8081
  servlet:
    context-path: /hw
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
  instance:
    preferIpAddress: true

security:
  oauth2:
    client:
      client-id: acme
      client-secret: acmesecret
      access-token-uri: http://localhost:8080/oauth/token
      user-authorization-uri: http://localhost:8080/oauth/authorize
    resource:
      user-info-uri: http://localhost:8080/me

logging:
  level:
    org.springframework.security: DEBUG
    org.springframework.web: DEBUG

Вопросы

  1. Как я могу решить эту проблему?
  2. После исправления предыдущего пункта я хочу знать, как выполнить авторизованный запрос к этой службе.Другими словами, я хочу включить авторизацию oauth 2 на сервисе hello и иметь возможность сделать запрос из hello world service

1 Ответ

0 голосов
/ 17 мая 2018

Я думаю, что вы используете очень странный подход для решения своей проблемы.

Я предлагаю вам следующее решение:

  1. Создание сервиса FeignClient.

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

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

}
  1. Добавление oauth2FeignRequestInterceptor в класс приложения SpringBoot

@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. Добавьте несколько аннотаций в класс приложений SpringBoot

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

Это все надежда, что это помогает.

...