Getting user IP on log in












0















I currently have a web project set up, with form-based JDBC realm authentication set up on the Apache Tomcat 8.0 server.



I want to store the IP address of the user that logs in into a database, using the HttpServletRequest class.



Where (i.e. after what action) would I add this functionality in my web application?



I am using JSP/Servlet, Tomcat, and MySQL.



Thanks!










share|improve this question





























    0















    I currently have a web project set up, with form-based JDBC realm authentication set up on the Apache Tomcat 8.0 server.



    I want to store the IP address of the user that logs in into a database, using the HttpServletRequest class.



    Where (i.e. after what action) would I add this functionality in my web application?



    I am using JSP/Servlet, Tomcat, and MySQL.



    Thanks!










    share|improve this question



























      0












      0








      0








      I currently have a web project set up, with form-based JDBC realm authentication set up on the Apache Tomcat 8.0 server.



      I want to store the IP address of the user that logs in into a database, using the HttpServletRequest class.



      Where (i.e. after what action) would I add this functionality in my web application?



      I am using JSP/Servlet, Tomcat, and MySQL.



      Thanks!










      share|improve this question
















      I currently have a web project set up, with form-based JDBC realm authentication set up on the Apache Tomcat 8.0 server.



      I want to store the IP address of the user that logs in into a database, using the HttpServletRequest class.



      Where (i.e. after what action) would I add this functionality in my web application?



      I am using JSP/Servlet, Tomcat, and MySQL.



      Thanks!







      jsp tomcat authentication servlets jdbc






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 21:30







      NotAMonkey24

















      asked Nov 29 '17 at 18:25









      NotAMonkey24NotAMonkey24

      4110




      4110
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You can use a servlet filter for that. The servlet filter can be used to intercept the incoming HTTP servlet request and do whatever you like to the request or log an entry.



          Note that Tomcat also has an access log by default that contains all HTTP requests, their status, IP, and username if any. Here is an example.



          192.168.0.47 - - [27/Nov/2017:07:18:07 -0600] "GET /example/module?wsdl HTTP/1.1" 200 3122
          192.168.0.47 - - [27/Nov/2017:07:18:07 -0600] "POST /otherapp/service HTTP/1.1" 200 251
          fe80:0:0:0:fc36:e3bb:5bc4:abce%14 - asm-admin [27/Nov/2017:07:18:08 -0600] "POST /otherapp/HEARTBEAT/?v-uiId=0 HTTP/1.1" 200 -
          fe80:0:0:0:fc36:e3bb:5bc4:abce%14 - asm-admin [27/Nov/2017:07:18:18 -0600] "POST /otherapp/HEARTBEAT/?v-uiId=0 HTTP/1.1" 200 -
          fe80:0:0:0:fc36:e3bb:5bc4:abce%14 - asm-admin [27/Nov/2017:07:18:28 -0600] "POST /otherapp/HEARTBEAT/?v-uiId=0 HTTP/1.1" 200 -





          share|improve this answer
























          • Where would I find this log? Also how would I be able to tell which request is the login request, and how would I use that to post to my database?

            – NotAMonkey24
            Nov 30 '17 at 23:22











          • You would know it is a login request because it went to the login page. You would know it is a failed login request because the code would be HTTP 401. You would know authN was successful but the user does not have the right permissions because the code would be 403. This log file is inside TOMCAT_HOME/logs. Have a look here for more info: techstacks.com/howto/configure-access-logging-in-tomcat.html

            – David Brossard
            Dec 2 '17 at 20:48











          • This works, thanks! Currently I'm only getting 0.0.0.0.0.1 from getRemoteAddr, but that's correct for localhost, right?

            – NotAMonkey24
            Dec 19 '17 at 21:11











          • Can you also explain when Filters are triggered? Right now it seems the code inside it is being run multiple times (not just at startup)

            – NotAMonkey24
            Dec 19 '17 at 21:13











          • Filters are run for every http request/response hitting that path

            – David Brossard
            Dec 20 '17 at 6:33



















          0














          After a successful login, you can get the username and IP-Address from the request and process further. I think, the challenge is, just to grab it right after the login, because tomcats form based login is pretty much transparent.
          I solved it by saving the username in the session-object and saving the data only, if the values are not already in the session. Probably you have to take into consideration, that a session might expire.






          share|improve this answer
























          • What would be the "trigger" to storing that session data?

            – NotAMonkey24
            Nov 30 '17 at 23:02











          • This could be if the sessionobject 'user' returns null if not set and the value. Something like: if ( session.getAttribute("user.name") == null ) { saveEntry(); session.setAttribute("user.name", request.getRemoteUser()) ; }

            – Fredy Fischer
            Dec 2 '17 at 15:12













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47559373%2fgetting-user-ip-on-log-in%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can use a servlet filter for that. The servlet filter can be used to intercept the incoming HTTP servlet request and do whatever you like to the request or log an entry.



          Note that Tomcat also has an access log by default that contains all HTTP requests, their status, IP, and username if any. Here is an example.



          192.168.0.47 - - [27/Nov/2017:07:18:07 -0600] "GET /example/module?wsdl HTTP/1.1" 200 3122
          192.168.0.47 - - [27/Nov/2017:07:18:07 -0600] "POST /otherapp/service HTTP/1.1" 200 251
          fe80:0:0:0:fc36:e3bb:5bc4:abce%14 - asm-admin [27/Nov/2017:07:18:08 -0600] "POST /otherapp/HEARTBEAT/?v-uiId=0 HTTP/1.1" 200 -
          fe80:0:0:0:fc36:e3bb:5bc4:abce%14 - asm-admin [27/Nov/2017:07:18:18 -0600] "POST /otherapp/HEARTBEAT/?v-uiId=0 HTTP/1.1" 200 -
          fe80:0:0:0:fc36:e3bb:5bc4:abce%14 - asm-admin [27/Nov/2017:07:18:28 -0600] "POST /otherapp/HEARTBEAT/?v-uiId=0 HTTP/1.1" 200 -





          share|improve this answer
























          • Where would I find this log? Also how would I be able to tell which request is the login request, and how would I use that to post to my database?

            – NotAMonkey24
            Nov 30 '17 at 23:22











          • You would know it is a login request because it went to the login page. You would know it is a failed login request because the code would be HTTP 401. You would know authN was successful but the user does not have the right permissions because the code would be 403. This log file is inside TOMCAT_HOME/logs. Have a look here for more info: techstacks.com/howto/configure-access-logging-in-tomcat.html

            – David Brossard
            Dec 2 '17 at 20:48











          • This works, thanks! Currently I'm only getting 0.0.0.0.0.1 from getRemoteAddr, but that's correct for localhost, right?

            – NotAMonkey24
            Dec 19 '17 at 21:11











          • Can you also explain when Filters are triggered? Right now it seems the code inside it is being run multiple times (not just at startup)

            – NotAMonkey24
            Dec 19 '17 at 21:13











          • Filters are run for every http request/response hitting that path

            – David Brossard
            Dec 20 '17 at 6:33
















          0














          You can use a servlet filter for that. The servlet filter can be used to intercept the incoming HTTP servlet request and do whatever you like to the request or log an entry.



          Note that Tomcat also has an access log by default that contains all HTTP requests, their status, IP, and username if any. Here is an example.



          192.168.0.47 - - [27/Nov/2017:07:18:07 -0600] "GET /example/module?wsdl HTTP/1.1" 200 3122
          192.168.0.47 - - [27/Nov/2017:07:18:07 -0600] "POST /otherapp/service HTTP/1.1" 200 251
          fe80:0:0:0:fc36:e3bb:5bc4:abce%14 - asm-admin [27/Nov/2017:07:18:08 -0600] "POST /otherapp/HEARTBEAT/?v-uiId=0 HTTP/1.1" 200 -
          fe80:0:0:0:fc36:e3bb:5bc4:abce%14 - asm-admin [27/Nov/2017:07:18:18 -0600] "POST /otherapp/HEARTBEAT/?v-uiId=0 HTTP/1.1" 200 -
          fe80:0:0:0:fc36:e3bb:5bc4:abce%14 - asm-admin [27/Nov/2017:07:18:28 -0600] "POST /otherapp/HEARTBEAT/?v-uiId=0 HTTP/1.1" 200 -





          share|improve this answer
























          • Where would I find this log? Also how would I be able to tell which request is the login request, and how would I use that to post to my database?

            – NotAMonkey24
            Nov 30 '17 at 23:22











          • You would know it is a login request because it went to the login page. You would know it is a failed login request because the code would be HTTP 401. You would know authN was successful but the user does not have the right permissions because the code would be 403. This log file is inside TOMCAT_HOME/logs. Have a look here for more info: techstacks.com/howto/configure-access-logging-in-tomcat.html

            – David Brossard
            Dec 2 '17 at 20:48











          • This works, thanks! Currently I'm only getting 0.0.0.0.0.1 from getRemoteAddr, but that's correct for localhost, right?

            – NotAMonkey24
            Dec 19 '17 at 21:11











          • Can you also explain when Filters are triggered? Right now it seems the code inside it is being run multiple times (not just at startup)

            – NotAMonkey24
            Dec 19 '17 at 21:13











          • Filters are run for every http request/response hitting that path

            – David Brossard
            Dec 20 '17 at 6:33














          0












          0








          0







          You can use a servlet filter for that. The servlet filter can be used to intercept the incoming HTTP servlet request and do whatever you like to the request or log an entry.



          Note that Tomcat also has an access log by default that contains all HTTP requests, their status, IP, and username if any. Here is an example.



          192.168.0.47 - - [27/Nov/2017:07:18:07 -0600] "GET /example/module?wsdl HTTP/1.1" 200 3122
          192.168.0.47 - - [27/Nov/2017:07:18:07 -0600] "POST /otherapp/service HTTP/1.1" 200 251
          fe80:0:0:0:fc36:e3bb:5bc4:abce%14 - asm-admin [27/Nov/2017:07:18:08 -0600] "POST /otherapp/HEARTBEAT/?v-uiId=0 HTTP/1.1" 200 -
          fe80:0:0:0:fc36:e3bb:5bc4:abce%14 - asm-admin [27/Nov/2017:07:18:18 -0600] "POST /otherapp/HEARTBEAT/?v-uiId=0 HTTP/1.1" 200 -
          fe80:0:0:0:fc36:e3bb:5bc4:abce%14 - asm-admin [27/Nov/2017:07:18:28 -0600] "POST /otherapp/HEARTBEAT/?v-uiId=0 HTTP/1.1" 200 -





          share|improve this answer













          You can use a servlet filter for that. The servlet filter can be used to intercept the incoming HTTP servlet request and do whatever you like to the request or log an entry.



          Note that Tomcat also has an access log by default that contains all HTTP requests, their status, IP, and username if any. Here is an example.



          192.168.0.47 - - [27/Nov/2017:07:18:07 -0600] "GET /example/module?wsdl HTTP/1.1" 200 3122
          192.168.0.47 - - [27/Nov/2017:07:18:07 -0600] "POST /otherapp/service HTTP/1.1" 200 251
          fe80:0:0:0:fc36:e3bb:5bc4:abce%14 - asm-admin [27/Nov/2017:07:18:08 -0600] "POST /otherapp/HEARTBEAT/?v-uiId=0 HTTP/1.1" 200 -
          fe80:0:0:0:fc36:e3bb:5bc4:abce%14 - asm-admin [27/Nov/2017:07:18:18 -0600] "POST /otherapp/HEARTBEAT/?v-uiId=0 HTTP/1.1" 200 -
          fe80:0:0:0:fc36:e3bb:5bc4:abce%14 - asm-admin [27/Nov/2017:07:18:28 -0600] "POST /otherapp/HEARTBEAT/?v-uiId=0 HTTP/1.1" 200 -






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 30 '17 at 23:13









          David BrossardDavid Brossard

          8,52433556




          8,52433556













          • Where would I find this log? Also how would I be able to tell which request is the login request, and how would I use that to post to my database?

            – NotAMonkey24
            Nov 30 '17 at 23:22











          • You would know it is a login request because it went to the login page. You would know it is a failed login request because the code would be HTTP 401. You would know authN was successful but the user does not have the right permissions because the code would be 403. This log file is inside TOMCAT_HOME/logs. Have a look here for more info: techstacks.com/howto/configure-access-logging-in-tomcat.html

            – David Brossard
            Dec 2 '17 at 20:48











          • This works, thanks! Currently I'm only getting 0.0.0.0.0.1 from getRemoteAddr, but that's correct for localhost, right?

            – NotAMonkey24
            Dec 19 '17 at 21:11











          • Can you also explain when Filters are triggered? Right now it seems the code inside it is being run multiple times (not just at startup)

            – NotAMonkey24
            Dec 19 '17 at 21:13











          • Filters are run for every http request/response hitting that path

            – David Brossard
            Dec 20 '17 at 6:33



















          • Where would I find this log? Also how would I be able to tell which request is the login request, and how would I use that to post to my database?

            – NotAMonkey24
            Nov 30 '17 at 23:22











          • You would know it is a login request because it went to the login page. You would know it is a failed login request because the code would be HTTP 401. You would know authN was successful but the user does not have the right permissions because the code would be 403. This log file is inside TOMCAT_HOME/logs. Have a look here for more info: techstacks.com/howto/configure-access-logging-in-tomcat.html

            – David Brossard
            Dec 2 '17 at 20:48











          • This works, thanks! Currently I'm only getting 0.0.0.0.0.1 from getRemoteAddr, but that's correct for localhost, right?

            – NotAMonkey24
            Dec 19 '17 at 21:11











          • Can you also explain when Filters are triggered? Right now it seems the code inside it is being run multiple times (not just at startup)

            – NotAMonkey24
            Dec 19 '17 at 21:13











          • Filters are run for every http request/response hitting that path

            – David Brossard
            Dec 20 '17 at 6:33

















          Where would I find this log? Also how would I be able to tell which request is the login request, and how would I use that to post to my database?

          – NotAMonkey24
          Nov 30 '17 at 23:22





          Where would I find this log? Also how would I be able to tell which request is the login request, and how would I use that to post to my database?

          – NotAMonkey24
          Nov 30 '17 at 23:22













          You would know it is a login request because it went to the login page. You would know it is a failed login request because the code would be HTTP 401. You would know authN was successful but the user does not have the right permissions because the code would be 403. This log file is inside TOMCAT_HOME/logs. Have a look here for more info: techstacks.com/howto/configure-access-logging-in-tomcat.html

          – David Brossard
          Dec 2 '17 at 20:48





          You would know it is a login request because it went to the login page. You would know it is a failed login request because the code would be HTTP 401. You would know authN was successful but the user does not have the right permissions because the code would be 403. This log file is inside TOMCAT_HOME/logs. Have a look here for more info: techstacks.com/howto/configure-access-logging-in-tomcat.html

          – David Brossard
          Dec 2 '17 at 20:48













          This works, thanks! Currently I'm only getting 0.0.0.0.0.1 from getRemoteAddr, but that's correct for localhost, right?

          – NotAMonkey24
          Dec 19 '17 at 21:11





          This works, thanks! Currently I'm only getting 0.0.0.0.0.1 from getRemoteAddr, but that's correct for localhost, right?

          – NotAMonkey24
          Dec 19 '17 at 21:11













          Can you also explain when Filters are triggered? Right now it seems the code inside it is being run multiple times (not just at startup)

          – NotAMonkey24
          Dec 19 '17 at 21:13





          Can you also explain when Filters are triggered? Right now it seems the code inside it is being run multiple times (not just at startup)

          – NotAMonkey24
          Dec 19 '17 at 21:13













          Filters are run for every http request/response hitting that path

          – David Brossard
          Dec 20 '17 at 6:33





          Filters are run for every http request/response hitting that path

          – David Brossard
          Dec 20 '17 at 6:33













          0














          After a successful login, you can get the username and IP-Address from the request and process further. I think, the challenge is, just to grab it right after the login, because tomcats form based login is pretty much transparent.
          I solved it by saving the username in the session-object and saving the data only, if the values are not already in the session. Probably you have to take into consideration, that a session might expire.






          share|improve this answer
























          • What would be the "trigger" to storing that session data?

            – NotAMonkey24
            Nov 30 '17 at 23:02











          • This could be if the sessionobject 'user' returns null if not set and the value. Something like: if ( session.getAttribute("user.name") == null ) { saveEntry(); session.setAttribute("user.name", request.getRemoteUser()) ; }

            – Fredy Fischer
            Dec 2 '17 at 15:12


















          0














          After a successful login, you can get the username and IP-Address from the request and process further. I think, the challenge is, just to grab it right after the login, because tomcats form based login is pretty much transparent.
          I solved it by saving the username in the session-object and saving the data only, if the values are not already in the session. Probably you have to take into consideration, that a session might expire.






          share|improve this answer
























          • What would be the "trigger" to storing that session data?

            – NotAMonkey24
            Nov 30 '17 at 23:02











          • This could be if the sessionobject 'user' returns null if not set and the value. Something like: if ( session.getAttribute("user.name") == null ) { saveEntry(); session.setAttribute("user.name", request.getRemoteUser()) ; }

            – Fredy Fischer
            Dec 2 '17 at 15:12
















          0












          0








          0







          After a successful login, you can get the username and IP-Address from the request and process further. I think, the challenge is, just to grab it right after the login, because tomcats form based login is pretty much transparent.
          I solved it by saving the username in the session-object and saving the data only, if the values are not already in the session. Probably you have to take into consideration, that a session might expire.






          share|improve this answer













          After a successful login, you can get the username and IP-Address from the request and process further. I think, the challenge is, just to grab it right after the login, because tomcats form based login is pretty much transparent.
          I solved it by saving the username in the session-object and saving the data only, if the values are not already in the session. Probably you have to take into consideration, that a session might expire.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 30 '17 at 7:56









          Fredy FischerFredy Fischer

          24028




          24028













          • What would be the "trigger" to storing that session data?

            – NotAMonkey24
            Nov 30 '17 at 23:02











          • This could be if the sessionobject 'user' returns null if not set and the value. Something like: if ( session.getAttribute("user.name") == null ) { saveEntry(); session.setAttribute("user.name", request.getRemoteUser()) ; }

            – Fredy Fischer
            Dec 2 '17 at 15:12





















          • What would be the "trigger" to storing that session data?

            – NotAMonkey24
            Nov 30 '17 at 23:02











          • This could be if the sessionobject 'user' returns null if not set and the value. Something like: if ( session.getAttribute("user.name") == null ) { saveEntry(); session.setAttribute("user.name", request.getRemoteUser()) ; }

            – Fredy Fischer
            Dec 2 '17 at 15:12



















          What would be the "trigger" to storing that session data?

          – NotAMonkey24
          Nov 30 '17 at 23:02





          What would be the "trigger" to storing that session data?

          – NotAMonkey24
          Nov 30 '17 at 23:02













          This could be if the sessionobject 'user' returns null if not set and the value. Something like: if ( session.getAttribute("user.name") == null ) { saveEntry(); session.setAttribute("user.name", request.getRemoteUser()) ; }

          – Fredy Fischer
          Dec 2 '17 at 15:12







          This could be if the sessionobject 'user' returns null if not set and the value. Something like: if ( session.getAttribute("user.name") == null ) { saveEntry(); session.setAttribute("user.name", request.getRemoteUser()) ; }

          – Fredy Fischer
          Dec 2 '17 at 15:12




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47559373%2fgetting-user-ip-on-log-in%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith