package cn.hsa.tps.utils;
import cn.hsa.tps.common.ResponseData;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;
public class CommonUtil {
/**
* 获得request对象
* 2021-03-15
* @return
*/
public static HttpServletRequest getRequest() {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
}
/**
* 获取ip地址方法
* @return
*/
public static String getIpAddr() {
HttpServletRequest request = getRequest();
String ip = null;
//X-Forwarded-For:Squid 服务代理
String ipAddresses = request.getHeader("X-Forwarded-For");
System.out.println("====ipAddresses:" + ipAddresses);
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
//打印所有头信息
String s = headerNames.nextElement();
String header = request.getHeader(s);
System.out.println(s + "::::" + header);
}
// System.out.println("headerNames:"+JSON.toJSONString(headerNames));
System.out.println("RemoteHost:" + request.getRemoteHost());
System.out.println("RemoteAddr:" + request.getRemoteAddr());
String unknown = "unknown";
if (ipAddresses == null || ipAddresses.length() == 0 || unknown.equalsIgnoreCase(ipAddresses)) {
//Proxy-Client-IP:apache 服务代理
ipAddresses = request.getHeader("Proxy-Client-IP");
}
if (ipAddresses == null || ipAddresses.length() == 0 || unknown.equalsIgnoreCase(ipAddresses)) {
//WL-Proxy-Client-IP:weblogic 服务代理
ipAddresses = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddresses == null || ipAddresses.length() == 0 || unknown.equalsIgnoreCase(ipAddresses)) {
//HTTP_CLIENT_IP:有些代理服务器
ipAddresses = request.getHeader("HTTP_CLIENT_IP");
}
if (ipAddresses == null || ipAddresses.length() == 0 || unknown.equalsIgnoreCase(ipAddresses)) {
//X-Real-IP:nginx服务代理
ipAddresses = request.getHeader("X-Real-IP");
}
//有些网络通过多层代理,那么获取到的ip就会有多个,一般都是通过逗号(,)分割开来,并且第一个ip为客户端的真实IP
if (ipAddresses != null && ipAddresses.length() != 0) {
ip = ipAddresses.split(",")[0];
}
//还是不能获取到,最后再通过request.getRemoteAddr();获取
if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ipAddresses)) {
ip = request.getRemoteAddr();
}
// 服务代理
// return ip;
//本地
return request.getRemoteAddr();
}
/*
* 取map中的值
*/
public static String mv(Map a, String key) {
if (a.get(key) != null) {
return a.get(key).toString();
}
return "";
}
public static String getRedisValue(ResponseData responseData){
return responseData.getData() == null ? null : responseData.getData().toString();
}
public static String getUserValue(ResponseData responseData,String key){
Map<String,Object> map = (Map<String,Object>)responseData.getData();
return map == null ? null : map.get(key) == null ? null : map.get(key).toString();
}
/**
* 验证是否为手机号
* @创建时间:2019-08-29
* @param str 字符串
* @return
*/
public static boolean isMobile(final String str) {
Pattern p = null;
Matcher m = null;
boolean b = false;
p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号
m = p.matcher(str);
b = m.matches();
return b;
}
/**
* 验证是否为电话,非手机号那种电话
* @创建时间:2019-08-29
* @param str 字符串
* @return
*/
public static boolean isPhone(final String str) {
Pattern p1 = null, p2 = null;
Matcher m = null;
boolean b = false;
p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$"); // 验证带区号的
p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 验证没有区号的
if (str.length() > 9) {
m = p1.matcher(str);
b = m.matches();
} else {
m = p2.matcher(str);
b = m.matches();
}
return b;
}
}
java获取ip和正则验证手机号
于 2021-11-17 15:56:23 首次发布