springBoot 获取请求头header里面的信息方式
项目中后端服务经常需要前端传的请求头里的信息,今天给大家分享一下:1、循环获取:/*** 获取判断请求头的参数* @return*/public static boolean checkHeaderName(){RequestAttributes ra = RequestContextHolder.getRequestAttributes();if(Objects.isNull(ra)){lo..
项目中后端服务经常需要前端传的请求头里的信息,今天给大家分享一下:
1、循环获取:
/**
* 获取判断请求头的参数
* @return
*/
public static boolean checkHeaderName(){
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
if(Objects.isNull(ra)){
log.info("服务里RequestAttributes对象为空");
return false;
}
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String name = enumeration.nextElement();
String value = request.getHeader(name);
if(name.equals("accept-"+HeaderEnum.language.name()) && value.equals(HeaderEnum.language.head())){
return true;
}
/**
* 或者其他的业务处理
*/
}
return false;
}
2、根据具体的key 获取:
/**
*获取 判断请求头的参数
* @return
*/
public static boolean checkHeader(){
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
if(Objects.isNull(ra)){
log.info("服务里RequestAttributes对象为空");
return false;
}
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
String head = request.getHeader("accept-"+HeaderEnum.language.name());
if(head.equals(HeaderEnum.language.head())){
return true;
}
/**
* 或者其他的业务处理
*/
return false;
}
3、修改请求头信息:只能依靠反射原原理(HttpServletRequest中定义的对Header的操作全是只读,没有修改)。假设的场景是,需要对请求Header统一添加一个x-request-id,通过这个ID来从日志中定位每一个请求。import java.io.IOException;
import java.lang.reflect.Field;
import java.util.UUID;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.RequestFacade;
import org.apache.tomcat.util.http.MimeHeaders;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
@WebFilter(urlPatterns = "/*")
@Component
@Order(-999)
public class RequestIdGenFilter extends HttpFilter {
/**
*
*/
private static final long serialVersionUID = 1787347739651657706L;
@Override
protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
try {
// 从 RequestFacade 中获取 org.apache.catalina.connector.Request
Field connectorField = ReflectionUtils.findField(RequestFacade.class, "request", Request.class);
connectorField.setAccessible(true);
Request connectorRequest = (Request) connectorField.get(req);
// 从 org.apache.catalina.connector.Request 中获取 org.apache.coyote.Request
Field coyoteField = ReflectionUtils.findField(Request.class, "coyoteRequest", org.apache.coyote.Request.class);
coyoteField.setAccessible(true);
org.apache.coyote.Request coyoteRequest = (org.apache.coyote.Request) coyoteField.get(connectorRequest);
// 从 org.apache.coyote.Request 中获取 MimeHeaders
Field mimeHeadersField = ReflectionUtils.findField(org.apache.coyote.Request.class, "headers", MimeHeaders.class);
mimeHeadersField.setAccessible(true);
MimeHeaders mimeHeaders = (MimeHeaders) mimeHeadersField.get(coyoteRequest);
this.mineHeadersHandle(mimeHeaders);
} catch (Exception e) {
throw new RuntimeException(e);
}
super.doFilter(req, res, chain);
}
protected void mineHeadersHandle (MimeHeaders mimeHeaders) {
// 添加一个Header,随机生成请求ID
mimeHeaders.addValue("x-request-id").setString(UUID.randomUUID().toString());;
// 移除一个header
mimeHeaders.removeHeader("User-Agent");
}
}
更多推荐
所有评论(0)