openfeign
最后发布时间:2024-08-10 22:37:28
浏览量:
@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import({EnableUserInfoTransmitterAutoConfiguration.class})
public @interface EnableUserInfoTransmitter {
}
public class EnableUserInfoTransmitterAutoConfiguration {
public EnableUserInfoTransmitterAutoConfiguration() {
}
@Bean
public TransmitUserInfoFeighClientIntercepter transmitUserInfo2FeighHttpHeader() {
return new TransmitUserInfoFeighClientIntercepter();
}
@Bean
public TransmitUserInfoFilter transmitUserInfoFromHttpHeader() {
return new TransmitUserInfoFilter();
}
}
从浏览器访问/page先进入
public class TransmitUserInfoFilter implements Filter {
然后进入controller servie
public PageModel<XXXX> page(Integer number, Integer size, String sortField, List<QueryCriteriaVo> criteriaVos) {
在service中进入feign
List<SysTenantDto> data = sysTenantFeignService.listByIds(tenantIds).getData();
@Configuration
public class FeignConfiguration implements RequestInterceptor {
public FeignConfiguration() {
}
public void apply(RequestTemplate template) {
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) {
while(headerNames.hasMoreElements()) {
String name = (String)headerNames.nextElement();
String values = request.getHeader(name);
if (!name.equalsIgnoreCase("content-length")) {
template.header(name, new String[]{values});
}
}
}
}
}
}
public class TransmitUserInfoFeighClientIntercepter implements RequestInterceptor {
private static final Logger log = LoggerFactory.getLogger(TransmitUserInfoFeighClientIntercepter.class);
public TransmitUserInfoFeighClientIntercepter() {
}
public void apply(RequestTemplate requestTemplate) {
SysUserDto user = SysUserInfoContext.getUser();
if (user != null) {
requestTemplate.header("Authorization", new String[]{"Bearer " + user.getToken()});
String userJson = JSON.toJSONString(user);
try {
userJson = URLEncoder.encode(userJson, "UTF-8");
} catch (Exception var5) {
}
requestTemplate.header("user_info", new String[]{userJson});
}
}
}