How to do URL authentication in struts2
- by Enrique Malhotra
Hi, I am using struts2.1.6 + Spring 2.5 I have four modules in my application.
Registration Module
Admin Module
Quote Module
Location Module.
In registration module the customer can register himself and only after registering he is supposed to have access of the remaining three modules.
I want to implement something like if the action being called belongs to the registration module it will work as normal but if the action being called belongs to the rest of those three modules it first should check if the user is logged-in and session has not timed-out. if yes it should proceed normally otherwise it should redirect to the login page.
Through research I have found out that interceptors could be used for this purpose but before proceeding I thought its better to get some feedback on it from experts.
Please suggest how it should be done and If possible put some code suggestions.
Here is my struts.xml file(The struts.xml contains four different config files belonging to each module):
<struts>
<include file="struts-default.xml" />
<constant name="struts.i18n.reload" value="false" />
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="false" />
<constant name="struts.serve.static.browserCache" value="false" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.multipart.maxSize" value="10000000" />
<constant name="struts.multipart.saveDir" value="C:/Temporary_image_location" />
<include file="/com/action/mappingFiles/registration_config.xml" />
<include file="/com/action/mappingFiles/admin_config.xml" />
<include file="/com/action/mappingFiles/quote.xml" />
<include file="/com/action/mappingFiles/location_config.xml" />
</struts>
The sample registration_config.xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="registration" extends="struts-default"
namespace="/my_company">
<action name="LoginView" class="registration" method="showLoginView">
<result>....</result>
<result name="input">...</result>
</action>
</package>
</struts>
The sample admin_config.xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="admin" extends="struts-default"
namespace="/my_company">
<action name="viewAdmin" class="admin" method="showAdminView">
<result>....</result>
<result name="input">...</result>
</action>
</package>
</struts>
Same code is there in the rest of two struts2 xml config files. I have used the same namespace in all the four config files with the different package names(As you can see)