Я хочу реализовать демонстрационное приложение, которое имеет клиентское приложение и приложение сервера ресурсов. Клиентское приложение должно войти в систему с помощью Google и отправить очень простой запрос на сервер ресурсов. Сервер ресурсов должен принять этот токен и предоставить ответ.
Клиент работает нормально, проблема в том, что запрос к localhost: 8080 / api возвращает 401 код статуса Http, и я не знаю почему. Я почти уверен, что токен должен быть проверен на сервере ресурсов, и я не предоставил URL-адрес от Google для его проверки.
клиентское приложение pom:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
И основной класс с демонстрационными контроллерами:
@RestController
@SpringBootApplication
public class OAuth2DemoApplication_2_1 {
private String resourceServerUrl = "http://localhost:8081";
private WebClient webClient;
public OAuth2DemoApplication_2_1(WebClient webClient) {
this.webClient = webClient;
}
public static void main(String[] args) {
SpringApplication.run(OAuth2DemoApplication_2_1.class, args);
}
@GetMapping("/")
String home(@AuthenticationPrincipal OidcUser user) {
return "Hello " + user.getFullName();
}
@GetMapping("/api")
String api() {
return this.webClient
.get()
.uri(this.resourceServerUrl + "/api")
.retrieve()
.bodyToMono(String.class)
.block();
}
@Configuration
public static class OktaWebClientConfig {
@Bean
WebClient webClient(
ClientRegistrationRepository clientRegistrations, OAuth2AuthorizedClientRepository authorizedClients
) {
ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2 =
new ServletOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, authorizedClients);
oauth2.setDefaultOAuth2AuthorizedClient(true);
oauth2.setDefaultClientRegistrationId("google");
return WebClient.builder()
.apply(oauth2.oauth2Configuration())
.build();
}
}
}
А это пом. xml моего ресурса- сервер
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
И основной класс:
@RestController
@SpringBootApplication
public class OAuth2DemoResourceServer_2_1 {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(OAuth2DemoResourceServer_2_1.class);
app.setDefaultProperties(Collections.singletonMap("server.port", "8081"));
app.run(args);
}
@GetMapping("/api")
String api() {
return "Made it to protected api on resource server!";
}
}
application.properties для обоих приложений выглядят так:
spring.security.oauth2.client.registration.google.clientId: 1067617345894-r92gmruc44ri8823cbppdbi9i3npu2he.apps.googleusercontent.com
spring.security.oauth2.client.registration.google.clientSecret: 6NYMx3-sNrrS-p62gWjLgK15