forked from TencentBlueKing/bk-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fs客户端管理支持ip、版本的模糊查询 TencentBlueKing#2435
* feat: fs客户端管理支持ip、版本的模糊查询 TencentBlueKing#2435 * feat: fs客户端管理支持ip、版本的模糊查询 TencentBlueKing#2435 * feat: fs客户端管理支持ip、版本的模糊查询 TencentBlueKing#2435
- Loading branch information
Showing
4 changed files
with
101 additions
and
7 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
36 changes: 36 additions & 0 deletions
36
...ver/src/main/kotlin/com/tencent/bkrepo/fs/server/config/feign/FeignGlobalConfiguration.kt
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,36 @@ | ||
package com.tencent.bkrepo.fs.server.config.feign | ||
|
||
import feign.Contract | ||
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor | ||
import org.springframework.cloud.openfeign.FeignClientProperties | ||
import org.springframework.cloud.openfeign.FeignFormatterRegistrar | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.core.convert.ConversionService | ||
import org.springframework.format.support.DefaultFormattingConversionService | ||
import org.springframework.format.support.FormattingConversionService | ||
|
||
/** | ||
* 同步feign的全局配置 | ||
* */ | ||
@Configuration | ||
class FeignGlobalConfiguration { | ||
@Bean | ||
fun feignContract( | ||
parameterProcessors: List<AnnotatedParameterProcessor>, | ||
feignClientProperties: FeignClientProperties?, | ||
feignConversionService: ConversionService, | ||
): Contract { | ||
val decodeSlash = feignClientProperties?.isDecodeSlash ?: true | ||
return OldSpringMvcContract(parameterProcessors, feignConversionService, decodeSlash) | ||
} | ||
|
||
@Bean | ||
fun feignConversionService(feignFormatterRegistrars: List<FeignFormatterRegistrar>): FormattingConversionService { | ||
val conversionService: FormattingConversionService = DefaultFormattingConversionService() | ||
for (feignFormatterRegistrar in feignFormatterRegistrars) { | ||
feignFormatterRegistrar.registerFormatters(conversionService) | ||
} | ||
return conversionService | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...-server/src/main/kotlin/com/tencent/bkrepo/fs/server/config/feign/OldSpringMvcContract.kt
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,54 @@ | ||
package com.tencent.bkrepo.fs.server.config.feign | ||
|
||
import feign.MethodMetadata | ||
import feign.Util.emptyToNull | ||
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor | ||
import org.springframework.cloud.openfeign.support.SpringMvcContract | ||
import org.springframework.context.ConfigurableApplicationContext | ||
import org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation | ||
import org.springframework.core.convert.ConversionService | ||
import org.springframework.core.io.DefaultResourceLoader | ||
import org.springframework.util.StringUtils | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
|
||
/** | ||
* 新版本的Spring Cloud的@FeignClient出于安全考虑(不是十分理解),不允许使用@RequestMapping, | ||
* 但这对现有的使用方式有很大的影响,需要修改所有的FeignClient,所以这里我们恢复之前的逻辑,允许在 | ||
* FeignClient上使用RequestMapping定义公共的路由前缀。 | ||
* */ | ||
class OldSpringMvcContract( | ||
annotatedParameterProcessors: List<AnnotatedParameterProcessor>, | ||
conversionService: ConversionService, | ||
private val decodeSlash: Boolean, | ||
) : SpringMvcContract(annotatedParameterProcessors, conversionService, decodeSlash) { | ||
private val resourceLoader = DefaultResourceLoader() | ||
|
||
/** | ||
* 旧版本的SpringMvcContract处理逻辑 | ||
* */ | ||
override fun processAnnotationOnClass(data: MethodMetadata, clz: Class<*>) { | ||
if (clz.interfaces.isNotEmpty()) { | ||
return | ||
} | ||
val classAnnotation = findMergedAnnotation(clz, RequestMapping::class.java) ?: return | ||
// Prepend path from class annotation if specified | ||
if (classAnnotation.value.isNotEmpty()) { | ||
var pathValue = emptyToNull(classAnnotation.value[0]) | ||
pathValue = resolve(pathValue) | ||
if (!pathValue.startsWith("/")) { | ||
pathValue = "/$pathValue" | ||
} | ||
data.template().uri(pathValue) | ||
if (data.template().decodeSlash() != decodeSlash) { | ||
data.template().decodeSlash(decodeSlash) | ||
} | ||
} | ||
} | ||
private fun resolve(value: String): String { | ||
return if (StringUtils.hasText(value) && resourceLoader is ConfigurableApplicationContext) { | ||
(resourceLoader as ConfigurableApplicationContext).environment.resolvePlaceholders(value) | ||
} else { | ||
value | ||
} | ||
} | ||
} |
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