org.macrocloud.kernel.context.ServletHttpHeadersGetter Maven / Gradle / Ivy
package org.macrocloud.kernel.context;
import lombok.RequiredArgsConstructor;
import org.macrocloud.kernel.context.props.BaseContextProperties;
import org.macrocloud.kernel.toolkit.utils.StringUtil;
import org.macrocloud.kernel.toolkit.utils.WebUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.http.HttpHeaders;
import org.springframework.lang.Nullable;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;
/**
* HttpHeaders 获取器
*
* @author macro
*/
@RequiredArgsConstructor
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class ServletHttpHeadersGetter implements HttpHeadersGetter {
private final BaseContextProperties properties;
@Nullable
@Override
public HttpHeaders get() {
HttpServletRequest request = WebUtil.getRequest();
if (request == null) {
return null;
}
return get(request);
}
@Nullable
@Override
public HttpHeaders get(HttpServletRequest request) {
HttpHeaders headers = new HttpHeaders();
List crossHeaders = properties.getCrossHeaders();
// 传递请求头
Enumeration headerNames = request.getHeaderNames();
if (headerNames != null) {
List allowed = properties.getHeaders().getAllowed();
while (headerNames.hasMoreElements()) {
String key = headerNames.nextElement();
// 只支持配置的 header
if (crossHeaders.contains(key) || allowed.contains(key)) {
String values = request.getHeader(key);
// header value 不为空的 传递
if (StringUtil.isNotBlank(values)) {
headers.add(key, values);
}
}
}
}
return headers;
}
}