Привет, я изучаю Spring Security и пытаюсь создать простой клиент OAuth2 и сервер ресурсов на основе рекомендаций https://dzone.com/articles/implement-oa ... pring-boot. -and-spr
Я столкнулся с проблемой, когда компилятор продолжает сообщать, что не может найти компонент для «ClientRegistrationRepository». Я немного покопался в Интернете и обнаружил, что если конфигурации клиента Spring настроены правильно, он должен работать. Кто-то, у кого были похожие проблемы, сказал, что проблема может быть вызвана ошибкой в файле свойств, но я не вижу такого случая.
Могу ли я обратиться за вашей помощью, чтобы узнать, настроено ли что-нибудь неправильно, спасибо.
Вывод в консоль
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method webClient in com.somecompany.configuration.WebClientConfig required a bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' in your configuration.
Основной класс клиента OAuth2
package com.somecompany;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Oauth2DemoClientApplication {
public static void main(String[] args) {
SpringApplication.run(Oauth2DemoClientApplication.class, args);
}
}
Клиентский контроллер OAuth2
package com.somecompany.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
@RestController
@RequestMapping("/api/client")
public class Oauth2DemoClientController {
@Autowired
private WebClient webClient;
@Value("${resourceServer.url}")
private String resourceServerUrl;
@Value("${resourceServer.helloPath}")
private String resourceServerHelloPath;
@GetMapping("/")
public String home(@AuthenticationPrincipal OidcUser user) {
return "Welcome " + user.getFullName();
}
@GetMapping("/hello")
public String sayHello() {
return webClient.get().uri(resourceServerUrl + resourceServerHelloPath).retrieve().bodyToMono(String.class)
.block();
}
}
Конфигурация клиента OAuth2
package com.somecompany.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
import org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class WebClientConfig {
@Value("${defaultClientApplication}")
private String defaultClientApplication;
@Bean
WebClient webClient(ClientRegistrationRepository clientRegistrations,
OAuth2AuthorizedClientRepository authorizedClients) {
ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2 = new ServletOAuth2AuthorizedClientExchangeFilterFunction(
clientRegistrations, authorizedClients);
oauth2.setDefaultOAuth2AuthorizedClient(true);
oauth2.setDefaultClientRegistrationId(defaultClientApplication);
return WebClient.builder().apply(oauth2.oauth2Configuration()).build();
}
}
Клиентское приложение OAuth2.yml
logging.level.root: "debug"
defaultClientApplication: "okta"
spring:
security:
oauth2:
client:
provider:
okta:
issuer-uri: "https://dev-27548664.okta.com/oauth2/default"
registration:
okta:
client-id: {client ID}
client-secret: {client secret}
resourceServer:
url: "http://localhost:8081"
helloPath: "/api/resource/hello"
Клиент OAuth2 pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.4.3
com.somecompany
oauth2-demo-client
1.0.0
oauth2-demo-client
oauth2-demo-client
11
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-oauth2-client
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-webflux
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
io.projectreactor
reactor-test
test
org.springframework.security
spring-security-test
test
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
Подробнее здесь: https://stackoverflow.com/questions/664 ... ient-regis
Невозможно найти bean-компонент <Spring Security> типа «org.springframework.security.oauth2.client.registration.ClientRe ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение