Until you use HTTPS, the method you offer is extremely unsafe. I would suggest creating some kind of session token (for example, use java.util.UUID.randomUUID()
) and set it as a cookie and save it somewhere on the server side so that later you can identify the user associated with this identifier cookie session.
This gives you the option to reset a specific session cookie if you believe that some kind of fraud is occurring and there is no direct connection between the username / password and the cookie you use. But note: this method is still vulnerable to a man-in-the-middle attack.
Regarding the expiration: yes, the cookie becomes invalid and can be deleted by the browser if it has expired. But you can set a cookie on something in the year 3000, so that it lives forever.
source share