Я исследую микросервисную архитектуру.Я выбрал Spring Cloud Framework.
Моя прикладная схема выглядит так:
![enter image description here](https://i.stack.imgur.com/yvQ9E.jpg)
Также у меня есть сервер обнаружения 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
Вопросы
- Как я могу решить эту проблему?
- После исправления предыдущего пункта я хочу знать, как выполнить авторизованный запрос к этой службе.Другими словами, я хочу включить авторизацию oauth 2 на сервисе hello и иметь возможность сделать запрос из
hello world service