Spring Security Login Form Example

In my previous post I have written a very simple spring security example without any login form mechanism. This example has the slight modification to include the login form example for the authentication. The changes are reflected in the spring-security.xml file. If the user is not logged in, the request will be redirected to the login page which is configured in the spring-security.xml file. If the authentication is failed then it would redirect to the failure pages. This is the slight advanced from the previous article. If you have any questions, please write it in the comments section.

1. Spring MVC Configuration


<beans xmlns="http://www.springframework.org/schema/beans"

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

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



package com.spring.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;

public class ExampleController {

	@RequestMapping(value="/basics", method = RequestMethod.GET)
	public String printWelcome(ModelMap model, Principal principal ) {
		String name = principal.getName();
		model.addAttribute("username", name);
		model.addAttribute("message", "Spring Security Custom Form example");
		return "basics";

	@RequestMapping(value="/login", method = RequestMethod.GET)
	public String login(ModelMap model) {
		return "login";

	@RequestMapping(value="/loginfailed", method = RequestMethod.GET)
	public String loginerror(ModelMap model) {
		model.addAttribute("error", "true");
		return "login";


	<h1>Message : ${message}</h1>

2. Spring Security Configuration

form-login element is used for configuring the login form for the application. It has the attributes login-page, default-target-url and authentication-failure-url. As the name of the attribute itself implies the purpose, it is very easy to understand for you. Whatever the URL you have configured in the file will be used while user access the application. logout element is configured to redirect the user once the logout action is preformed.


<beans:beans xmlns="http://www.springframework.org/schema/security"

<http auto-config="true">
		<intercept-url pattern="/basics*" access="ROLE_USER" />
		<form-login login-page="/login" default-target-url="/basics"
			authentication-failure-url="/loginfailed" />
		<logout logout-success-url="/logout" />

			<user name="Spring" password="spring" authorities="ROLE_USER" />


It is very important to understand the variables used in this JSP file. The variables used in the page j_security_check, j_negotiate_check, j_username and j_password are predefined variables in the spring security framework. If you modify the variable names, the application will not work. Our application is configured in such a way that when user is not logged in, the first request will be forwarded to the login.jsp. Once user enters the user name and password, the values are retrived by spring security and validates against the correct values from the authentication-provider. The real beauty is that, all the work is handled by the framework itself, we are not writing any extra code.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<body onload='document.f.j_username.focus();'>
	<h3>Login with Username and Password (Spring Security Example)</h3>

	<form name='f' action="<c:url value='j_spring_security_check' />"

				<td><input type='text' name='j_username' value=''>
				<td><input type='password' name='j_password' />
				<td colspan='2'><input name="submit" type="submit"
					value="submit" />
				<td colspan='2'><input name="reset" type="reset" />


3. Spring Security and Spring MVC Integration


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
id="WebApp_ID" version="2.5">
  <display-name>Spring MVC Application</display-name>

I hope this article provided few more information on configuring the login form using the spring security. If you have any questions, please post it in the comments section. In my next article, I will write about configuring the database to authenticate the user. Please subscribe here to receive the future articles.

For other types of security unrelated to the Spring Security Login Form, if you get a second, note this other site carries numerous security related equipment for the home or your business. You can see their website here Security Cameras. For other informative security data, also look at this security website.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!