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!
Comments (24)
Links to this Post
- JavaPins | January 8, 2012



There is no treatment jstl tags, if we use this page instead of the default page
// not affected
…..
why??
example
spring-sequrity.xml
Response:
login.jsp
security.xml
This is a excellent article.
Hi,
Can you please send source code above example.
I have been working on this Spring security from past three days
@Srinivas
All the code is available in my github repository.
Thanks for sharing.
In spring security 3 this does not work. AuthenticationException is now in different package and same with the other class imported
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.
Hi Ivan, please reach Spring Security forum for this question. Thanks!
I am not able to get the example code for spring security. Please help me.
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!
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.
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.
Can you send me the Example(babu.javaprgs@gmail.com). it is more helpful fro me.
Thanks in Advance
it is not sufficient example , plz send me another example.
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
Hi Loiane,
Can you please help me by sending me a complete source code for this login example? My email is danielvt@yahoo.com
Hi santosh ganji,
the source code is presented on the post.
Hi Daniel,
The source code is on this post.
Simply copy it and paste into your jsp.
Thanks!
Hi Babu,
The source code is on the code.
Simply copy and paste it on your project.
Thanks!
What exactly do you need Amit?
Thanks!
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
Hi Aravind,
I have a basic Spring Security 3 example on my github.
You can get the minimum required jars list from this project:
https://github.com/loiane/spring-security-3
Thanks!