How to confirm email addrees in spring MVC web app

2020-05-28 18:13发布

问题:

I am registering user with email address . but i want to send the user a confirmation link where if they click then their email address gets confirmed

I am using java spring MVC hibernate mysql

回答1:

Among other tools you can use "java mail" package to send emails direclty from your application. Here's the link to API docs Java Mail API

So the scenario could be like the following:

  • User account is created. It's in unconfirmed state now;
  • You generate a unique confirmation ID to your applicaiton. The easiest way is to use java.util.UUID.randomUUID().toString(). UUID is a random globally unique value.;
  • You store the ID (e.g. 0123)+account to the database for future use;
  • Send the URL+ID (http://yourapp.com/confirm?id=0123) as an email using javamail to the user;
  • Do not show the ID in browser now;
  • The user checks inbox and see your letter;
  • The user clicks the link and request is sent to your site confirmaion servlet;
  • Confirmation servlet will search for the account associated with the specified confirmation id;
  • Confirmaion servlet set account to "confirmed state" as obvously the user has access to the specified mail box


回答2:

Use Java Mail Api to create a e-mail with confirmation. Also, you need to generate unique id to confirm the user - this info can be stored in db. After the moment user clicks on your confirmation link you should set user in 'Confirmed' state.