![](/img/trans.png)
[英]Defining a bean of type OAuth2AuthorizedClientService in your configuration consider. Parameter 0 of constructor in LoginController required a Bean
[英]No primary or single unique constructor found for interface OAuth2AuthorizedClientService
我试图在一个简单的 OAuth2 Spring 应用程序中接收访问令牌的字符串形式。 似乎最好的方法是使用 OAuth2AuthorizedClientService 。 但是,每当尝试使用它时,我都会收到以下错误:
java.lang.IllegalStateException: No primary or single unique constructor found for interface org.springframework.security.oauth2.client.OAuth2AuthorizedClientService
at org.springframework.beans.BeanUtils.getResolvableConstructor(BeanUtils.java:267) ~[spring-beans-5.3.21.jar:5.3.21]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:219) ~[spring-web-5.3.21.jar:5.3.21]
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:85) ~[spring-webmvc-5.3.22.jar:5.3.22]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:147) ~[spring-web-5.3.21.jar:5.3.21]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122) ~[spring-web-5.3.21.jar:5.3.21]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:179) ~[spring-web-5.3.21.jar:5.3.21]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:146) ~[spring-web-5.3.21.jar:5.3.21]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.22.jar:5.3.22]
...
我的项目再简单不过了,我有一个主 class、Config 和 Controller。 有两个端点; /home (permitAll) 和 /test (authenticated)。 请求 /test 时出现上述错误。
配置(@Configuration):
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.mvcMatchers("/test/**").authenticated()
.mvcMatchers("/home/**").permitAll()
// mvcMatchers more secure than antMatchers as it infers other things in path
// regexMatchers allows you to use any regex for matching paths
.and().oauth2Login();
return http.build();
}
private ClientRegistration clientRegistration() {
return CommonOAuth2Provider.GITHUB.getBuilder("github").clientId("<not sharing>")
.clientSecret("<not sharing>")
.scope("user:email").build();
}
@Bean
public ClientRegistrationRepository clientRepository() {
ClientRegistration clientReg = clientRegistration();
return new InMemoryClientRegistrationRepository(clientReg);
}
Controller(@RestController):
@GetMapping("/home")
public String home() {
return "home page";
}
@GetMapping("/test")
public OAuth2AuthenticationToken testToken(OAuth2AuthenticationToken token,
OAuth2AuthorizedClientService svc) {
var t = svc.loadAuthorizedClient(token.getAuthorizedClientRegistrationId(), token.getName());
System.out.println(t.getAccessToken().getTokenValue());
return token;
}
我想我的问题可能是 Maven 依赖项。 但是,使用上面的文章作为参考,我的依赖项似乎不是问题。 它们如下所示:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.2</version>
</dependency>
我正在使用 jdk-16.0.1。 任何帮助表示赞赏。
哎呀,简单的错误。 我将 OAuth2AuthorizedClientService 作为参数传递,而不是在 controller 中自动装配它。
@Autowired
private OAuth2AuthorizedClientService service;
@GetMapping("/home")
public String home() {
return "home page";
}
@GetMapping("/test")
public OAuth2AuthenticationToken testToken(OAuth2AuthenticationToken token) {
var t = service.loadAuthorizedClient(token.getAuthorizedClientRegistrationId(), token.getName());
System.out.println(t.getAccessToken().getTokenValue());
return token;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.