[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2253
  • Last Modified:

Session handling in Struts 2.1.6

Hello folks,

I have a project with the following setup:

Tomcat 6.x
Struts 2.1.6
DisplayTag 1.2
Spring 2.x (1 or 5, don't remember now)

I want to know to to do session controlling in every action of my app, like if the users weren't logged in, they're redirect to certain page to login (in the case of my project, either the user come to a special crafted url like login/SPECIALHASHTOLOGIN or won't enter at all.

Need more details?

Thx in advance.
0
dgomesbr
Asked:
dgomesbr
  • 2
1 Solution
 
dgomesbrAuthor Commented:
I'll need modify the default interceptor stack (or create a custom stack) and add a custom interceptor. This custom interceptor will need to implement SessionAware to access the user session, and must implement your custom logic (which action to redirect to, which URLs do not need protection, etc.).

Here is a good tutorial of a LoginInterceptor that behaves similar to what you are requesting:
 
http://www.vitarara.org/cms/struts_2_cookbook/creating_a_login_interceptor 
0
 
dgomesbrAuthor Commented:
Some samples of struts.xml and applicationContext.xml
struts.xml:
<?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>
    <constant name="struts.objectFactory" value="spring" />
    <constant name="struts.devMode" value="true" />
	<constant name="struts.enable.SlashesInActionNames" value="true" />
    
    <package name="renaconPackage" extends="struts-default">
	 	<interceptors>
	 		<interceptor name="login" class="loginInterceptor"/>
			
			<!-- stack seguro -->	  	    
			<interceptor-stack name="defaultLoginStack">
				<interceptor-ref name="params" />
				<interceptor-ref name="login" />
				<interceptor-ref name="prepare" />
				<interceptor-ref name="chain" />
				<interceptor-ref name="modelDriven" />
				<interceptor-ref name="fileUpload" />
				<interceptor-ref name="staticParams" />
				<interceptor-ref name="params" />
				<interceptor-ref name="conversionError" />
				<interceptor-ref name="validation" />
				<interceptor-ref name="workflow" />
			</interceptor-stack>
			 
		</interceptors>  	  	
	  	
	  	<!-- mudando o default stack para nosso stack seguro -->
	  	<default-interceptor-ref name="defaultLoginStack" />
	  	 
	  	 
		<!-- 
			*************************************************************
			** Action handler generico									*
			************************************************************* 
			*	eg: usuario/list										* 
			*			-> UsuarioAction.list();						*
			*			-> /pages/usuario/list.jsp						*
			*************************************************************  
		-->
		<action name="*/*" class="{1}Action" method="{2}">
			<interceptor-ref name="defaultLoginStack" />		
            <result>/pages/{1}/{2}.jsp</result>
        </action>			 
    </package>
 
</struts>
 
 
 
ApplicationContext.xml (only the needed part):
 
	<!-- servicos -->
 	<bean id="usuarioService" class="br.com.autenticis.renacon.ejb.UsuarioBean" />
    
    <!-- actions que necessitam de servicos injetados -->
    <bean id="usuarioAction" scope="prototype" class="br.com.autenticis.renacon.actions.UsuarioAction">
        <constructor-arg ref="usuarioService" />
    </bean>
 
    <bean id="loginInterceptor" scope="prototype" class="br.com.autenticis.renacon.actions.interceptors.LoginInterceptor">
        <constructor-arg ref="usuarioService" />
    </bean>

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now