我有一个用 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 标题块以显示身份验证块,例如,“您是(未登录)/”,登录/注销。我想这应该可以解决问题。
- 如果这不能解决问题,请解释原因。
- 有没有更好(像拦截器一样优雅)的方法来做到这一点?
先感谢您。