SpringBoot添加swagger2接口文档并添加全局Authorization参数
程序员文章站
2022-07-02 18:16:05
...
依赖包
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
配置类(securitySchemes与securityContexts作用为配置全局Authorization参数)
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//http://localhost:9007/swagger-ui.html
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.com.dinglisec.iot.uaa.api"))
.paths(PathSelectors.any())
.build()
.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger构建api文档")
.description("简单优雅的restful风格")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
private List<ApiKey> securitySchemes() {
List<ApiKey> apiKeys = new ArrayList<>();
apiKeys.add(new ApiKey("Authorization", "Authorization", "header"));
return apiKeys;
}
private List<SecurityContext> securityContexts() {
List<SecurityContext> securityContexts = new ArrayList<>();
securityContexts.add(SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.regex("^(?!auth).*$")).build());
return securityContexts;
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
List<SecurityReference> securityReferences = new ArrayList<>();
securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
return securityReferences;
}
}
接口正常参数配置
@ApiOperation(value="登录", notes="根据用户账号密码进行登录")
@ApiImplicitParams({@ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query")})
@RequestMapping(value = "login", method = GET)
public Message login(String username, String password) {
//......
}
接口实体类参数配置
@ApiOperation(value="通过角色与公司获取用户集", notes="通过角色与公司获取用户集")
@GetMapping(value = "userPageSet", produces = APPLICATION_JSON_UTF8_VALUE)
@PreAuthorize("hasPermission('user', 'select')")
public Page<User> getUserByRoleName(@ApiParam(name="角色信息对象",value="传入json格式",required=true) GetRoleUserListRequest req, @ApiParam(name="分页对象",value="传入json格式",required=true) @PageableDefault(page=0,size=10) Pageable pageable) {
// ......
}
或
@ApiOperation(value="修改当前用户密码", notes="修改当前用户密码")
@ApiImplicitParam(name = "req", value = "用户信息实体类", required = true, dataType = "ChangePasswordRequest")
@PutMapping(value = "user/password")
@PreAuthorize("hasPermission('user', 'update')")
public Message updatePassword(@RequestBody ChangePasswordRequest req) {
// ......
}
接口文档链接http://localhost:9007/swagger-ui.html
配置全局token
上一篇: ApplicationContextAware接口
下一篇: c#贪吃蛇小游戏Wpf