基础配置类

    1. import springfox.documentation.service.SecurityScheme;
    2. import org.springframework.context.annotation.Bean;
    3. import springfox.documentation.builders.ApiInfoBuilder;
    4. import springfox.documentation.builders.PathSelectors;
    5. import springfox.documentation.builders.RequestHandlerSelectors;
    6. import springfox.documentation.service.*;
    7. import springfox.documentation.spi.DocumentationType;
    8. import springfox.documentation.spi.service.contexts.SecurityContext;
    9. import springfox.documentation.spring.web.plugins.Docket;
    10. import java.util.ArrayList;
    11. import java.util.Collections;
    12. import java.util.List;
    13. /**
    14. * swagger配置
    15. *
    16. * @author mori
    17. * @date 2021-07-28 09:38:00
    18. */
    19. public class BaseSwaggerConfig {
    20. private final String basePackage;
    21. private final String title;
    22. private final String description;
    23. private final Contact contact;
    24. private final String version;
    25. public BaseSwaggerConfig(String basePackage, String title, String description, Contact contact, String version) {
    26. this.basePackage = basePackage;
    27. this.title = title;
    28. this.description = description;
    29. this.contact = contact;
    30. this.version = version;
    31. }
    32. @Bean
    33. public Docket createRestApi() {
    34. return new Docket(DocumentationType.OAS_30)
    35. .apiInfo(apiInfo()).select()
    36. .apis(RequestHandlerSelectors.basePackage(basePackage)).paths(PathSelectors.any())
    37. .build()
    38. .securitySchemes(securitySchemes())
    39. .securityContexts(securityContexts());
    40. }
    41. private ApiInfo apiInfo() {
    42. return new ApiInfoBuilder().title(title).description(description).contact(contact)
    43. .version(version).build();
    44. }
    45. private List<SecurityScheme> securitySchemes() {
    46. return Collections.singletonList(HttpAuthenticationScheme.JWT_BEARER_BUILDER
    47. .name("Authorization")
    48. .scheme("bearer")
    49. .build());
    50. }
    51. private List<SecurityContext> securityContexts() {
    52. List<SecurityContext> securityContexts = new ArrayList<>();
    53. securityContexts.add(SecurityContext.builder()
    54. .securityReferences(defaultAuth())
    55. .build());
    56. return securityContexts;
    57. }
    58. /**
    59. * bearer 授权上下文
    60. *
    61. * @return List<SecurityContext>
    62. */
    63. private List<SecurityContext> securityContext() {
    64. SecurityContext context = new SecurityContext(
    65. defaultAuth(),
    66. // 配置需要访问授权的请求,效果是对应页面上的请求有没有小锁图标
    67. PathSelectors.regex("/auth.*").negate(),
    68. // 配置需要访问授权的请求,效果是对应页面上的请求有没有小锁图标
    69. each -> true,
    70. // operationSelector优先级高于上面两个,配置需要访问授权的请求,效果是对应页面上的请求有没有小锁图标
    71. // 将auth开头的请求在swagger页面上放行,不使用jwt bearer token 授权方案
    72. operationContext -> !operationContext.requestMappingPattern().matches("/auth.*")
    73. );
    74. return Collections.singletonList(context);
    75. }
    76. /**
    77. * bearer 授权范围
    78. *
    79. * @return List<SecurityReference>
    80. */
    81. private List<SecurityReference> defaultAuth() {
    82. AuthorizationScope authorizationScope
    83. = new AuthorizationScope("global", "accessEverything");
    84. AuthorizationScope[] authorizationScopes = {authorizationScope};
    85. return Collections.singletonList(new SecurityReference("Authorization", authorizationScopes));
    86. }
    87. }

    配置使用

    1. @Configuration
    2. @EnableOpenApi
    3. private static class SwaggerConfig extends BaseSwaggerConfig {
    4. private final static String BASE_PACKAGE = "com.xxx.api.controller";
    5. private final static String TITLE = "API TITLE";
    6. private final static String DESCRIPTION = "API DESCRIPTION";
    7. private final static Contact CONTACT = new Contact("name", "url", "email");
    8. private final static String VERSION = "v0.1";
    9. public SwaggerConfig() {
    10. super(BASE_PACKAGE, TITLE, DESCRIPTION, CONTACT, VERSION);
    11. }
    12. }