1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI; import io.swagger.annotations.Api; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.ApiKey; import springfox.documentation.service.AuthorizationScope; 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.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.List; /** * Swagger 接口文档配置类 * * @author * @date 2023/6/17 */ @EnableSwaggerBootstrapUI @EnableSwagger2 @Configuration public class SwaggerConfig extends WebMvcConfigurationSupport { /** * 配置扫描的api控制包路径 * 这里配置的是你后端框架controller类对应的路径 */ private static final String BASE_PACKAGE = "xxxxxxxxxxxxxxx.controller"; /** * 服务器路径 * 代码部署服务的路径,选添,可以不写 */ private static final String SERVICE_URL = "xxxxxxxxx"; /** * 配置swagger文档 * * @return docket 对象 */ @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() // 设置扫描基础包 .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE)) // 扫描使用Api注解的控制器 .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .paths(PathSelectors.any()) .build() //添加登录认证 //这里下面两行代码已经相关下面包可以注释掉,这里是配置的Swagger文档登录验证,需要在application.yml文件中进行相关的配置,配置信息见后面详细情况 .securitySchemes(securitySchemes()) .securityContexts(securityContexts()); } @Bean public RequestMappingInfoHandlerMapping requestMapping() { return new RequestMappingHandlerMapping(); } /** * 配置 api 文档信息 * * @return 文档信息 */ private ApiInfo apiInfo() { return new ApiInfoBuilder() // 页面标题(自由发挥) .title("xxxxx") // API描述 .description("自由发挥") // 创建路径 .termsOfServiceUrl(SERVICE_URL) .version("1.0.0") .build(); } /** * 解决 swagger 静态资源 404问题 * * @param registry 资源路径 */ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { // 解决静态资源无法访问 registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/"); // 解决swagger无法访问 registry.addResourceHandler("/swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); // 解决swagger的js文件无法访问 registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); // 解决swagger-bootstrap 无法访问 registry.addResourceHandler("doc.html"). addResourceLocations("classpath:/META-INF/resources/"); } private List<ApiKey> securitySchemes() { //设置请求头信息 List<ApiKey> result = new ArrayList<>(); ApiKey apiKey = new ApiKey("Authorization", "Authorization", "header"); result.add(apiKey); return result; } private List<SecurityContext> securityContexts() { //设置需要登录认证的路径 List<SecurityContext> result = new ArrayList<>(); // 所有请求路径 result.add(getContextByPath("/.*")); return result; } private SecurityContext getContextByPath(String pathRegex) { return SecurityContext.builder() .securityReferences(defaultAuth()) .forPaths(PathSelectors.regex(pathRegex)) .build(); } private List<SecurityReference> defaultAuth() { List<SecurityReference> result = new ArrayList<>(); AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; authorizationScopes[0] = authorizationScope; result.add(new SecurityReference("Authorization", authorizationScopes)); return result; } } |
1 2 3 4 5 6 7 8 9 10 11 |
## 开启 Swagger的 Basic认证功能,默认是false swagger: # 是否关闭 swagger接口文档访问 # production: true basic: # 开启简单用户验证 enable: true # 用户名(自由发挥) username: xx # 用户密码(自由发挥) password: xx |
from:https://blog.csdn.net/weixin_47930865/article/details/131257239