Now when we have a basic login stuff working, its time to start polishing the work so that we can move towards a system which can be used for production and customer facing.
Our own login page
The login page we were using was provided by the spring security which will not match the look and feel of our overall application, so we now need to have our own login page.
so to do this we need to change the security config as given below:
and now if we run the application again we will see following:
Our own login page
The login page we were using was provided by the spring security which will not match the look and feel of our overall application, so we now need to have our own login page.
so to do this we need to change the security config as given below:
- we specified /login/form as our login page url which could go through a controller or just specify this url in the web.xml and specify the jsp file which will be hit when /login/form requested
- login-processing-url will go to /j_spring_security_check by default and specifies the url where the login form shall be submitted to.
- username and password parameters default to j_username and j_password but we can change then as i did above (customusername and custompassword).
- authentication_failure_url specifies the destination to go to when authentication is failed.
In web.xml file we have included the mapping for /login/form as following
Here is the custom login page
The custom login page is essentially the jsp page with a form having login and password fields, lets have a look at the login page.
- We are looking for ${param.error} if there was problem because we have specified this param in authentication-failure-url attribute of <form-login tag in security config
Configuring Logout!
Last but not least we have to configure the logout logic for the app,
- configure the logout url in security config file as shown below
- Add a logout link in your jsp pages which will go to /logout as we have configured it above.
- Add a message on login.jsp (because the logout functionality will to /login/form?logout as a result) and check for this logout param in the request and if its present then show the appropriate message as below:
if you run the application at this point you will get an error message from browser saying something like the page is in a redirection loop
This is because Spring is No More rendering the login page and we have our own login page which is rendered when users hit /login or any other page that requires authentication.
- we have this rule in our security config
- <intercept-url pattern="/**" access="ROLE_USER"/>
- Which means only users with ROLE_USER will be able to see ANY PAGE in the application
- which means if users want to even go to our login page they wont be allowed to go there
- hence we need to change it.
Here what we will do:
so we have added some additional rules say skip any security stuff if users try to access / which is root of the application i.e. Welcome page or any page that has /login in its url which means the it cannot be a page which is meant to be secure or /logout which is for obvious reasons.
There is an additional url for ROLE_USER which is just an example of having as many different role as you want so basically and URL having /events/ in it will not be available if the user does not have ROLE_USER
There is an additional url for ROLE_USER which is just an example of having as many different role as you want so basically and URL having /events/ in it will not be available if the user does not have ROLE_USER
and now if we run the application again we will see following:
Hurrayyy!!!
when you hit this login button with the correct username and password you will see the secured page you requested for (in my case its index.jsp which i have specified in <welcome-file-list> in web.xml
below is what i see in my case when the login is successful.
Whats the next scene please!!!
.jpg)
comments are welcome!
ReplyDelete