shz.auth.util.ServletHelp Maven / Gradle / Ivy
package shz.auth.util;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.ContentCachingRequestWrapper;
import shz.core.Coder;
import shz.core.IOHelp;
import shz.core.PRException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public final class ServletHelp {
private ServletHelp() {
throw new IllegalStateException();
}
public static HttpServletRequest getRequest() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes instanceof ServletRequestAttributes)
return ((ServletRequestAttributes) requestAttributes).getRequest();
return null;
}
public static HttpServletResponse getResponse() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes instanceof ServletRequestAttributes)
return ((ServletRequestAttributes) requestAttributes).getResponse();
return null;
}
public static String getIp(HttpServletRequest request) {
String ip;
// X-Forwarded-For:Squid 服务代理
if (isIp(ip = request.getHeader("X-Forwarded-For"))) return ip0(ip);
// Proxy-Client-IP:apache 服务代理
if (isIp(ip = request.getHeader("Proxy-Client-IP"))) return ip0(ip);
// WL-Proxy-Client-IP:weblogic 服务代理
if (isIp(ip = request.getHeader("WL-Proxy-Client-IP"))) return ip0(ip);
// HTTP_CLIENT_IP:有些代理服务器
if (isIp(ip = request.getHeader("HTTP_CLIENT_IP"))) return ip0(ip);
// X-Real-IP:nginx服务代理
if (isIp(ip = request.getHeader("X-Real-IP"))) return ip0(ip);
return ip0(request.getRemoteAddr());
}
private static boolean isIp(String ip) {
return ip != null && ip.length() > 0 && !"unknown".equalsIgnoreCase(ip);
}
private static String ip0(String ip) {
int idx = ip.indexOf(",");
if (idx != -1) ip = ip.substring(0, idx);
return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
}
public static UserAgent getUserAgent(HttpServletRequest request) {
return UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
}
public static boolean isAjax(HttpServletRequest request) {
String header = request.getHeader("Accept");
if (header != null && header.toLowerCase().contains("application/json")) return true;
header = request.getHeader("X-Requested-With");
//XMLHttpRequest
return header != null && header.toLowerCase().contains("xmlhttprequest");
}
public static String getBody(HttpServletRequest request) {
if (request == null) return null;
if (request instanceof ContentCachingRequestWrapper) {
ContentCachingRequestWrapper requestWrapper = (ContentCachingRequestWrapper) request;
try {
return new String(requestWrapper.getContentAsByteArray(), requestWrapper.getCharacterEncoding());
} catch (UnsupportedEncodingException e) {
throw PRException.of(e);
}
}
try {
String enc = request.getCharacterEncoding();
Charset cs = enc == null ? StandardCharsets.ISO_8859_1 : Charset.forName(enc);
StringWriter sw = new StringWriter();
IOHelp.read(IOHelp.getBr(request.getInputStream(), cs), sw);
return sw.toString();
} catch (IOException e) {
throw PRException.of(e);
}
}
public static String repeatedKey(HttpServletRequest request) {
if (request == null) return null;
String body = null;
if (request instanceof ContentCachingRequestWrapper) {
ContentCachingRequestWrapper requestWrapper = (ContentCachingRequestWrapper) request;
body = Coder.md5(requestWrapper.getContentAsByteArray());
} else try {
body = Coder.md5(request.getInputStream());
} catch (IOException ignored) {
}
return Coder.md5((request.getServletPath() + ":" + request.getMethod() + ":" + body).getBytes());
}
}