-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
ruoyi-admin/src/main/java/com/ruoyi/web/core/config/SpringfoxFixConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.ruoyi.web.core.config; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
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.spring.web.plugins.WebFluxRequestHandlerProvider; | ||
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider; | ||
|
||
/** | ||
* Springfox 在springboot 2.6+的处理. | ||
* | ||
* 还要设置spring.mvc.pathmatch.matching-strategy=ant_path_matcher才行. | ||
* | ||
* ref: https://blog.csdn.net/kkorkk/article/details/123774484 | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
public class SpringfoxFixConfig { | ||
|
||
@Bean | ||
//-Druoyi.fix.springfox=false for spring < 2.6 | ||
@ConditionalOnProperty(prefix = "ruoyi.fix", name = "springfox", havingValue = "true", matchIfMissing = true) | ||
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; | ||
} | ||
|
||
private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) { | ||
List<T> copy = mappings.stream().filter(mapping -> mapping.getPatternParser() == null) | ||
.collect(Collectors.toList()); | ||
mappings.clear(); | ||
mappings.addAll(copy); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
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); | ||
} | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,3 +140,5 @@ xss: | |
swagger: | ||
# 是否开启swagger | ||
enabled: true | ||
|
||
spring.mvc.pathmatch.matching-strategy: ant_path_matcher |