| | |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import com.ruoyi.framework.config.properties.SwaggerProperties; |
| | | import io.swagger.models.auth.In; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.beans.BeansException; |
| | | import org.springframework.beans.factory.config.BeanPostProcessor; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.util.ReflectionUtils; |
| | | import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; |
| | | import springfox.documentation.builders.ApiInfoBuilder; |
| | | import springfox.documentation.builders.PathSelectors; |
| | | import springfox.documentation.builders.RequestHandlerSelectors; |
| | |
| | | import springfox.documentation.spi.DocumentationType; |
| | | import springfox.documentation.spi.service.contexts.SecurityContext; |
| | | import springfox.documentation.spring.web.plugins.Docket; |
| | | import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider; |
| | | import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.lang.reflect.Field; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @RequiredArgsConstructor |
| | | @Configuration |
| | | @EnableKnife4j |
| | | public class SwaggerConfig { |
| | | |
| | | @Autowired |
| | | private SwaggerProperties swaggerProperties; |
| | | private final SwaggerProperties swaggerProperties; |
| | | private final TokenProperties tokenProperties; |
| | | private final OpenApiExtensionResolver openApiExtensionResolver; |
| | | |
| | | @Autowired |
| | | private TokenProperties tokenProperties; |
| | | /** |
| | | * 用于适配springboot 2.6 |
| | | */ |
| | | @Bean |
| | | @SuppressWarnings("all") |
| | | public BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() { |
| | | return new BeanPostProcessor() { |
| | | @Override |
| | | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| | | if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) { |
| | | customizeSpringfoxHandlerMappings(getHandlerMappings(bean)); |
| | | } |
| | | return bean; |
| | | } |
| | | |
| | | @Autowired |
| | | private OpenApiExtensionResolver openApiExtensionResolver; |
| | | private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) { |
| | | mappings.removeIf(mapping -> mapping.getPatternParser() != null); |
| | | } |
| | | |
| | | private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) { |
| | | try { |
| | | Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings"); |
| | | field.setAccessible(true); |
| | | return (List<RequestMappingInfoHandlerMapping>) field.get(bean); |
| | | } catch (IllegalArgumentException | IllegalAccessException e) { |
| | | throw new IllegalStateException(e); |
| | | } |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * 创建API |
| | |
| | | * 默认的安全上引用 |
| | | */ |
| | | private List<SecurityReference> defaultAuth() { |
| | | AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); |
| | | AuthorizationScope authorizationScope = new AuthorizationScope("global" , "accessEverything"); |
| | | AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; |
| | | authorizationScopes[0] = authorizationScope; |
| | | List<SecurityReference> securityReferences = new ArrayList<>(); |