I'm running into trouble making an interceptor blocking a user acessing any jsp by verifying that a boolean variable is true or false; that variable is in a bean (heyBean), that is previusly set with an action method in a session (the action implements session aware). If true, the user can proceed with the action; if not, the user is redirected to the login page. Obviously, login page must NOT be protected by this interceptor. The problem is, the interceptor is not being called when I call a protected action before logging in.
Here is my heyBean:
package hey.model;
import java.util.ArrayList;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import rmiserver.RMIServerInterface;
public class HeyBean {
private RMIServerInterface server;
private String username; // username and password supplied by the user
private String password;
private boolean isAuthenticated;
public HeyBean() {
try {
server = (RMIServerInterface) Naming.lookup("server");
} catch(NotBoundException|MalformedURLException|RemoteException e) {
e.printStackTrace(); // what happens *after* we reach this line?
}
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isIsAuthenticated() {
return isAuthenticated;
}
public void setIsAuthenticated(boolean isAuthenticated) {
this.isAuthenticated = isAuthenticated;
}
public boolean getUserMatchesPassword() throws RemoteException {
return server.userMatchesPassword(this.username, this.password);
}
public ArrayList<String> getAllUsers() throws RemoteException {
return server.getAllUsers(); // are you going to throw all exceptions?
}
public void sayHey(String whoSaidHey, String toWhoSaidHey) throws RemoteException {
server.markAsHeyed(whoSaidHey, toWhoSaidHey);
}
public ArrayList<String> getAllWhoSaidHey() throws RemoteException {
return server.getAllWhoSaidHey(); // are you going to throw all exceptions?
}
}
Here is my interceptor:
package hey.interceptor;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import hey.model.HeyBean;
public class LoginInterceptor implements Interceptor {
private static final long serialVersionUID = 189237412378L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Map<String, Object> session = invocation.getInvocationContext().getSession();
// this method intercepts the execution of the action and we get access
// to the session, to the action, and to the context of this invocation
HeyBean hB = (HeyBean) session.get("heyBean");
if(hB != null && hB.isIsAuthenticated()) {
System.out.println("PASSOU!");
return invocation.invoke();
}
else {
System.out.println("NAO PASSOU!");
return Action.LOGIN;
}
}
@Override
public void init() { }
@Override
public void destroy() { }
}
Here is my struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- The core configuration file for the framework is the default (struts.xml) file
and should reside on the classpath of the webapp (generally /WEB-INF/classes). -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- devMode equals debug information and reload everything for every request -->
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />
<package name="hey" extends="struts-default">
<!-- interceptor -->
<interceptors>
<interceptor name="loginInterceptor" class="hey.interceptor.LoginInterceptor" />
<interceptor-stack name="loginStack">
<interceptor-ref name="loginInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="loginStack" />
<default-action-ref name="index" />
<global-results>
<result name="error">/error.jsp</result>
<result name="login">/index.jsp</result>
</global-results>
<!-- all exceptions not caught by the application will lead to error.jsp -->
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error" />
</global-exception-mappings>
<!-- 'index' action leads to the view provided by index.jsp -->
<action name="index">
<result>/index.jsp</result>
</action>
<!-- 'login' action calls 'execute' or 'logout' in 'LoginAction' -->
<action name="login" class="hey.action.LoginAction" method="execute">
<interceptor-ref name="defaultStack" />
<result name="success">/hey.jsp</result>
<result name="input">/index.jsp</result>
</action>
<action name="logout" class="hey.action.LogoutAction" method="execute">
<result name="success">/index.jsp</result>
</action>
<action name="sayHey" class="hey.action.SayHeyAction" method="execute">
<result name="success">/hey.jsp</result>
</action>
</package>
</struts>
I've just solved it! It turns out I was doing a getHeyBean() in the action code, what would construct a new instance of HeyBean if it didn't previously existed. My bad :(. Thank you all for your cooperation.