2

我有一个用 Struts 1 编写的遗留应用程序。我被要求添加的唯一功能是保护某些操作。目前任何用户都可以为所欲为。这个想法是允许所有用户看到数据,但阻止修改操作,即修改用户应该登录的数据。

我知道 Struts2 有拦截器,所以我可以将它们附加到所需的操作并在需要时将用户转发到登录页面。但是我怎样才能在 Struts 1 应用程序中做类似的事情呢?

我的第一个想法是创建自己的抽象 Action 类:

public class AuthenticatedAction {
    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form, 
        HttpServletRequest theRequest, 
        HttpServletResponse theResponse) {
            if (!logged) {
                // forward to log in form
            } else {
                doExecute(mapping, form, request, response);
            }
        }

public abstract ActionForward doExecute(
    ActionMapping mapping,
    ActionForm form, 
    HttpServletRequest theRequest, 
    HttpServletResponse theResponse);
}

然后更改所有需要身份验证的操作

extends Action

extends AuthenticatedAction 

然后添加登录表单、登录操作(执行身份验证并将此状态放入会话)并更改 JSP 标题块以显示身份验证块,例如,“您是(未登录)/”,登录/注销。我想这应该可以解决问题。

  1. 如果这不能解决问题,请解释原因。
  2. 有没有更好(像拦截器一样优雅)的方法来做到这一点?

先感谢您。

4

2 回答 2

2

我想您可以使用过滤器来满足您的要求。您可以在 web.xml 中声明一个过滤器,并提供一个应用它的 url 模式。

< filter>
< filter-name>filterName< /filter-name>
< filter-class>filter.class</filter-class>

< filter-mapping>
< filter-name>filterName< /filter-name>
< url-pattern>*.*< /url-pattern>
< /filter-mapping>

相关链接在这里

于 2010-01-25T18:41:21.277 回答
0

我认为好的方法是创建过滤器类在 web.xml 中创建条目,现在在您的过滤器中进行如下编码

public void doFilter(ServletRequest request, ServletResponse response,

   FilterChain chain) throws IOException, ServletException {
  System.out.println("coming from globester filtersss 2");
  HttpServletRequest requesth = (HttpServletRequest) request;
        HttpServletResponse responseh = (HttpServletResponse) response;
        HttpSession session = requesth.getSession();

        String ss=requesth.getParameter("cat");

        if(ss!=null && ss.equalsIgnoreCase("trues")){

        }else{
         responseh.sendRedirect("searchSanta.jsp?cat=trues");
        }
}
于 2010-12-24T21:49:22.633 回答