Flask接口签名sign原理与实例代码浅析
517
2022-09-16
java如何获取用户登录ip、浏览器信息、SessionId
目录java获取用户登录ip、浏览器信息、SessionId1、获取用户登录ip2、获取用户所使用的浏览器信息3、获取用户SessionId获取用户登录的ip地址(考虑多种请求头的情况)
java获取用户登录ip、浏览器信息、SessionId
1、获取用户登录ip
//获取用户登录ip;
String loginIp = request.getHeader("X-Forwarded-For");
if (loginIp == null || loginIp.length() == 0 || "http://unknown".equalsIgnoreCase(loginIp)) {
loginIp = request.getHeader("X-Real-IP");
}
if (loginIp == null || loginIp.length() == 0 || "unknown".equalsIgnoreCase(loginIp)) {
loginIp = request.getHeader("Proxy-Client-IP");
}
if (loginIp == null || loginIp.length() == 0 || "unknown".equalsIgnoreCase(loginIp)) {
loginIp = request.getHeader("WL-Proxy-Client-IP");
}
if (loginIp == null || loginIp.length() == 0 || "unknown".equalsIgnoreCase(loginIp)) {
http://loginIp = request.getHeader("HTTP_CLIENT_IP");
}
if (loginIp == null || loginIp.length() == 0 || "unknowhttp://n".equalsIgnoreCase(loginIp)) {
loginIp = request.getRemoteAddr();
}
2、获取用户所使用的浏览器信息
/**
*获取浏览器信息;
*/
String ua = request.getHeader("User-Agent");
//转成UserAgent对象
UserAgent userAgent = UserAgent.parseUserAgentString(ua);
Browser browser = userAgent.getBrowser();
String browserInfo=browser.toString();//浏览器名
3、获取用户SessionId
request.getSession().getId();
获取用户登录的ip地址(考虑多种请求头的情况)
这段代码网上找的,传一个HttpServletRequest request就行,直接用
public class IpUtil {
/**
* 获取登录用户IP地址
*
* @param request
* @return
*/
public static String getIpAddr(HttpServletRequest 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();
}
if (ip.equals("0:0:0:0:0:0:0:1")) {
ip = "本地";
}
return ip;
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~