我正在尝试使用Spring Boot并遇到UserRedirectRequiredException来实现授权代码授予类型。根据我的理解,授权代码似乎无法交换访问令牌代码,下面提到的代码被用于相同的问题。 如果有人可以提供一些见解,例如如何调用使用授权授权类型保护的API,将很有帮助。
我尝试过的事情:
按如下所述在配置类中配置过滤器注册Bean,但这没有帮助:
@Bean
public FilterRegistrationBean<OAuth2ClientContextFilter>
oauth2ClientFilterRegistration(OAuth2ClientContextFilter filter) {
FilterRegistrationBean<OAuth2ClientContextFilter> registration = new
FilterRegistrationBean<OAuth2ClientContextFilter>();
registration.setFilter(filter);
registration.setOrder(-100);
return registration;
}
@RestController
@EnableOAuth2Client
public class ApiController {
@Value("${security.oauth2.client.clientId}")
private String clientId;
@Value("${security.oauth2.client.clientSecret}")
private String clientSecret;
@Value("${security.oauth2.client.accessTokenUri}")
private String accessTokenUri;
@Value(("${security.oauth2.client.userAuthorizationUri}"))
private String userAuthorizationUri;
@Value("${security.oauth2.client.apiUrl}")
private String apiUrl;
@Value("${security.oauth2.client.redirectUri}")
private String redirectUri;
@Autowired
private OAuth2ClientContext oAuth2Context;
@Bean
public OAuth2RestTemplate getOauth2RestTemplate() {
return new OAuth2RestTemplate(resource(),oAuth2Context);
}
@RequestMapping("/api")
public String retrieve() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.set("Accept","application/json");
headers.set("Accept-Encoding","gzip, deflate, br");
String parameters "grant_type=authorization_code&client_id=xxxxxxxxxx&client_secret=xxxxxxxxx";
OAuth2RestTemplate oAuth2RestTemplate = getOauth2RestTemplate();
HttpEntity request = new HttpEntity(parameters,headers);
HttpEntity token = oAuth2RestTemplate.postForEntity(accessTokenUri,
request, String.class);
private OAuth2ProtectedResourceDetails resource() {
AuthorizationCodeResourceDetails authorizationCodeResourceDetails = new
AuthorizationCodeResourceDetails();
authorizationCodeResourceDetails.setClientId(clientId);
authorizationCodeResourceDetails.setClientSecret(clientSecret);
authorizationCodeResourceDetails.setAccessTokenUri(accessTokenUri);
authorizationCodeResourceDetails.
setuserAuthorizationUri(userAuthorizationUri);