Springboot+Swagger+Oauth2 密码模式登录后,请求中无token
Springboot+Swagger+Oauth2 密码模式登录可以正常进入,但是请求接口的时候Header中没有token存在,接口返回401未授权。请问我应该怎么修改Swagger配置
以下是我的Swagger配置类
@Bean public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(swaggerEnabled)
.select()
.apis(RequestHandlerSelectors.basePackage("com.polly.robot.web"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo())
.securitySchemes(Collections.singletonList(securitySchemes()))
.securityContexts(Collections.singletonList(securityContexts()));
}
/**
* 这个方法主要是写一些文档的描述
*/
private ApiInfo apiInfo() {
return new ApiInfo(
title,
"",
"1.0.0",
"",
new Contact("", contactUrl, email),
"", "", Collections.emptyList());
}
/**
* 认证方式使用密码模式
*/
private SecurityScheme securitySchemes() {
GrantType grantType = new ResourceOwnerPasswordCredentialsGrant(authServer);
return new OAuthBuilder()
.name("assess_token")
.grantTypes(Collections.singletonList(grantType))
.scopes(Arrays.asList(scopes()))
.build();
}
/**
* 设置 swagger2 认证的安全上下文
*/
private SecurityContext securityContexts() {
return SecurityContext.builder()
.securityReferences(Collections.singletonList(new SecurityReference("assess_token", scopes())))
.forPaths(PathSelectors.any())
.build();
}
/**
* 允许认证的scope
*/
private AuthorizationScope[] scopes() {
AuthorizationScope authorizationScope = new AuthorizationScope("server", "接口测试");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return authorizationScopes;
}
Swagger引用:
<dependency> <groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
以上是 Springboot+Swagger+Oauth2 密码模式登录后,请求中无token 的全部内容, 来源链接: utcz.com/p/944156.html