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]
...
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.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;
}