我试图在一个简单的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]
...
我的项目再简单不过了,我有一个主类Config和Controller。有两个端点: /home (permitAll)和/test (身份验证)。请求/test时会发生上述错误。
Config (@配置):
@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);
}
财务主任(@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。任何帮助都是非常感谢的。
发布于 2022-08-30 02:21:21
喔,简单的错误。我将OAuth2AuthorizedClientService作为param传递,而不是在控制器中自动连接它。
@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;
}
}
https://stackoverflow.com/questions/73530344
复制相似问题