Spring Security Login

1. Introduction

This article is going to focus on Login with Spring Security. We’re going to built on top of the simple previous Spring MVC example, as that’s a necessary part of setting up the web application along with the login mechanism.

2. The Maven Dependencies

To add Maven dependencies to the project, please see the Spring Security with Maven article. Both standard spring-security-web and spring-security-config will be required.

3. The web.xml

The Spring Security configuration in the web.xml is simple – only an additional filter added to the standard Spring MVC web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


   id="WebApp_ID" version="3.0">

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

   <!-- Spring MVC -->


   <!-- Spring Security -->


The filter – DelegatingFilterProxy – simply delegates to a Spring managed bean – the FilterChainProxy – which itself is able to benefit from full Spring bean lifecycle management and such.

2. The Spring Security configuration

The Spring configuration is mostly written in Java, but Spring Security configuration doesn’t yet support full Java and still needs to be XML for the most part. There is an ongoing effort to add Java based configuration for Spring Security, but this is not yet mature.

The overall project is using Java configuration, so the XML configuration file needs to be imported via a Java @Configuration class:

@ImportResource({ "classpath:webSecurityConfig.xml" })
public class SecSecurityConfig {
   public SecSecurityConfig() {

The Spring Security XML Configuration – webSecurityConfig.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security" 





   <http use-expressions="true">
      <intercept-url pattern="/login*" access="isAnonymous()" />
      <intercept-url pattern="/**" access="isAuthenticated()"/>

         authentication-failure-url="/login.html?error=true" />

      <logout logout-success-url="/login.html" />

            <user name="user1" password="user1Pass" authorities="ROLE_USER" />

2.1. <intercept-url>

We are allowing anonymous access on /login so that users can authenticate. We are also securing everything else.

Note that the order of the <intercept-url> element is significant – the more specific rules need to come first, followed by the more general ones.

2.2. <form-login>

  • login-page – the custom login page
  • default-target-url – the landing page after a successful login
  • authentication-failure-url – the landing page after an unsuccessful login

2.3. <authentication-manager>

The Authentication Provider is backed by a simple, in-memory implementation – InMemoryUserDetailsManager specifically  – configured in plain text. This only exists in Spring 3.1 and above and is meant to be used for rapid prototyping when a full persistence mechanism is not yet necessary.

3. The Login Form

The login form page is going to be registered with Spring MVC using the straightforward mechanism to map views names to URLs with no need for an explicit controller in between:


This of course corresponds to the login.jsp:

   <form name='f' action="j_spring_security_check" method='POST'>
            <td><input type='text' name='j_username' value=''></td>
            <td><input type='password' name='j_password' /></td>
            <td><input name="submit" type="submit" value="submit" /></td>

The Spring Login form has the following relevant artifacts:

  • j_spring_security_check – the URL where the form is POSTed to trigger the authentication process
  • j_username – the user name
  • j_password – the password

4. Further Configuring Spring Login

We briefly discussed a few configurations of the login mechanism when we introduced the Spring Security XML Configuration above – let’s go into some detail now.

One reason to override most of the defaults in Spring Security is to hide the fact that the application is secured with Spring Security and minimize the information a potential attacker knows about the application.

Fully configured, the <form-login> element looks like this:


4.1. The Login page

The custom login page is configured via the login-page attribute on <form-login>:


If this is not specified, a default URL is used – spring_security_login – and Spring Security will generate a very basic Login Form at that URL.

4.2. The POST URL for Login

The default URL where the Spring Login will POST to trigger the authentication process is /j_spring_security_check.

This URL can be overridden via the login-processing-url attribute on <form-login>:


A good reason to override this default URL is to hide the fact that the application is actually secured with Spring Security – that information should not be available externally.

4.3. The Landing page on Success

After a successful Login process, the user is redirected to a page – which by default is the root of the web application.

This can be overridden via the default-target-url attribute on <form-login>:


In case the always-use-default-target is set to true, then the user is always redirected to this page. If that attribute is set to false, then the user will be redirected to the previous page they wanted to visit before being promoted to authenticate.

4.4. The Landing page on Failure

Same as with the Login Page, the Login Failure Page is autogenerated by Spring Security at /spring_security_login?login_error by default.

This can be overridden via the authentication-failure-url attribute on <form-login>:


5. Conclusion

In this Spring Login Example we configured a simple authentication process – we discussed the Spring Security Login Form, the Security XML Configuration and some of the more advanced customizations available in the namespace.

The implementation of this Spring Login tutorial can be found in the github project – this is an Eclipse based project, so it should be easy to import and run as it is.

When the project runs locally, the sample html can be accessed at:


Reference: Spring Security Form Login from our JCG partner Eugen Paraschiv at the baeldung blog.
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply

9 + = eighteen

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books