Spring Security: Login and Logout Form JSP

January 25, 2010 | By

spring security login Spring Security: Login and Logout Form JSP

When you configure spring security in your web application you can configure your login.jsp in the applicationContext-security.xml.

But how this page looks like? If you search around, you are not going to find it easily. There is many articles about how to configure spring security, but a few ones list login.jsp.

If you take a look in the spring security distribution file, you are going to find an example application, and inside it: login.jsp and logout link.

login.jsp

<%@ taglib prefix='c' uri='http://java.sun.com/jstl/core_rt' %>
<%@ page import="org.springframework.security.ui.AbstractProcessingFilter" %>
<%@ page import="org.springframework.security.ui.webapp.AuthenticationProcessingFilter" %>
<%@ page import="org.springframework.security.AuthenticationException" %>

<html>
  <head>
    <title>Login</title>
  </head>

  <body>
    <h1>Login</h1>

    <c:if test="${not empty param.login_error}">
      <font color="red">
        Your login attempt was not successful, try again.<br/><br/>
        Reason: <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/>.
      </font>
    </c:if>

    <form name="f" action="<c:url value='j_spring_security_check'/>" method="POST">
      <table>
        <tr><td>User:</td><td><input type='text' name='j_username' value='<c:if test="${not empty param.login_error}"><c:out value="${SPRING_SECURITY_LAST_USERNAME}"/></c:if>'/></td></tr>
        <tr><td>Password:</td><td><input type='password' name='j_password'></td></tr>
        <tr><td><input type="checkbox" name="_spring_security_remember_me"></td><td>Don't ask for my password for two weeks</td></tr>

        <tr><td colspan='2'><input name="submit" type="submit"></td></tr>
        <tr><td colspan='2'><input name="reset" type="reset"></td></tr>
      </table>
    </form>
  </body>
</html>

The names of the fields MUST remain the same or else your authentication will fail.

As far as the logout goes, all you have to do is send the user to a particular servlet define by spring-security.

logout link:

<a href="<c:url value="/j_spring_security_logout"/>">Logout</a>

That’s it!

Happy Coding!

Filed in: Spring, Spring Security | Tags: , , ,

Comments (24)

Links to this Post

  1. JavaPins | January 8, 2012
  1. TanT

    There is no treatment jstl tags, if we use this page instead of the default page

    // not affected

    …..

    why??

    example
    spring-sequrity.xml

  2. TanT

    Response:

    login.jsp

    security.xml

  3. RP

    This is a excellent article.

  4. Srinivas

    Hi,
    Can you please send source code above example.
    I have been working on this Spring security from past three days

  5. Prasad

    Thanks for sharing.

  6. Vikas Anand

    In spring security 3 this does not work. AuthenticationException is now in different package and same with the other class imported

  7. Ivan

    When clicking the logout link, I redirect to my welcome page and actually get hte login page again indicating that I am in fact logged off, however, if I change the url in the location bar to access a page inside the application, it appears and act like I am still loggen in.

  8. I am not able to get the example code for spring security. Please help me.

    • Loiane

      Hi Amit,

      Click on the github link, on the github you will see a download button. When you click on it, it will donwload the complete source code. Let me know if you still face any issues.
      Thanks!

  9. Hi Loiane,

    i tried to click on GitHUb top menu but while clicking i can see some other post there. There is no download link. Can you please help me to get the example or can you send example to my mail id. It will be very great help for me.

    Thanks.

  10. Hi Loiane,

    i tried to click on GitHUb top menu but while clicking i can see some other post there. There is no download link. Can you please help me to get the example or can you send example to my mail id. It will be very great help for me.
    MY mail id is: amitsingh0542@gmail.com

    Thanks.

  11. Babu

    Can you send me the Example(babu.javaprgs@gmail.com). it is more helpful fro me.

    Thanks in Advance

  12. Amit Kumar

    it is not sufficient example , plz send me another example. 

  13. santosh ganji

    hi Loiane,
    could you please tell me where can i get the source code for the spring login security? I cant see the download link clicking on github. could you please help me i this regard

  14. Daniel

    Hi Loiane,

    Can you please help me by sending me a complete source code for this login example? My email is danielvt@yahoo.com

  15. Aravind

    Hi Loiane,

    The set of jars required for 3.0 is different. can you gimme the list of minimum required jars for basic spring security using 3.0