2011-01-07 14 views
2

Ich habe eine Stripes-Framework-Frage.Stripes - Redirect, Session verfallen

Diese Umleitungsseite in der Annotationsmethode vorher?

so etwas wie:

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

Antwort

1

Ich glaube, Sie versuchen, etwas zu tun:

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 
    } 
    } 
} 

Aber eine vollständige Sicherheitslösung wäre, die Stripes Security Interceptor zu implementieren.

0

Hm. Das ist nicht nett.

Vervielfältigungscode in allen Methoden.

Also, ich gehe lesen Stripes Security Interceptor.

Danke.

+0

Sie können natürlich auch eigene Abfangjäger schreiben: http://www.stripesframework.org/display/stripes/Intercept+Execution (es ist sehr einfach!) – Kdeveloper

0

Ich denke, Ihr Problem besteht darin, einen Benutzer auf Anmeldeseite umleiten, wenn sie nicht angemeldet sind. Verwenden @before auf jedem actionBean ist keine gute Idee. Um dies zu erreichen, können Sie Ihren eigenen Interceptor erstellen, indem Sie SpringInterceptorSupport erweitern.

@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; 
    } 

}