этот?
ледую микросервисную архитектуру. Я выбрал каркас весеннего облака.
Моя заявка Шема выглядит так:
Также у меня есть сервер обнаружения eureka, но я решил пропустить картинку, чтобы упростить ее.
Полный исходный код примера вы можете найти на githib:https://github.com/gredwhite/spring-cloud
Объяснение проблемы:привет мир сервис:
@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:привет мировой сервис
@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