<p>In Spring security tutorial, we will take a look into access control in Spring Security Authorization. We will discuss about authorized access through Spring Security or access control for some of the more secure resources or urls of any enterprise which some special type of authentication just like admin have some more access or authorization than user login. Sometimes you need to secure your page from unauthorized access. Authorized access is the secure access of page through a permitted username and password. For example, the admin section page can only have permission for admin only.</p>
<div dir="ltr" style="text-align: justify;">In the below example, we will ensure secure URL access by providing auto generated Login form using Spring Security. User needs to provide correct login credential to view the page. For accessing admin section, you need to provide admin login and password. While for user section, both admin and user login are permitted.</p>
<pre class="highlight"><;security:http auto-config="true">; 
 <;security:intercept-url pattern="/admin*" access="ROLE_ADMIN" />; 
 <;security:logout logout-success-url="/admin" />; 
 <;security:intercept-url pattern="/index*" access="ROLE_USER,ROLE_ADMIN" />; 
 <;security:logout logout-success-url="/index" />; 
<;/security:http>; 
</pre>
<p>It means the user with authority as<b> <i>ROLE_ADMIN</i> </b>can have access to <b>URL <i>/admin .</i></b><i> </i>Also, the<b> URL <i>/index</i></b> is open for both type of users having authority<b> <i>ROLE_USER</i> or <i>ROLE_ADMIN</i> . </b> If non authorized user try to access it, a “<b>http 403 access denied page</b>” will be displayed.</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>
<h2><b>Required Tools used for this Application:</b></h2>
<ul style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 12.727272033691406px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 1.4; 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;">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>
</ul>
<h2>Spring Security Authorization</h2>
<p>To understand this application you have some prior knowledge about the <a style="color: #888888; text-decoration: initial;" href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/" target="_blank" rel="noopener"><b>Spring MVC web application.</b></a></p>
<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><b>Step 2: </b>Create the project &#8220;<b>SpringSecurityAuthorizedAccess</b>&#8221; with packages<b>&#8220;com.dineshonjava.admin.controller&#8221;</b> and create the &#8220;<b>AdminController.java</b>&#8221; file in this package.<br />
<br 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: 13.63636302947998px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;" /> <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/authorized6.png" border="0" /></div>
<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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; 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: 13.63636302947998px; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.363636016845703px; 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 <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 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: 13.63636302947998px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; 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;">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 style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;">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 style="margin: 0px 0px 0.25em; padding: 0px; text-indent: 0px;">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 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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">Step 5: Creating welcome page (<i>welcome.jsp</i>)</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>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.<br />
<br 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: 13.63636302947998px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;" /> <b>Step 6:</b> Creating <b>AdminController </b>class (<b><b>AdminController</b>.java</b>)</p>
<pre class="highlight">package com.dineshonjava.admin.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 AdminController { 
 
 @RequestMapping(value = "/admin", method = RequestMethod.GET) 
 public String welcomeAdmin(ModelMap model, Principal principal) { 
 String username = principal.getName(); 
 model.addAttribute("author", username); 
 model.addAttribute("message", "Hello Spring Security - ADMIN PAGE"); 
 return "welcome"; 
 
 } 
 
 @RequestMapping(value = "/index", method = RequestMethod.GET) 
 public String printMessage(ModelMap model, Principal principal) { 
 
 String username = principal.getName(); 
 model.addAttribute("author", username); 
 model.addAttribute("message", "Hello Spring Security - USER LOGIN"); 
 return "welcome"; 
 } 
} 
</pre>
<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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">Step 7: </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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; 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>)</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:> 
<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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><;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 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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"> </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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">1. <;security:intercept-url pattern=“/index*” access=“ROLE_USER,</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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">ROLE_ADMIN</b>”/>;. <;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 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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">/index* </b>are secured and only users will role <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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">ROLE_USER</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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">ROLE_ADMIN </b></b>can access the pages. 
 
<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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">2. <;security:intercept-url pattern=“/admin*” access=“</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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">ROLE_ADMIN</b>”/>;. <;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 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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">/admin* </b>are secured and only admin will role <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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">ROLE_ADMIN </b></b>can access the pages.<b> </b>If you try this page with the<b> </b><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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">ROLE_USER</b></b> then you cant access the admin page. 
 
<b>3. </b><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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><;security:authentication-provider/>;</b> tag specifies the <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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">username </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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">password</b><b> </b>provider. It can be also a database table. Here we have used hard coded <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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">username </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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"> password</b>. Password is "<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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">sweety</b>" and username is "<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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">user_dineshonjava</b>" for </b><b>url patters have pattern <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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">/index* </b>as well as we access with </b><b><b>Password is "<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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">sweetu</b>" and username is "<b>admin</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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">_dineshonjava</b>"</b>.</b> 
 
For url patters have pattern <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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">/admin* </b><b><b>Password is "<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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">sweetu</b>" and username is "<b>admin</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: 13.63636302947998px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">_dineshonjava</b>"</b>.</b> 
 
<b><b>Step 8: Spring Configuration File (sdnext-servlet.xml)</b><br 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: 13.63636302947998px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;" /> </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:component-scan base-package="com.dineshonjava.admin" />; 
 
 
 <;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>; 
 
<;/beans>; 
</pre>
<p><b>Step 9: 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 loging:</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/authorized.png" border="0" /></div>
<p><b>Error messages will be displayed if wrong username and password are provided. </b><br />
<i><b>http://localhost:8080/sdnext/spring_security_login?login_error</b></i></p>
<div class="separator" style="clear: both; text-align: center;"></div>
<div class="separator" style="clear: both; text-align: center;"></div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/authorized1.png" border="0" /></div>
<p><b>And if you try to login with user login(Username<i>- user_dineshonjava, </i>Password<i>-sweety</i>), you will get the below error message :</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/authorized3-1.png" border="0" /></div>
<p><b>And if your login is admin login(Username-<i>admin_dineshonjava</i>, Password-<i>sweetu</i>) , you will get the following page :</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/authorized4.png" width="600" height="221" border="0" /></div>
<p><b>Now click on the Logot link its lands to the /index page which accessible by both users "<i>user_dineshonjava</i>" as well as "<i>admin_dineshonjava</i>". But here we access this page using the username "<i>user_</i><i>dineshonjava</i>" and password "<i>sweetu</i>". Get the following.</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/authorized5.png" width="600" height="211" border="0" /></div>
<p> ;</p>
<h2><b>Download Source Code + Libs</b><br />
<a href="https://sites.google.com/site/dinesh9582486434/my-forms/SpringSecurityAuthorizedAccess.zip?attredirects=0&;d=1" target="_blank" rel="noopener"><b>SpringSecurityAuthorizedAccess.zip</b></a></h2>
<p> ;</p>
<p><b>References- </b><br />
<a style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #888888; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302947998px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: start; text-decoration: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;" 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 style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #888888; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302947998px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: start; text-decoration: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;" href="http://static.springsource.org/spring-security/site/" target="_blank" rel="noopener"><b>Spring Security</b></a><br />
<a style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #888888; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302947998px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.363636016845703px; orphans: 2; text-align: start; text-decoration: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;" 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/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-http-basic/">previous</a><;<; || <a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/">index </a>|| >;>;<a href="https://dineshonjava.com/customize-http-403-access-denied-page/">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-http-basic/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/customize-http-403-access-denied-page/">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: '575'});
});
</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…