2014-11-25 150 views
1

我遇到了麻烦,通过验证布尔变量是true还是false,使拦截器阻止用户访问任何jsp;该变量位于bean(heyBean)中,这是以前在会话中使用动作方法设置的(动作会实现会话感知)。如果属实,用户可以继续操作;如果没有,用户将重定向到登录页面。显然,登录页面不能被拦截器保护。问题是,拦截器,当我在登录前调用一个受保护的动作不会被调用struts2拦截器的问题

这是我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? 
    } 
} 

这里是我的拦截器:

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() { } 
} 

这里我的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> 
+0

你确定它不通过拦截器?或者它只是不工作吗?似乎很好的事实,你应该以另一种方式获得会议 – 2014-11-25 09:38:25

+0

它是不是问题,只有三项保护措施。在大多数情况下,您不受保护。 – 2014-11-25 09:39:26

回答

0

我刚刚解决了它!事实证明,我在动作代码中做了一个getHeyBean(),如果它之前不存在,将构造一个HeyBean的新实例。我的坏:(。谢谢大家的合作

+0

接受您的答案然后:) – Pravin 2014-11-25 17:33:55