JAVA获取当前域名
eg:前端node代理,域名:
http://localhost:3000
后端接口域名
http://192.168.0.123:8080
获取方法
request.getScheme();//-->http
request.getServerName();//-->localhost
request.getServerPort();//-->3000
request.getLocalAddr();//-->192.168.0.123
request.getLocalPort();//-->8080
根据需求拼起来即可
获取请求IP,因为存在网关或者nginx代理 后端根据上面的代码无论谁请求获取到的都是代理服务器的IP 下面代码获取请求者IP非代理IP
/**
* 最终获取请求者IP
*/
def getCurId(request){
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
还没有评论,来说两句吧...