2

我有一个条纹框架问题。

这个重定向页面在注解方法之前?

就像是:

@Before
public void test()
{
  String login=(String)context.getRequest().getSession().getAttribute("login");
  if (login==null)
  {
    Redirect...(LoginActionBean.class);  // ??????
    exit....();                                    // ??????
  }
}
4

3 回答 3

1

我认为你尝试做这样的事情:

public class MyPageActionBean implements ActionBean {
  private ActionBeanContext context;

  public ActionBeanContext getContext() {
    return context;
  }

  public void setContext(ActionBeanContext context) {
    this.context = context;
  }

  @DefaultHandler
  public Resolution view() {
    String login =
      (String)context.getRequest().getSession().getAttribute("login");
    if (login==null) {
      return new RedirectResolution(LoginActionBean.class);
    } else {
      // do you're normal stuff here
    }
  }
}

但更完整的安全解决方案是实施Stripes Security Interceptor

于 2011-01-08T00:37:03.983 回答
0

嗯。这不好。

所有方法中的重复代码。


public Resolution view1()
{
  String login=....
  if () {...}
  else  {...}
}

public Resolution view2()
{
  String login=....
  if () {...}
  else  {...}
}
public Resolution view3()
{
  String login=....
  if () {...}
  else  {...}
}

所以,我去阅读 Stripes Security Interceptor。

谢谢。

于 2011-01-10T16:25:15.727 回答
0

我认为您的问题是在用户未登录时在登录页面上重定向用户。在每个 actionBean 上使用@before不是一个好主意。为此,您可以通过扩展 SpringInterceptorSupport 来制作自己的拦截器。

@Intercepts(LifecycleStage.ActionBeanResolution)
public class MyInterceptor extends SpringInterceptorSupport {
private static final List<Class<? extends ActionBean>> ALLOW = Arrays.asList(LoginActionBean.class, anyOtherActionBeanAllowedWithoutLogin.class);

@Override
  @SuppressWarnings({ "rawtypes" })
  public Resolution intercept(ExecutionContext execContext) throws Exception {
    Resolution resolution = execContext.proceed();
    ActionBean actionBean = execContext.getActionBean();
    Class<? extends ActionBean> destinationclass = actionBean.getClass();
    if (!ALLOW.contains(destinationclass) && !isSessionExist()) {
      resolution = new RedirectResolution(LoginActionBean.class);
    }
    return resolution;

  }

  private boolean isSessionExist() {
    String login = (String)context.getRequest().getSession().getAttribute("login");
    return login != null;
  }

}
于 2016-09-26T17:54:21.010 回答