mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-15 06:29:01 +08:00
♻️ 增加 NoToken 注解方便对应 无token feign调用场景
This commit is contained in:
1 parent
a7552c6a5c
commit
13f13d4813
27 files changed
+440
-335
No files matched your search
+32
-6
@@ -18,6 +18,8 @@
|
||||
package com.pig4cloud.pigx.common.feign;
|
||||
|
||||
import com.pig4cloud.pigx.common.feign.endpoint.FeignClientEndpoint;
|
||||
import com.pig4cloud.pigx.common.feign.interceptor.PigxFeignInnerRequestInterceptor;
|
||||
import com.pig4cloud.pigx.common.feign.interceptor.PigxFeignRequestCloseInterceptor;
|
||||
import feign.Feign;
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
@@ -42,11 +44,35 @@ import org.springframework.context.annotation.Import;
|
||||
@AutoConfigureAfter(EnableFeignClients.class)
|
||||
public class PigxFeignAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnAvailableEndpoint
|
||||
public FeignClientEndpoint feignClientEndpoint(ApplicationContext context) {
|
||||
return new FeignClientEndpoint(context);
|
||||
}
|
||||
/**
|
||||
* feign actuator endpoint
|
||||
* @param context
|
||||
* @return FeignClientEndpoint
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnAvailableEndpoint
|
||||
public FeignClientEndpoint feignClientEndpoint(ApplicationContext context) {
|
||||
return new FeignClientEndpoint(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* add http connection close header
|
||||
*
|
||||
* @return PigxFeignRequestCloseInterceptor
|
||||
*/
|
||||
@Bean
|
||||
public PigxFeignRequestCloseInterceptor pigFeignRequestCloseInterceptor() {
|
||||
return new PigxFeignRequestCloseInterceptor();
|
||||
}
|
||||
|
||||
/**
|
||||
* add inner request header
|
||||
*
|
||||
* @return PigxFeignInnerRequestInterceptor
|
||||
*/
|
||||
@Bean
|
||||
public PigxFeignInnerRequestInterceptor pigFeignInnerRequestInterceptor() {
|
||||
return new PigxFeignInnerRequestInterceptor();
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.pig4cloud.pigx.common.feign.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 服务无token调用声明注解
|
||||
* <p>
|
||||
* 只有发起方没有 token 时候才需要添加此注解, @NoToken + @Inner
|
||||
* <p>
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface NoToken {
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.pig4cloud.pigx.common.feign.interceptor;
|
||||
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.feign.annotation.NoToken;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2024/6/1
|
||||
*/
|
||||
public class PigxFeignInnerRequestInterceptor implements RequestInterceptor, Ordered {
|
||||
|
||||
/**
|
||||
* Called for every request. Add data using methods on the supplied
|
||||
* {@link RequestTemplate}.
|
||||
* @param template
|
||||
*/
|
||||
@Override
|
||||
public void apply(RequestTemplate template) {
|
||||
Method method = template.methodMetadata().method();
|
||||
NoToken noToken = method.getAnnotation(NoToken.class);
|
||||
if (noToken != null) {
|
||||
template.header(SecurityConstants.FROM, SecurityConstants.FROM_IN);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.pig4cloud.pigx.common.feign.interceptor;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2024/3/15
|
||||
* <p>
|
||||
* http connection close
|
||||
*/
|
||||
public class PigxFeignRequestCloseInterceptor implements RequestInterceptor {
|
||||
|
||||
/**
|
||||
* set connection close
|
||||
* @param template
|
||||
*/
|
||||
@Override
|
||||
public void apply(feign.RequestTemplate template) {
|
||||
template.header(HttpHeaders.CONNECTION, "close");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user