前言
大三下啦,我也已经开始实习啦,因此现在会记录一些我在实习过程中使用到的,或者自己认为有用的自定义的工具包。当然这些工具包自然是根据一些已经开源的项目编写的,当然,也有可能记录的是我认为很好用的工具包,所以,这些文章类似于我的实习笔记,大家看个乐即可,菜菜,勿喷。
RequestContextHolder
先说一下,我们知道在我们的controller层中,是可以获取到HttpRequest对象和HttpResponse对象的,如果我们需要在Service层中用到这个对象,我们可以通过参数去传递这个对象,但是吧,比较暴力。
所以这里我找到了RequestContextHolder这个对象。他的意思就是Request对象的上下文,因此我们可以通过这个对象获取到我们每一个请求的request和response对象。
工具包代码如下,下面还有使用方法
package com.logistics.util; import lombok.extern.slf4j.Slf4j; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Slf4j public class MyHttpUtils { private static final ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); /** * 获取 HttpServletRequest * * @return */ public static HttpServletRequest getRequest() { return requestAttributes.getRequest(); } /** * 获取 HttpServletResponse * * @return */ public static HttpServletResponse getResponse() { return requestAttributes.getResponse(); } /** * 获取header * * @param headerName * @return */ public static String getHeader(String headerName) { return getRequest().getHeader(headerName); } /** * 获取cookie对象 * * @param cookieName * @return */ public static Cookie getCookie(String cookieName) { HttpServletRequest request = getRequest(); Cookie[] cookies = request.getCookies(); for (Cookie cookie : cookies) { if (StringUtils.equals(cookieName, cookie.getName())) { return cookie; } } return null; } /** * 获取cookie的值 * * @param cookieName * @return */ public static String getCookieValue(String cookieName) { return getCookie(cookieName).getValue(); } /** * 设置cookie * * @param cookieName * @param cookieValue */ public static void setCookie(String cookieName, String cookieValue) { Cookie cookie = new Cookie(cookieName, cookieValue); getResponse().addCookie(cookie); } /** * 获取访问者IP * * @return */ public static String getIP() { HttpServletRequest request = getRequest(); String Xip = request.getHeader("X-Real-IP"); String XFor = request.getHeader("X-Forwarded-For"); if (StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)) { //多次反向代理后会有多个ip值,第一个ip才是真实ip int index = XFor.indexOf(","); if (index != -1) { return XFor.substring(0, index); } else { return XFor; } } XFor = Xip; if (StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)) { return XFor; } if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) { XFor = request.getHeader("Proxy-Client-IP"); } if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) { XFor = request.getHeader("WL-Proxy-Client-IP"); } if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) { XFor = request.getHeader("HTTP_CLIENT_IP"); } if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) { XFor = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) { XFor = request.getRemoteAddr(); } return XFor; } }