<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>In this tutorial we will show you the way to configure<b> Spring Security</b> with <a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/" target="_blank" rel="noopener"><b>Spring MVC web application</b></a> to secure mvc pages. We will take an spring mvc web application example in which, we will configure Spring Security to protect a page from outside access.</p>
<div id="ads-id" align="center"></div>
<p><b>Spring Security</b> allows to you to integrate security features with J2EE web application easily, it take care about all incoming HTTP requests via servlet filters, and implements &#8220;<b>user defined</b>&#8221; security checking.</p>
<p>In this tutorial, we show you how to integrate <b>Spring Security 3.0 with Spring MVC3 </b>web application to secure URL access. After implemented Spring security, to view the content of the page, users need to key in correct “<b>username</b>” and “<b>password</b>”.</p>
<p><b>Required Tools used for this Application:</b></p>
<ul>
<li>Spring MVC 3.0.1</li>
<li>Spring Security 3.1.0</li>
<li>STS 2.8.1.RELEASE</li>
<li>Tomcat 7</li>
<li>Jdk 1.7</li>
</ul>
</div>
<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>To understand this application you have some prior knowledge about the <a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/" target="_blank" rel="noopener"><b>Spring MVC web application.</b></a></p>
<p>In this tutorial, there is an example of <b>Hello World page</b> that is managed by <b>Spring MVC framework</b>. We will configure<b> Spring Security</b> in this example and will make the <b>Hello World page</b> secure. User have to authenticate user to view <b>Hello World page</b>.</p>
<p><b>Step 1: </b>Please download the following more jars <b>for Spring Security Lib</b> from its<a href="http://www.springsource.com/download/community?project=Spring%20Security&;version=3.2.0.M1" target="_blank" rel="noopener"><b> official site</b></a>.</p>
<ul style="text-align: left;">
<li><i><b>spring-security-acl-3.1.3.RELEASE.jar</b></i></li>
<li><i><b>spring-security-aspects-3.1.3.RELEASE.jar</b></i></li>
<li><i><b>spring-security-cas-3.1.3.RELEASE.jar</b></i></li>
<li><i><b>spring-security-config-3.1.3.RELEASE.jar</b></i></li>
<li><i><b>spring-security-core-3.1.3.RELEASE.jar</b></i></li>
<li><i><b>spring-security-crypto-3.1.3.RELEASE.jar</b></i></li>
<li><i><b>spring-security-ldap-3.1.3.RELEASE.jar</b></i></li>
<li><i><b>spring-security-openid-3.1.3.RELEASE.jar</b></i></li>
<li><i><b>spring-security-remoting-3.1.3.RELEASE.jar</b></i></li>
<li><i><b>spring-security-taglibs-3.1.3.RELEASE.jar</b></i></li>
<li><i><b>spring-security-web-3.1.3.RELEASE.jar</b></i></li>
</ul>
<p><b>Step 2: </b>Create the project &#8220;<b>SpringSecurityHelloExample</b>&#8221; with packages <b>&#8220;com.dineshonjava.security.controller&#8221;</b> and create the &#8220;<b>HelloSecurityController.java</b>&#8221; file in this package.</p>
<p><b>Step 3:</b> Some more folders also create on the &#8220;<b>WEB-INF</b>&#8221; folder with name <b>libs, views </b>for <b>jars </b>and <b>jsp</b> files respectively. Two files &#8220;<b>sdnext-servlet.xml</b>&#8221; and &#8220;<b>sdnext-security.xml</b>&#8221; are created on the &#8220;<b>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/security.png" border="0" /></div>
</div>
<p><b>Step 4: </b>Configuring <i><b>web.xml</b></i> for Spring Security</p>
<pre class="highlight"><;web-app version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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>;*.html<;/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.html<;/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 <b>web.xml</b>, we have configured <b>Spring MVC</b> to manage the request came for the URL <b>“*.html”</b>. For configuring Spring Security we do the following :</p>
<ol>
<li>First of all, we have to register <b>org.springframework.web.filter.DelegatingFilterProxy</b> filter in <b>web.xml</b>. This filter manages the securing of the web pages.</li>
<li>The filter will manage the requested URL <b>“/*”</b>. That means all the requests will go through the filter so that it can authenticate the user of particulate web pages that we will configured as secured pages with Spring Security.</li>
<li>Register <b>org.springframework.web.context.ContextLoaderListener</b> listener provided in Spring so that it can configure spring context on server startup.</li>
</ol>
<p><b>Step 5: Creating welcome page (<i>welcome.jsp</i>)</b></p>
</div>
<pre class="highlight"><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
 
<;html>; 
<;head>; 
 
<;title>;WELCOME TO SECURE AREA<;/title>; 
<;/head>; 
<;body>; 
 
Message : ${message} 
 
Author : ${author} 
<;/body>; 
<;/html>; 
</pre>
</div>
<p>Our welcome page is very simple that only shows a message that is stored in model object. The message is provided by the controller class.</p>
<p>Step 6: Creating HelloSecurityController class (HelloSecurityController.java)</p>
</div>
<pre class="highlight">package com.dineshonjava.security.controller; 
 
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 
@RequestMapping("/index") 
public class HelloSecurityController { 
 
 @RequestMapping(method = RequestMethod.GET) 
 public String executeSecurity(ModelMap model) { 
 
 model.addAttribute("message", "Spring Security Hello World"); 
 model.addAttribute("author", "By DineshOnJava.com"); 
 return "welcome"; 
 
 } 
} 
</pre>
<p><b>Step 7: </b>Spring Securing Configuration file (<b><i>sdnext-security.xml</i></b>)</p>
<pre class="highlight"><;beans xmlns:p="http://www.springframework.org/schema/p" xmlns:> 
After that we will have to create a Spring Security Configuration file, in which have to define the security constrains that are to be applied to our application. You will see a lot of new things in this file. I will explain all the tags one by one make the things clear to you. 
<ol> 
 	<li><b><;security:http</b>/>; tag is used to define security setting for web application for defining access constrains for pages, defining login pages, login process to use, activate remember me option, customizing session level setting etc. Here we have used only one option i.e. <b> <;security:intercept-url pattern=“/index*” access=“ROLE_USER”/>;. <;security:intercept-url/>;</b> tag is used to define url patterns to be secure and the definition of the roles who can access them. In our example all url patters have pattern <b>/index* </b>are secured and only user will role <b>ROLE_USER</b> can access the pages.</li> 
 	<li><b><;security:authentication-manager/>;</b> tag is used to define method of authentication of the user on the basis of that user will be able to access a page.</li> 
 	<li><b><;security:authentication-provider/>;</b> tag specifies the <b>username </b>and <b> password </b>provider. It can be also a database table. Here we have used hard coded <b>username </b>and <b>password</b>. Password is "<b>sweety</b>" and username is "<b>dineshonjava</b>".</li> 
</ol> 
<b>Step 8: Spring Configuration File (sdnext-servlet.xml)</b> 
 
</div> 
<pre class="highlight"><;beans xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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:component-scan base-package="com.dineshonjava.security" />; 
 
 <;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="jspViewResolver">; 
 <;property name="viewClass" 
 value="org.springframework.web.servlet.view.JstlView" />; 
 <;property name="prefix" value="/WEB-INF/views/"/>; 
 <;property name="suffix" value=".jsp" />; 
 <;/bean>; 
 
 
<;/beans>; 
</pre>
<p><b>Step 9:</b> Running the example</p>
<p>Export the example as war and deploy it Tomcat <b><b>7</b></b> server. While browsing the project you will get the following screen for loging:</p>
<p><i>http://localhost:8080/sdnext/spring_security_login</i><br />
<i> </i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/security1.png" border="0" /></div>
<h2 style="text-align: left;"><i><b>From where is this login page came? </b></i></h2>
<p>We have not created it.<b> Actually, this is the default login page provided by Spring Security</b>. We can also customize it to use our own login page. We will see an example also relate to this.</p>
<p><b>Error messages will be displayed if wrong username and password are provided. </b><br />
<b><br />
</b> <i><b>http://localhost:8080/sdnext/spring_security_login?login_error</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/security2.png" border="0" /></div>
<p><i><b> </b></i>If <b>correct username and password are provided</b>, Spring security will redirect to the original requested URL and display the content of the page.</p>
<p><i><b>http://localhost:8080/sdnext/</b></i><br />
<i><b><br />
</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/security3.png" border="0" /></div>
<p><i><b> </b></i></p>
</div>
<p><b>Download Source Code + Libs</b><br />
<b><a href="https://sites.google.com/site/dinesh9582486434/my-forms/SpringSecurityHelloExample.zip?attredirects=0&;d=1" target="_blank" rel="noopener">SpringSecurityHelloExample.zip</a></b></p>
<p><b>References</b></p>
<ol>
<li><a href="http://static.springsource.org/spring-security/site/" target="_blank" rel="noopener">Spring Security</a></li>
<li><a href="http://static.springsource.org/spring-security/site/docs/3.0.x/reference/springsecurity.html" target="_blank" rel="noopener">Spring Security documentation</a></li>
</ol>
<p> ;</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-take-baby-step-to-secure/">previous</a><;<; || <a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/">index </a>|| >;>;<a href="https://dineshonjava.com/spring-security-form-based-login-example/">next</a>>;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-security-form-based-login-example/">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: '579'});
});
</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…