Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

java - How to access client's MAC address in Restful web services

I have a angular application, which is making a rest call as:

http://localhost:8765/test/iptest

And in my controller :

@GetMapping(value = "test/iptest")
public ResponseEntity<Object> doTest() {
    log.debug("Inside Test");
    return new ResponseEntity<>("Test success", HttpStatus.OK);
}

How do I access client's IP and MAC address using this request?

EDIT: I was able to access Client's IP Address using HttpServletRequest as:

    import javax.servlet.http.HttpServletRequest;
    import javax.ws.rs.core.Context;

    @GetMapping(value = "test/iptest")
    public ResponseEntity<Object> doTest(@Context HttpServletRequest request) {
        log.debug("IP Address is:"+request.getRemoteAddr());
        return new ResponseEntity<>("Test success", HttpStatus.OK);
    }

But I'm not able to access MAC ADDRESS


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can't - other than on the local LAN, remote MAC addresses are not exposed to your end point. The MAC address in the Ethernet header of the packet received at the server will be that of the nearest router that was the last hop in the path.

Even on the local LAN you cannot get this information for an IP socket - looking up a local MAC address requires poking around in the OS's ARP tables.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...