{"id":3983,"date":"2020-10-04T08:13:33","date_gmt":"2020-10-04T08:13:33","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=3983"},"modified":"2020-10-04T08:30:18","modified_gmt":"2020-10-04T08:30:18","slug":"using-custom-login-form-for-login-and-logout","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/using-custom-login-form-for-login-and-logout\/","title":{"rendered":"FormBased Authentication"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2020\/10\/FormBasedAuth2.jpg\" alt=\"\" \/><br \/>\n<strong>Note:The example is for CSRF disabled<\/strong><\/p>\n<ol>\n<li>From the above diagram you can see JSESSIONID and HttpStatus.OK is send is response once the credentials are authenticated<\/li>\n<li>JSESSIONID would be used for Subsequent request<\/li>\n<li>In the below code we define URL for login and logout.\n<pre>\r\n.formLogin()\r\n.loginPage(\"\/login\").permitAll().usernameParameter(\"username\").passwordParameter(\"password\")\r\n.defaultSuccessUrl(\"\/test\", true)\r\n.and()\r\n.rememberMe()\r\n.and()\r\n.logout().logoutUrl(\"\/logout\").clearAuthentication(true).invalidateHttpSession(true).deleteCookies(\"JSESSIONID\", \"remember-me\")\r\n.logoutSuccessUrl(\"\/login\")\t\t\t\r\n<\/pre>\n<\/li>\n<li>usernameParameter and passwordParameter is the name of the input form element as given in html<\/li>\n<li>defaultSuccessUrl tells the default page after authentication<\/li>\n<li>rememberMe allows the User to remember the session in server. The default JSESSIONID time is 30 minutes of inactivity. remember-me session would be active for 2 weeks and allows user to access page for 2 weeks<\/li>\n<li>logout is similar to login with following\n<pre>\r\n.logout().logoutUrl(\"\/logout\")\r\n.clearAuthentication(true)\r\n.invalidateHttpSession(true)\r\n.deleteCookies(\"JSESSIONID\", \"remember-me\")\r\n.logoutSuccessUrl(\"\/login\")\t\t\t\r\n<\/pre>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2020\/10\/login.jpg\" alt=\"\" \/><\/p>\n<p><strong>JSESSIONID and remember-me as seen in cookie in response after login button clicked<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2020\/10\/LoginReq.jpg\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2020\/10\/Logout.jpg\" alt=\"\" \/><\/p>\n<p><strong>JSESSIONID and remember-me cookie deleted in response after logout button clicked<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2020\/10\/LogoutReq.jpg\" alt=\"\" \/><\/p>\n<p><strong>login.html<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;body&gt;\r\n&lt;div class=&quot;container&quot;&gt;\r\n    &lt;form class=&quot;form-signin&quot; method=&quot;post&quot; action=&quot;\/login&quot;&gt;\r\n        &lt;table cellpadding=&quot;3&quot; cellspacing=&quot;3&quot; border=&quot;1px solid black&quot; style=&quot;border-collapse: collapse&quot;&gt;\r\n            &lt;tr&gt;\r\n                &lt;td&gt;&lt;label for=&quot;username&quot; class=&quot;sr-only&quot;&gt;Username&lt;\/label&gt;&lt;\/td&gt;\r\n                &lt;td&gt;&lt;input type=&quot;text&quot; id=&quot;username&quot; name=&quot;username&quot; class=&quot;form-control&quot; placeholder=&quot;Username&quot; required=&quot;&quot;\r\n                           autofocus=&quot;&quot;&gt;&lt;\/td&gt;\r\n            &lt;\/tr&gt;\r\n            &lt;tr&gt;\r\n                &lt;td&gt;&lt;label for=&quot;password&quot; class=&quot;sr-only&quot;&gt;Password&lt;\/label&gt;&lt;\/td&gt;\r\n                &lt;td&gt;&lt;input type=&quot;password&quot; id=&quot;password&quot; name=&quot;password&quot; class=&quot;form-control&quot; placeholder=&quot;Password&quot;\r\n                           required=&quot;&quot;&gt;&lt;\/td&gt;\r\n            &lt;\/tr&gt;\r\n            &lt;tr&gt;\r\n                &lt;td&gt;&lt;label for=&quot;remember-me&quot; class=&quot;sr-only&quot;&gt;Remember Me?&lt;\/label&gt;&lt;\/td&gt;\r\n                &lt;td&gt;&lt;input type=&quot;checkbox&quot; id=&quot;remember-me&quot; name=&quot;remember-me&quot; class=&quot;form-control&quot;&gt;&lt;\/td&gt;\r\n            &lt;\/tr&gt;\r\n            &lt;tr&gt;\r\n                &lt;td colspan=&quot;2&quot; align=&quot;center&quot;&gt;&lt;button class=&quot;btn btn-lg btn-primary btn-block&quot; type=&quot;submit&quot;&gt;Login&lt;\/button&gt;&lt;\/td&gt;\r\n            &lt;\/tr&gt;\r\n        &lt;\/table&gt;\r\n    &lt;\/form&gt;\r\n&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n<\/pre>\n<p><strong>test.html<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nYou have been Logged In\r\n\r\n&lt;form class=&quot;form-signin&quot; method=&quot;get&quot; action=&quot;\/logout&quot;&gt;\r\n    &lt;button class=&quot;btn btn-lg btn-primary btn-block&quot; type=&quot;submit&quot;&gt;Logout&lt;\/button&gt;\r\n&lt;\/form&gt;\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Override\r\nprotected void configure(HttpSecurity httpSecurity) throws Exception{\r\n\thttpSecurity\r\n\t\t\t\t.csrf().disable()\r\n\t\t\t\t.authorizeRequests()\r\n\t\t\t\t.antMatchers(&quot;\/&quot;, &quot;index&quot;, &quot;\/css\/*&quot;, &quot;\/js\/*&quot;).permitAll()\r\n\t\t\t\t.antMatchers(&quot;\/api\/**&quot;).hasRole(&quot;ADMIN&quot;)\r\n\t\t\t\t.anyRequest()\r\n\t\t\t\t.authenticated()\r\n\t\t\t\t.and()\r\n\t\t\t\t.formLogin()\r\n\t\t\t\t.loginPage(&quot;\/login&quot;).permitAll().usernameParameter(&quot;username&quot;).passwordParameter(&quot;password&quot;)\r\n\t\t\t\t.defaultSuccessUrl(&quot;\/test&quot;, true)\r\n\t\t\t\t.and()\r\n\t\t\t\t.rememberMe()\r\n\t\t\t\t.and()\r\n\t\t\t\t.logout().logoutUrl(&quot;\/logout&quot;).clearAuthentication(true).invalidateHttpSession(true).deleteCookies(&quot;JSESSIONID&quot;, &quot;remember-me&quot;)\r\n\t\t\t\t.logoutSuccessUrl(&quot;\/login&quot;);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Note:The example is for CSRF disabled From the above diagram you can see JSESSIONID and HttpStatus.OK is send is response once the credentials are authenticated JSESSIONID would be used for Subsequent request In the below code we define URL for login and logout. .formLogin() .loginPage(&#8220;\/login&#8221;).permitAll().usernameParameter(&#8220;username&#8221;).passwordParameter(&#8220;password&#8221;) .defaultSuccessUrl(&#8220;\/test&#8221;, true) .and() .rememberMe() .and() .logout().logoutUrl(&#8220;\/logout&#8221;).clearAuthentication(true).invalidateHttpSession(true).deleteCookies(&#8220;JSESSIONID&#8221;, &#8220;remember-me&#8221;) .logoutSuccessUrl(&#8220;\/login&#8221;) usernameParameter and&hellip; <a href=\"https:\/\/codethataint.com\/blog\/using-custom-login-form-for-login-and-logout\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3983","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3983"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3983\/revisions"}],"predecessor-version":[{"id":3995,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3983\/revisions\/3995"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}