<div dir="ltr" style="text-align: justify;">
<p>In this tutorial we will discuss same <a href="https://dineshonjava.com/spring-security-form-based-login-example/"><b>previous example</b></a> of custom login form for authentication but difference is that only we using database for username and password instead of reading from XML file.</p>
<div dir="ltr" style="background-color: white; font-family: Arial,Tahoma,Helvetica,FreeSans,sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<h2><b>Required Tools used for this Application:</b></h2>
<ul style="line-height: 1.4; margin: 0.5em 0px; padding: 0px 2.5em;">
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;">Spring MVC 3.0.1</li>
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;">Spring Security 3.1.0</li>
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;">STS 2.8.1.RELEASE</li>
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;">Tomcat 7</li>
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;">Jdk 1.7</li>
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;">MySQL Database</li>
</ul>
</div>
<p>To understand this application you have some prior knowledge about the <a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/ target=" rel="noopener"><b>Spring MVC web application.</b></a></p>
<h2 id="ads-id" align="center">Spring Security Login Form Using Database</h2>
<p><b>Step 1: </b>Please download the following more jars <b>for Spring Security Lib</b> from its<a style="color: #888888; text-decoration: initial;" href="http://www.springsource.com/download/community?project=Spring%20Security&;version=3.2.0.M1" target="_blank" rel="noopener"><b> official site</b></a>.<br />
To get started with the implementation, following jars need to be present in the class path of the project.</p>
<ul style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; margin: 0.5em 0px; orphans: 2; padding: 0px 2.5em; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;"><i><b>spring-security-acl-3.1.3.RELEASE.jar</b></i></li>
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;"><i><b>spring-security-aspects-3.1.3.RELEASE.jar</b></i></li>
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;"><i><b>spring-security-cas-3.1.3.RELEASE.jar</b></i></li>
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;"><i><b>spring-security-config-3.1.3.RELEASE.jar</b></i></li>
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;"><i><b>spring-security-core-3.1.3.RELEASE.jar</b></i></li>
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;"><i><b>spring-security-crypto-3.1.3.RELEASE.jar</b></i></li>
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;"><i><b>spring-security-ldap-3.1.3.RELEASE.jar</b></i></li>
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;"><i><b>spring-security-openid-3.1.3.RELEASE.jar</b></i></li>
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;"><i><b>spring-security-remoting-3.1.3.RELEASE.jar</b></i></li>
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;"><i><b>spring-security-taglibs-3.1.3.RELEASE.jar</b></i></li>
<li style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;"><i><b>spring-security-web-3.1.3.RELEASE.jar</b></i></li>
</ul>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p><b>Step 2: </b>Create the project &#8220;<b>SpringSecurityLoginDatabase</b>&#8221; with packages<b>&#8220;com.dineshonjava.security.controller&#8221;</b> and create the &#8220;<b>LoginController.java</b>&#8221; file in this package.</p>
<p><b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #274e13; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">Step 3:</b> Some more folders also create on the &#8220;<b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #274e13; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">WEB-INF</b>&#8221; folder with name <b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #274e13; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">libs, views </b>for <b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #274e13; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">jars</b> and <b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #274e13; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">jsp</b> files respectively. Two files &#8220;<b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #274e13; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">sdnext-servlet.xml</b>&#8221; and &#8220;<b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #274e13; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">sdnext-security.xml</b>&#8221; are created on the &#8220;<b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #274e13; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">WEB-INF</b>&#8221; folder.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/databasesecutity.png" border="0" /></div>
<h2><b>Namespace Configuration:</b></h2>
<p>The namespace configuration of the spring provides lot of shortcuts that hides much of the complexity of the framework. To start with this configuration, define a security filter in<i><b> web.xml </b></i>as shown below:<br />
<b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #274e13; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">Step 4: </b>Configuring <i style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #274e13; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b>web.xml</b></i> for Spring Security</p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">; 
 
 <;servlet>; 
 <;servlet-name>;sdnext<;/servlet-name>; 
 <;servlet-class>;org.springframework.web.servlet.DispatcherServlet<;/servlet-class>; 
 <;load-on-startup>;1<;/load-on-startup>; 
 <;/servlet>; 
 
 <;servlet-mapping>; 
 <;servlet-name>;sdnext<;/servlet-name>; 
 <;url-pattern>;/<;/url-pattern>; 
 <;/servlet-mapping>; 
 <;listener>; 
 <;listener-class>; 
 org.springframework.web.context.ContextLoaderListener 
 <;/listener-class>; 
 <;/listener>; 
 
 <;context-param>; 
 <;param-name>;contextConfigLocation<;/param-name>; 
 <;param-value>; 
 /WEB-INF/sdnext-*.xml, 
 <;/param-value>; 
 <;/context-param>; 
 
 <;welcome-file-list>; 
 <;welcome-file>;index<;/welcome-file>; 
 <;/welcome-file-list>; 
 
 <;!-- Spring Security -->; 
 <;filter>; 
 <;filter-name>;springSecurityFilterChain<;/filter-name>; 
 <;filter-class>; 
 org.springframework.web.filter.DelegatingFilterProxy 
 <;/filter-class>; 
 <;/filter>; 
 
 <;filter-mapping>; 
 <;filter-name>;springSecurityFilterChain<;/filter-name>; 
 <;url-pattern>;/*<;/url-pattern>; 
 <;/filter-mapping>; 
 
<;/web-app>; 
</pre>
<p>In the above configuration, <i><b>DelegatingFilterProxy </b></i>delegates the control to a filter implementation which is defined as a bean named <i><b>springSecurityFilterChain</b></i>. This bean is an infrastructure internal bean to handle namespace configurations. Once this configuration is done, all the incoming requests enter the spring framework for security checks.</p>
<h2><b>Security Configuration:</b></h2>
<p>The security configuration is done in XML file and can have any name such as <b>sdnext-security.xml.</b> This file needs to be loaded explicitly from <b>web.xml</b>. This is done by adding <b>ContextLoadListener</b>. The following lines needs to be added before security filter definition in <b>web.xml.</b></p>
<p><b><b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #274e13; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">Step 5: </b>Spring Securing Configuration file (<b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #274e13; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><i>sdnext-security.xml</i></b>)</b><br />
<b> </b></p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:> 
This configuration is done to enable form-login authentication model where the login page is <b>login.jsp</b>. Note that in the intercept tag, pattern for <b>/index*</b> is given and access rule is defined as ROLE_USER. That means <b>/index*</b> is redirect to <b>/login</b> to checked for security, which makes sense as login.jsp is the starting point from where the user is authenticated. 
 
The tag <b><;security:authentication-manager>;</b> processes the authentication information; <b><;security:authentication-provider>;</b> defines the credential information and the roles given to each user (authentication information). 
<b> 
</b> <b>Step 6:</b> Spring Configuration File (<b>sdnext-servlet.xml</b>) 
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:tx="http://www.springframework.org/schema/tx" 
 xsi:schemaLocation=" 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">; 
 
 <;context:property-placeholder location="classpath:resources/database.properties" />; 
 <;context:component-scan base-package="com.dineshonjava.security" />; 
 
 <;tx:annotation-driven transaction-manager="hibernateTransactionManager"/>; 
 
 <;bean id="jspViewResolver" 
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">; 
 <;property name="viewClass" 
 value="org.springframework.web.servlet.view.JstlView" />; 
 <;property name="prefix" value="/WEB-INF/views/" />; 
 <;property name="suffix" value=".jsp" />; 
 <;/bean>; 
 
 <;bean id="dataSource" 
 class="org.springframework.jdbc.datasource.DriverManagerDataSource">; 
 <;property name="driverClassName" value="${database.driver}" />; 
 <;property name="url" value="${database.url}" />; 
 <;property name="username" value="${database.user}" />; 
 <;property name="password" value="${database.password}" />; 
 <;/bean>; 
 
 <;bean id="sessionFactory" 
 class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">; 
 <;property name="dataSource" ref="dataSource" />; 
 <;property name="hibernateProperties">; 
 <;props>; 
 <;prop key="hibernate.dialect">;${hibernate.dialect}<;/prop>; 
 <;prop key="hibernate.show_sql">;${hibernate.show_sql}<;/prop>; 
 <;prop key="hibernate.hbm2ddl.auto">;${hibernate.hbm2ddl.auto}<;/prop>; 
 <;/props>; 
 <;/property>; 
 <;/bean>; 
 
<;/beans>; 
</pre>
<p><b>Step 7: Creating LoginController class (LoginController.java)</b></p>
<pre class="highlight">package com.dineshonjava.security.controller; 
 
import java.security.Principal; 
 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@Controller 
public class LoginController { 
 
 @RequestMapping(value="/index", method = RequestMethod.GET) 
 public String executeSecurity(ModelMap model, Principal principal ) { 
 
 String name = principal.getName(); 
 model.addAttribute("author", name); 
 model.addAttribute("message", "Welcome To Login Form Based Spring Security Example!!!"); 
 return "welcome"; 
 
 } 
 
 @RequestMapping(value="/login", method = RequestMethod.GET) 
 public String login(ModelMap model) { 
 
 return "login"; 
 
 } 
 
 @RequestMapping(value="/fail2login", method = RequestMethod.GET) 
 public String loginerror(ModelMap model) { 
 
 model.addAttribute("error", "true"); 
 return "login"; 
 
 } 
 
 @RequestMapping(value="/logout", method = RequestMethod.GET) 
 public String logout(ModelMap model) { 
 
 return "login"; 
 
 } 
} 
</pre>
<p><b>Creating database.properties</b></p>
<pre class="highlight">database.driver=com.mysql.jdbc.Driver 
database.url=jdbc:mysql://localhost:3306/DAVDB 
database.user=root 
database.password=root 
hibernate.dialect=org.hibernate.dialect.MySQLDialect 
hibernate.show_sql=true 
hibernate.hbm2ddl.auto=update 
</pre>
<h2><b>JSP Views-</b></h2>
<p>In custom login form, you have to follow Spring Security standard name :<br />
1. j_spring_security_check – Login service<br />
2. j_spring_security_logout – Logout service<br />
3. j_username – Username<br />
4. j_password – Password<br />
<b>Step 8: Creating welcome page (welcome.jsp)</b></p>
<pre class="highlight"><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
<;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">; 
<;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>; 
<;html>; 
<;head>; 
<;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">; 
<;title>;WELCOME TO SECURE AREA<;/title>; 
<;/head>; 
<;body>; 
 <;h1>;Message : ${message}<;/h1>; 
 <;h1>;Author : ${author}<;/h1>; 
 <;a href='<;c:url value="/j_spring_security_logout" />;' >; Logout<;/a>; 
<;/body>; 
<;/html>; 
</pre>
<p><b>login.jsp</b></p>
<pre class="highlight"><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
<;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">; 
<;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>; 
<;html>; 
<;head>; 
<;title>;Login Page For Security<;/title>; 
<;style>; 
.errorblock { 
 color: #ff0000; 
 background-color: #ffEEEE; 
 border: 3px solid #ff0000; 
 padding: 8px; 
 margin: 16px; 
} 
<;/style>; 
<;/head>; 
<;body onload='document.f.j_username.focus();'>; 
 <;h3>;Login with Username and Password (Custom Login Page)<;/h3>; 
 
 <;c:if test="${not empty error}">; 
 <;div class="errorblock">; 
 Your login attempt was not successful, try again.<;br />; Caused : 
 ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message} 
 <;/div>; 
 <;/c:if>; 
 
 <;form name='f' action="<;c:url value='j_spring_security_check' />;" 
 method='POST'>; 
 
 <;table>; 
 <;tr>; 
 <;td>;User:<;/td>; 
 <;td>;<;input type='text' name='j_username' value=''>; 
 <;/td>; 
 <;/tr>; 
 <;tr>; 
 <;td>;Password:<;/td>; 
 <;td>;<;input type='password' name='j_password' />; 
 <;/td>; 
 <;/tr>; 
 <;tr>; 
 <;td colspan='2'>;<;input name="submit" type="submit" 
 value="submit" />; 
 <;/td>; 
 <;/tr>; 
 <;tr>; 
 <;td colspan='2'>;<;input name="reset" type="reset" />; 
 <;/td>; 
 <;/tr>; 
 <;/table>; 
 
 <;/form>; 
<;/body>; 
<;/html>; 
</pre>
<p>Step 9: Create an database "DAVDB" and two tables "USERS" and other "USER_ROLES" for Spring Security for our application as follows.<br />
<b>USERS Table</b></p>
<pre class="highlight">CREATE TABLE `users` ( 
 `USER_ID` int(10) unsigned NOT NULL, 
 `USERNAME` varchar(40) NOT NULL, 
 `PASSWORD` varchar(40) NOT NULL, 
 `ACTIVE` tinyint(1) NOT NULL, 
 PRIMARY KEY (`USER_ID`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 
</pre>
<p><b>USER_ROLES table</b></p>
<pre class="highlight">Create Table 
 
CREATE TABLE `user_roles` ( 
 `USER_ROLE_ID` int(10) unsigned NOT NULL, 
 `USER_ID` int(10) unsigned NOT NULL, 
 `AUTHORITY` varchar(45) NOT NULL, 
 PRIMARY KEY (`USER_ROLE_ID`), 
 KEY `FK_user_roles` (`USER_ID`), 
 CONSTRAINT `FK_user_roles` FOREIGN KEY (`USER_ID`) REFERENCES `users` (`USER_ID`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 
 
</pre>
<p> ;</p>
<pre class="highlight">INSERT INTO `DAVDB`.`user_roles` 
 (`USER_ROLE_ID`, 
 `USER_ID`, 
 `AUTHORITY` 
 ) 
 VALUES 
 ('1', 
 '1', 
 'ROLE_USER' 
 ); 
 
</pre>
<pre class="highlight">INSERT INTO `DAVDB`.`users` 
 (`USER_ID`, 
 `USERNAME`, 
 `PASSWORD`, 
 `ACTIVE` 
 ) 
 VALUES 
 ('1', 
 'dineshonjava', 
 'sweety', 
 '1' 
 ); 
</pre>
<p><b>Step 10: Running the example</b></p>
<p>Export the example as war and deploy it Tomcat 7 server. While browsing the project you will get the following screen for login:</p>
<p>Access URL <b>“<i>http://localhost:8080/sdnext/index</i></b>“, Spring will redirect to your custom login form.<br />
<b><i>URL : http://localhost:8080/</i><i><i>sdnext</i>/login</i></b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/databasesecutity1.png" border="0" /></div>
<p><b>If username/password is wrong, authentication failed, display custom error messages.</b><br />
<b><br />
</b> <i><b>URL : http://localhost:8080/<i>sdnext</i>/fail2login</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/securitylogin2.png" border="0" /></div>
<p><b>If username/password is correct, authentication success, display requested page.</b><br />
<b><br />
</b> <i><b>URL : http://localhost:8080/sdnext/index</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/securitylogin3.png" width="600" height="216" border="0" /></div>
<p><b>If username/password is correct, authentication success, display requested page.</b><br />
<b>on requested page click on <i>Logout </i>link.</b><br />
<b><br />
</b> <i><b>URL : http://localhost:8080/sdnext/logout</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/securitylogin4.png" border="0" /></div>
<p> ;</p>
<h3><b>Download Source Code + Libs</b><br />
<a href="https://sites.google.com/site/dinesh9582486434/my-forms/SpringSecurityLoginDatabase.zip?attredirects=0&;d=1" target="_blank" rel="noopener"><b>SpringSecurityLoginDatabase.zip</b></a></h3>
<p> ;</p>
<p><b>References- </b><br />
<a href="https://dineshonjava.com/spring-security-form-based-login-example/" target="_blank" rel="noopener"><b>https://dineshonjava.com/spring-security-form-based-login-example/</b></a><br />
<a href="http://static.springsource.org/spring-security/site/" target="_blank" rel="noopener"><b>Spring Security</b></a><br />
<a href="http://static.springsource.org/spring-security/site/docs/3.0.x/reference/springsecurity.html" target="_blank" rel="noopener"><b>Spring Security documentation</b></a></p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><span style="color: red; font-size: x-large; text-align: center;"><b>Spring Security Related Posts</b></span></p>
<ul>
<li><b><a href="https://dineshonjava.com/spring-security-interview-questions-and-answers/"><span style="color: red;">Spring Security Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-java-based-configuration-with-example/"><span style="color: red;">Spring Security Java Based Configuration with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-xml-namespace-configuration-example/"><span style="color: red;">Spring Security XML Namespace Configuration Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-hello-world-example/"><span style="color: red;">Spring Security XML Based Hello World Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-form-based-login-example/"><span style="color: red;">Spring Security form-based login example </span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-login-form-using/"><span style="color: red;">Spring Security Login Form Based Example Using Database</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-http-basic/"><span style="color: red;">Spring Security Authentication Example Using HTTP Basic </span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-authorized-access/"><span style="color: red;"> Spring Security Authorized Access Control Example </span></a></b></li>
<li><b><a href="https://dineshonjava.com/customize-http-403-access-denied-page/"><span style="color: red;">Spring Security Customized Access Denied Page</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-custom-error-message/"><span style="color: red;">Spring Security Custom Error Message</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-logout-example/"><span style="color: red;"> Spring Security Logout Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-fetch-logged-in-username/"><span style="color: red;">Spring Security Fetch Logged in Username</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-password-hashing/"><span style="color: red;">Spring Security Password Hashing</span></a></b></li>
</ul>
<p> ;</p>
</div>
<p> ;</p>
<div style="background-color: #ff99cc;"> <b> <;<;<a href="https://dineshonjava.com/spring-security-hello-world-example/">previous</a><;<; || <a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/">index </a>|| >;>;<a href="https://dineshonjava.com/spring-security-http-basic/">next</a>>;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-security-form-based-login-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-security-http-basic/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '577'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…