服务器之家

服务器之家 > 正文

java获取登录者IP和登录时间的两种实现代码详解

时间:2020-07-21 18:05     来源/作者:心寒丶

 第一种直接用java自带的InetAddress类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
 
 
public class test{
      public static void main(String[] args) throws Exception{
          InetAddress addr = InetAddress.getLocalHost();  
          String ip=addr.getHostAddress();//获得本机IP 
          String address=addr.getHostName();//获得本机名称 
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateNowStr = sdf.format(d);
          System.out.println("IP: "+ip+" Name: "+address+" Time: "+dateNowStr);   
 }
 
}

输出结果如下:

java获取登录者IP和登录时间的两种实现代码详解

第二种根据Web request请求获取:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package demo.login;
 
import ins.framework.web.Struts2Action;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
 
@SuppressWarnings("serial")
public class LoginAction extends Struts2Action {
 
    public String login() {
        HttpServletRequest request=this.getRequest();
        this.getIpAddress(request);
        return SUCCESS;
    }
 
    public void getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        System.out.println("x-forwarded-for ip: " + ip);
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值,第一个ip才是真实ip
            if( ip.indexOf(",")!=-1 ){
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
            System.out.println("Proxy-Client-IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
            System.out.println("WL-Proxy-Client-IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
            System.out.println("HTTP_CLIENT_IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            System.out.println("HTTP_X_FORWARDED_FOR ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
            System.out.println("X-Real-IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            System.out.println("getRemoteAddr ip: " + ip);
        }
    //获取本地ip
      if("0:0:0:0:0:0:0:1".equals(ip)){
              try {
                  ip = InetAddress.getLocalHost().getHostAddress();
              } catch (UnknownHostException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              System.out.println("getLocal ip: " + ip);
          }
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        System.out.println("登录IP: " + ip+"登录时间:"+dateNowStr);
    }  
}

本地会输出:

java获取登录者IP和登录时间的两种实现代码详解

服务器端如下:

java获取登录者IP和登录时间的两种实现代码详解

到此这篇关于java获取登录者IP和登录时间的两种实现代码详解的文章就介绍到这了,更多相关java获取登录者IP和登录时间内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/jungeCSND/article/details/107024203

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
返回顶部