{"id":213,"date":"2020-01-31T16:49:30","date_gmt":"2020-01-31T16:49:30","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/spring-boot\/?p=213"},"modified":"2020-08-11T17:33:32","modified_gmt":"2020-08-11T17:33:32","slug":"how-to-user-registration-page-in-spring-boot-java","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/spring-boot\/how-to-user-registration-page-in-spring-boot-java\/","title":{"rendered":"Complete Application with Spring Boot \u2013 Part 4 -User Registration Page"},"content":{"rendered":"<p>This corresponds to Part 40 of our Complete Spring Boot Application (FleetMS) and we would create the user registration page. You will need to take the following 6 steps<\/p>\n<ol>\n<li><a href=\"#t1\">Create the Registration Page<\/a><\/li>\n<li><a href=\"#t2\">Update the Model and Database<\/a><\/li>\n<li><a href=\"#t3\">Write the ConfirmPassword Script<\/a><\/li>\n<li><a href=\"#t4\">Modify the Security Config<\/a><\/li>\n<li><a href=\"#t5\">Modify the addNew Method<\/a><\/li>\n<li><a href=\"#t6\">Add a Success Message<\/a><\/li>\n<li><a href=\"#t7\">Encode Your Password<\/a><\/li>\n<\/ol>\n<p>Let&#8217;s dive right in!<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Before you begin, you need to write the endpoint for \/register.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t1\">Step 1: Create the Registration page<\/strong><\/h5>\n<p>The easiest way to do this is to make a copy of the login page and name it register.html. Then add additional fields for Firstname, Lastname and ConfirmPassword.<\/p>\n<p>Also remember to set the th:action to &#8220;\/users\/addNew&#8221;. As you already know, the method should be post.<\/p>\n<p>Add a message placeholder using the code below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;span<\/span> <span style=\"color: #0000cc;\">th:text=<\/span><span style=\"background-color: #fff0f0;\">\"${message}\"<\/span><span style=\"color: #007700;\">&gt;&lt;\/span&gt;<\/span>\r\n<\/pre>\n<p>Then set the id of the password text field to password and the id of the confirmPassword field to confirmPassword<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">Step 2: Update the Model and Database<\/strong><\/h5>\n<p>Now update the User.java class to include Firstname and Lastname fields<\/p>\n<p>Run the application and check that these fields are created in the database<\/p>\n<p>If not, then manually create them. This means, you need to open MySQL Command Prompt and write the queries.<\/p>\n<p>Moreover, ensure that the id field is an auto_increment field.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t3\">Step 3: Write the Password Confirmation Script<\/strong><\/h5>\n<p>Now we are going to write the JavaScript code to confirm that the passwords match.<\/p>\n<p>Create a new file in the js folder called register.js.<\/p>\n<p>Include the code below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">$(<span style=\"background-color: #fff0f0;\">'document'<\/span>).ready(<span style=\"color: #008800; font-weight: bold;\">function<\/span>(){\t\t\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">var<\/span> password <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">document<\/span>.getElementById(<span style=\"background-color: #fff0f0;\">\"password\"<\/span>)\r\n\t<span style=\"color: #008800; font-weight: bold;\">var<\/span> confirmPassword <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">document<\/span>.getElementById(<span style=\"background-color: #fff0f0;\">\"confirmPassword\"<\/span>);\r\n\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">function<\/span> validatePassword(){\r\n\t  <span style=\"color: #008800; font-weight: bold;\">if<\/span>(password.value <span style=\"color: #333333;\">!=<\/span> confirmPassword.value) {\r\n\t    confirmPassword.setCustomValidity(<span style=\"background-color: #fff0f0;\">\"Passwords Don't Match\"<\/span>);\r\n\t  } <span style=\"color: #008800; font-weight: bold;\">else<\/span> {\r\n\t    confirmPassword.setCustomValidity(<span style=\"background-color: #fff0f0;\">''<\/span>);\r\n\t  }\r\n\t}\t\r\n\tpassword.onchange <span style=\"color: #333333;\">=<\/span> validatePassword;\r\n\tconfirmPassword.onkeyup <span style=\"color: #333333;\">=<\/span> validatePassword;\t\t\r\n});\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t4\">Step 4: Modify the SecurityConfiguration<\/strong><\/h5>\n<p>Now we need to modify the security configuration to allow access to the register.html and register.js without authentication. Also we should allow access to the &#8220;\/user\/addNew&#8221; endpoint.<\/p>\n<p>Open the ApplicationSecurityConfig.java file<\/p>\n<p>Add an antMatcher() for &#8220;\/register&#8221; and &#8220;\/users\/addNew&#8221;.<\/p>\n<p>For the &#8220;\/register&#8221; you need to allow access the the js folder by also adding &#8220;\/js\/**&#8221;.<\/p>\n<p>So the addition to the file is as shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">antMatchers<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/register\"<\/span><span style=\"color: #333333;\">,<\/span> \r\n\t\t<span style=\"background-color: #fff0f0;\">\"\/resources\/**\"<\/span><span style=\"color: #333333;\">,<\/span> \r\n\t\t<span style=\"background-color: #fff0f0;\">\"\/css\/**\"<\/span><span style=\"color: #333333;\">,<\/span> \r\n\t\t<span style=\"background-color: #fff0f0;\">\"\/fonts\/**\"<\/span><span style=\"color: #333333;\">,<\/span> \r\n\t\t<span style=\"background-color: #fff0f0;\">\"\/img\/**\"<\/span><span style=\"color: #333333;\">,<\/span> \r\n\t\t<span style=\"background-color: #fff0f0;\">\"\/js\/**\"<\/span><span style=\"color: #333333;\">).<\/span><span style=\"color: #0000cc;\">permitAll<\/span><span style=\"color: #333333;\">()<\/span>\r\n<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">antMatchers<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/users\/addNew\"<\/span><span style=\"color: #333333;\">).<\/span><span style=\"color: #0000cc;\">permitAll<\/span><span style=\"color: #333333;\">()<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t5\">Step 5: Modify the &#8220;addNew&#8221; Method<\/strong><\/h5>\n<p>We now need to modify the method for saving a new user. This because, we want a successful user registration to redirect to the login page.<\/p>\n<p>We would also display a message telling the user that the registration was successful and he can go on to login.<\/p>\n<p>Open the UserController, change the addNew method to the one below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Modified method to Add a new user User<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@PostMapping<\/span><span style=\"color: #333333;\">(<\/span>value<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"users\/addNew\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> RedirectView <span style=\"color: #0066bb; font-weight: bold;\">addNew<\/span><span style=\"color: #333333;\">(<\/span>User user<span style=\"color: #333333;\">,<\/span> RedirectAttributes redir<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\tuserService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">save<\/span><span style=\"color: #333333;\">(<\/span>user<span style=\"color: #333333;\">);<\/span>\t\r\n\tRedirectView  redirectView<span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> RedirectView<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/login\"<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #008800; font-weight: bold;\">true<\/span><span style=\"color: #333333;\">);<\/span>\r\n        redir<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addFlashAttribute<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"message\"<\/span><span style=\"color: #333333;\">,<\/span>\r\n    \t\t<span style=\"background-color: #fff0f0;\">\"You successfully registered! You can now login\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> redirectView<span style=\"color: #333333;\">;<\/span>\t\t\t\t\r\n<span style=\"color: #333333;\">}<\/span>\t\r\n<\/pre>\n<p>Just to explain briefly what is happening here.<\/p>\n<p>First we did not use Model and addAttributes. The reason is that, values in the model attributes are cleared after redirect.<\/p>\n<p>Therefore, we make the method return a RedirectView instead of a string(view). Then we place a flashAttribute in the\u00a0RedirectAttributes object. In this way, the value of the flash attribute is preserved when a redirect occurs.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t6\">Step 6: Add the Success Message<\/strong><\/h5>\n<p>Since we need to redurect to the login page after registration, the success message will therefore be placed in the login page. But it shows only if a redirect occurs from the register page.<\/p>\n<p>Open the login page.<\/p>\n<p>Just below the opening &lt;form&gt; tag, add the following code:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;div<\/span> <span style=\"color: #0000cc;\">style=<\/span><span style=\"background-color: #fff0f0;\">\"text-align:center; font-size:20px; color:green; font-weight:bold\"<\/span><span style=\"color: #007700;\">&gt;<\/span>  \r\n    <span style=\"color: #007700;\">&lt;span<\/span> <span style=\"color: #0000cc;\">th:utext=<\/span><span style=\"background-color: #fff0f0;\">\"${message}\"<\/span><span style=\"color: #007700;\">&gt;&lt;\/span&gt;<\/span> \r\n<span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t7\">Step 7: Encode the Password<\/strong><\/h5>\n<p>Now we would encode the password using BCryptPasswordEncoder. Although there are other encoders, BCrypt offers a stronger encryption algorithm than most. We&#8217;ll do this in just three steps.<\/p>\n<ul>\n<li><strong>First<\/strong>, create a BCryptPasswordEncoder bean in the ApplicationSecurityConfig file. The bean is as shown below:<\/li>\n<\/ul>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Bean<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> BCryptPasswordEncoder <span style=\"color: #0066bb; font-weight: bold;\">bCryptPasswordEncoder<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">BCryptPasswordEncoder<\/span><span style=\"color: #333333;\">();<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<ul>\n<li><strong>Second<\/strong>, set providers encoder to use a BCryptPasswordEncoder type instead of NoOpPasswordEncoder<\/li>\n<li><strong>Third<\/strong>, open the UserService. Modify the save method such that before you save, you need to set the password. Use setPassword and then provide an encoded password.<\/li>\n<\/ul>\n<p>Now you can test the application!<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>This corresponds to Part 40 of our Complete Spring Boot Application (FleetMS) and we would create the user registration page. You will need to take &hellip; <!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":216,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[23,22],"class_list":["post-213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring-boot-tutorials","tag-bcryptpasswordencoder","tag-user-registration-page"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/comments?post=213"}],"version-history":[{"count":8,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":248,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/213\/revisions\/248"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media\/216"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media?parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/categories?post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/tags?post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}