关闭

springboot配置swagger

瞎溜达 1年前 ⋅ 265 阅读
  1. 引入相关库
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.9.2</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.9.2</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-annotations</artifactId>
                <version>1.5.21</version>
            </dependency>
            <dependency>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-models</artifactId>
                <version>1.5.21</version>
            </dependency>​
  2. swagger配置类
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        @Bean(name = "swaggerApi")
        public Docket swaggerApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .groupName("组名称")
                    .apiInfo(apiInfo())
                    .enable(true)//控制swagger开关
                    .select()
    .apis(RequestHandlerSelectors.basePackage("项目Controller路径"))
                    .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                    .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                    .paths(PathSelectors.any())
                    .build()
                    .securitySchemes(securitySchemes())
                    .securityContexts(securityContexts())
                    .globalOperationParameters(pars);
        }
    
        private List<ApiKey> securitySchemes() {
            List<ApiKey> apiKeyList = new ArrayList();
            apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
            return apiKeyList;
        }
    
        private List<SecurityContext> securityContexts() {
            List<SecurityContext> securityContexts = new ArrayList<>();
            securityContexts.add(SecurityContext.builder()
                    .securityReferences(defaultAuth())
                    .forPaths(PathSelectors.regex("^(?!auth).*$")).build());
            return securityContexts;
        }
    
    
        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;
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("manager api")
                    .description("接口文档说明")
                    .contact(new Contact("name", "url", "email"))
                    .version("1.0")
                    .build();
        }
    }
    ​
  3. 配置WebMvcConfigurer
    @Configuration
    public class MvcConfig implements WebMvcConfigurer {
    
    
        @Autowired
        private TokenInterceptor tokenInterceptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
         registry.addInterceptor(tokenInterceptor).excludePathPatterns("/user/login", "/swagger-ui.html", "/swagger-resources/**"); //允许访问swagger页面相关资源
        }
    }​
  4. 配置接口类:
    @Api(tags = {"user"}) //swagger类注解
    @RestController
    @RequestMapping("/user")
    public class UserController {
    
        @ApiOperation(value = "登录接口") //swagger注解
        @PostMapping("/login")
        public ResponseResult login(String username,String password) {
            
    		return null;
        }
    }​

全部评论: 0

    我有话说: