1. 每一個請求都須要換取key:java
@Bean public Docket createRestApi() { //添加head參數start ParameterBuilder appId = new ParameterBuilder(); ParameterBuilder tokenCode = new ParameterBuilder(); List<Parameter> pars = new ArrayList<Parameter>(); appId.name("AppId").description("客戶端編號").modelRef(new ModelRef("string")).parameterType("header").required(false).build(); tokenCode.name("AppToken").description("Token令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build(); pars.add(appId.build()); pars.add(tokenCode.build()); return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.inuo.project.tool.swagger")) .paths(PathSelectors.any()).build() .globalOperationParameters(pars) // 詳細定製 .apiInfo(apiInfo()); } /** * 添加摘要信息 */ private ApiInfo apiInfo() { // 用ApiInfoBuilder進行定製 return new ApiInfoBuilder().title("標題:XX管理系統_接口文檔").description("描述:用於管理集團旗下公司的人員信息,具體包括XXX,XXX模塊...") .contact(new Contact(inuoConfig.getName(), null, null)).version("版本號:" + inuoConfig.getVersion()) .build(); }
2. 全局Keyapi
@Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2). useDefaultResponseMessages(false) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.regex("^(?!auth).*$")) .build() .securitySchemes(securitySchemes()) .securityContexts(securityContexts()) ; } private List<ApiKey> securitySchemes() { List<ApiKey> list = new ArrayList<ApiKey>(); ApiKey ak = new ApiKey("Authorization", "Authorization", "header"); list.add(ak); return list; } private List<SecurityContext> securityContexts() { List<SecurityContext> list = new ArrayList<SecurityContext>(); SecurityContext sc = SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.regex("^(?!auth).*$")).build(); list.add(sc); return list; } List<SecurityReference> defaultAuth() { AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; authorizationScopes[0] = authorizationScope; List<SecurityReference> list = new ArrayList<SecurityReference>(); SecurityReference sr = new SecurityReference("Authorization", authorizationScopes); list.add(sr); return list; }