Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects

Sunday, 8 September 2013

Spring Security Hello World Example - Login & Logout with Spring Security

In this particular blog we will talk about a very useful feature of Spring i.e. Spring Security. It enables the developer to integrate security features easily and in a managed way. Spring security captures all incoming http requests by applying servlet filters and route them according to user defined security configurations. In this blog we will show you how to implement spring security in a spring MVC application. We will make all incoming requests starting from /home* to undergo a login process if the user is not logged in already.

Spring Security Dependencies

To implement spring security we need three jars mainly, 'spring-security-core', 'spring-security-web' & 'spring-security-config'.
<project xmlns="" xmlns:xsi=""
 <name>SpringSecurityExample Maven Webapp</name>


  <!-- Spring 3 -->



  <!-- Spring Security -->







We need to add some security configuration here in web.xml. We need to add spring-security filter chain here to tell the container about security settings and configurations.
<web-app id="WebApp_ID" version="2.4"
 xmlns="" xmlns:xsi=""

 <display-name>Spring MVC Application</display-name>

 <!-- Spring MVC -->



 <!-- Spring Security -->




This is a simple spring-dispatcher, we have added a view resolver here and a component scan attribute to scan application controllers.
<beans xmlns=""
 xsi:schemaLocation="    ">

 <context:component-scan base-package="com.beingjavaguys.controller" />

  <property name="prefix">
  <property name="suffix">



This is so called spring-security configuration file to specify login details and security url's to check for login.
<beans:beans xmlns=""
 xmlns:beans="" xmlns:xsi=""

 <http auto-config="true">
  <intercept-url pattern="/home*" access="ROLE_ADMIN" />

    <user name="nagesh" password="chauhan@123" authorities="ROLE_ADMIN" />



This is simple spring controller having a single request mapping, after a success to the request the method will open a jsp view along with a sended message.

package com.beingjavaguys.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

public class HomeController {

 public ModelAndView getHome() {
  String string = "Congrats ! You are done with your first Spring Security configuration !";
  return new ModelAndView("home", "string", string);



We have routed the flow to '/home' so that the login implementation with spring security can be demonstrated.
<h2>Hello World!</h2>


And finally it comes to logout part, 'j_spring_security_logout' just click on this and logged in user will be logeed out on the fly.
<%@ taglib prefix="c" uri=""%>
<title>Being Java Guys | Hello World</title>

  <h2>Being Java Guys | Hello World</h2>
   Click To | <a href="<c:url value="j_spring_security_logout" />">

If everything goes right you will see following screens:

In this particular blog we came across 'Spring Security Hello World Example - Login & Logout with Spring Security'. In upcoming blogs we will see more about spring and Other Implementations in Java.

Thanks for reading !
Being Java Guys Team

Download "Spring Security Hello World Example" from "SkyDrive"


  1. thx for the tutorial nagesh, only thing download link is not working...

  2. hey, this is working nice... but could you please tell me how can I customize the login form? I want to change how it appears, please help me with tha

  3. If anyone want to become an oracle certified professional reach FITA, which offers best Oracle Training in Chennai with years of experienced professionals. For more details about oracle, sql training visit this site.

    Oracle Course in Chennai

  4. joyce christopher7 April 2015 at 05:27

    HI, am looking python certification if possible in chennai Php training in chennai<./a>

  5. Thank you so much for sharing this informative blog. Your technical information is really useful for me. Keep update your blog.


    PHP Training in Chennai

  6. It was worth post for the reader, if you want to be a professional in salesforce just reach here,...Salesforce training in Chennai


Like Us on Facebook

Like Us On Google+


Skype: neel4soft