2024-03-05 10:26:05 +08:00

97 lines
3.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* -----------------------------------
* 林风社交论坛开源版本请务必保留此注释头信息
* 开源地址: https://gitee.com/virus010101/linfeng-community
* 商业版详情查看: https://www.linfeng.tech
* 商业版购买联系技术客服QQ: 3582996245
* 可正常分享和学习源码,不得转卖或非法牟利!
* Copyright (c) 2021-2023 linfeng all rights reserved.
* 版权所有 ,侵权必究!
* -----------------------------------
*/
package io.linfeng.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
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;
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Value("${linfeng.swagger.enable}")
private Boolean enable;
@Value("${linfeng.swagger.version}")
private String version;
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(enable)
.select()
.apis(RequestHandlerSelectors.basePackage("io.linfeng"))
.paths(PathSelectors.any())
.build()
.securityContexts(securityContexts())
.securitySchemes(securitySchemes());
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("林风社交论坛开源版接口文档")
.description("林风社交论坛项目开源版接口文档 官网https://net.linfeng.tech")
.contact(new Contact("linfeng","http://localhost:8080/doc.html","linfengtech001@163.com"))
.version(version)
.build();
}
private List<ApiKey> securitySchemes(){
//设置请求头信息
List<ApiKey> result = new ArrayList<>();
ApiKey apiKey = new ApiKey("token","token","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("token",authorizationScopes));
return result;
}
}