我正在将Spring Boot与Rest服务一起使用,每种方法都工作正常,但是现在我添加了SWAGGER UI,并希望使用clientID和Client Secret生成Bearer令牌。我做了所有代码,但作为回应,我得到了,
{"error":"invalid_client","error_description":"Browser requests to the token endpoint must use Proof Key for Code Exchange."}
我已经尝试了多种方法来解决它,但总是运气不好
import static org.hibernate.validator.internal.util.CollectionHelper.newArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.ClientCredentialsGrant;
import springfox.documentation.service.Contact;
import springfox.documentation.service.GrantType;
import springfox.documentation.service.OAuth;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.ApiKeyVehicle;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
@ComponentScan(basePackages = "com.*")
public class SwaggerConfigNew {
private String accessTokenUri = "URL";
public static final String securitySchemaOAuth2 = "oauth2schema";
public static final String authorizationScopeGlobal = "global";
public static final String authorizationScopeGlobalDesc = "accessEverything";
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.*"))
.build().securityContexts(Collections.singletonList(securityContext()))
.securitySchemes(Arrays.asList(securitySchema())).apiInfo(apiInfo());
}
private OAuth securitySchema() {
List<AuthorizationScope> authorizationScopeList = newArrayList();
authorizationScopeList.add(new AuthorizationScope("read", "read all"));
authorizationScopeList.add(new AuthorizationScope("write", "access all"));
List<GrantType> grantTypes = newArrayList();
GrantType grantType = new ClientCredentialsGrant(accessTokenUri);
grantTypes.add(grantType);
return new OAuth("oauth2", authorizationScopeList, grantTypes);
}
private SecurityContext securityContext() {
return SecurityContext.builder().securityReferences(defaultAuth()).build();
}
private List<SecurityReference> defaultAuth() {
final AuthorizationScope[] authorizationScopes = new AuthorizationScope[3];
authorizationScopes[0] = new AuthorizationScope("read", "read all");
authorizationScopes[1] = new AuthorizationScope("trust", "trust all");
authorizationScopes[2] = new AuthorizationScope("write", "write all");
return Collections.singletonList(new SecurityReference("oauth2", authorizationScopes));
}
@Bean
public SecurityConfiguration security() {
return new SecurityConfiguration("client", "secret", "", "", "Bearer access token", ApiKeyVehicle.HEADER,
HttpHeaders.AUTHORIZATION, "");
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Authentication API").description("build 18-Apr-2018")
.contact(new Contact("Badr Kacimi", "www.hiveprod.com", "")).license("hiveprod")
.licenseUrl("\"https://www.apache.org/licenses/LICENSE-2.0").build();
}
}
我希望使用Bearer令牌,以便我的服务代码可以验证该令牌