swagger+jwt的配置
本帖最后由 逸帅 于 2021-7-3 23:57 编辑记录一下swagger的配置(包括jwt令牌)
记录一下这一套配置,我还在写这个项目
吐槽:swagger2写jwt的认证贼麻烦
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
Docket docket = new Docket(DocumentationType.SWAGGER_2);
ApiInfoBuilder builder = new ApiInfoBuilder();
builder.title("小秋在线办公系统")
.description("一个比较大型的,前后端分离的,基于微信小程序开发的软件")
.termsOfServiceUrl("https://www.52pojie.cn/home.php?mod=space&uid=1131846")
.version("1.0")
.build();
ApiInfo info = builder.build();
docket.apiInfo(info);
ApiSelectorBuilder selectorBuilder = docket.select();
//配置扫描所有包下面的所有方法
selectorBuilder.paths(PathSelectors.any());
//限制所有方法中,带ApiOperation注解的方法,就会添加到swagger
selectorBuilder.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class));
//构建后重新赋值
docket = selectorBuilder.build();
//设置swagger整合JWT的认证
//设置令牌在header中,名为token,描述为token
ApiKey apiKey = new ApiKey("token", "token", "header");
List<ApiKey> apiKeyList = new ArrayList<>();
apiKeyList.add(apiKey);
docket.securitySchemes(apiKeyList);
//设置令牌起作用的范围为全局
AuthorizationScope scope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] scopes = {scope};
SecurityReference reference = new SecurityReference("token", scopes);
List refList = new ArrayList();
refList.add(reference);
//需要封装到securityContexts才能被docket添加
SecurityContext context = SecurityContext.builder().securityReferences(refList).build();
List cxtList = new ArrayList();
cxtList.add(context);
docket.securityContexts(cxtList);
return docket;
}
}
页:
[1]