2017-10-11 83 views
1

我试图创建一个Wicket 7.8.0应用程序,一切工作正常,除了页面重定向到登录前访问原来的页面。重定向到原来的页面不工作

每当我试图访问受保护页面没有被登录,我被正确地重定向到SignIn页面,但是一旦我登录了,我就会被重定向到主页而不是原始页面。

这里是我的应用程序类:

public class MyApplication extends AuthenticatedWebApplication { 

    ... 

    @Override 
    public void init() { 
     super.init(); 

     MetaDataRoleAuthorizationStrategy.authorize(HomePage.class, "TEST_ROLE"); 
     MetaDataRoleAuthorizationStrategy.authorize(SecuredPage.class, "TEST_ROLE"); 

     this.mountPage("signin", SignInPage.class); 
     this.mountPage("homepage", HomePage.class); 
     this.mountPage("secured/secured", SecuredPage.class); 
     //this page is secured with annotations 
     this.mountPage("secured/another", AnotherSecuredPage.class); 

     this.getRequestCycleSettings().setGatherExtendedBrowserInfo(true); 
    } 
} 

为了登录,我使用了一个非常简化的登录页面:当我在已登录

public class SignInPage extends WebPage { 

    private String username; 
    private String password; 

    private static final long serialVersionUID = 8096706227164750788L; 

    public SignInPage() { 
     this.add(new FeedbackPanel("feedback")); 
     final Form<SignInPage> form = new Form<>("form"); 
     form.add(new TextField<>("username", new PropertyModel<String>(this, "username"))); 
     form.add(new PasswordTextField("password", new PropertyModel<String>(this, "password"))); 
     form.add(new SubmitLink("submit") { 

      private static final long serialVersionUID = 6057698894229534492L; 

      @Override 
      public void onSubmit() { 
       final Session session = SignInPage.this.getSession(); 
       if(session.signIn(SignInPage.this.username, SignInPage.this.password)) { 
        this.continueToOriginalDestination(); 
        setResponsePage(getApplication().getHomePage()); 
       } 
       else { 
        SignInPage.this.error("Bad username/password combo!"); 
       } 
      } 

     }); 
     final WebClientInfo clientInfo = (WebClientInfo) this.getSession().getClientInfo(); 
     this.add(new Label("userAgent", clientInfo.getUserAgent())); 
     this.add(form); 
    } 
} 

在至少一次应用程序虽然,如果我再次注销,重定向到原始页面每次重新登录时都有效。

我在做什么错了?

+0

更好地看看wicket-examples,你不需要为这个页面设计一个'母类':https://github.com/apache/wicket/blob/master/wicket-auth-roles/src /main/java/org/apache/wicket/authroles/authentication/AuthenticatedWebApplication.java – svenmeier

+0

的确,我没有注意到这一点!这就是定义onConfigure()方法首先不被调用的原因。 –

回答

1

进一步调试后,我发现了这个问题。

在我的应用程序的init()中,我收集的浏览器信息是this.getRequestCycleSettings().setGatherExtendedBrowserInfo(true);。在SignInPage上,我打电话(WebClientInfo) this.getSession().getClientInfo()。这会导致Wicket重定向到一个中间页面,该页面将收集浏览器信息,并在会话尚未初始化时将其放入会话中的第一次呼叫登录页面。

由于这个中介重定向,原来的页面url会丢失。它看起来像Wicket中的一个bug。

我发现解决此问题的唯一方法是通过简单地直接从请求检索所述原始用户代理头更换WebClientInfo对象,和手动过程中它:

  1. 从删除this.getRequestCycleSettings().setGatherExtendedBrowserInfo(true);应用程序init。
  2. 在SignInPage类,有

    final WebRequest webRequest = ((WebRequest)this.getRequest()); 
    this.add(new Label("userAgent", webRequest.getHeader("User-Agent"))); 
    

现在更换

final WebClientInfo clientInfo = (WebClientInfo) this.getSession().getClientInfo(); 
this.add(new Label("userAgent", clientInfo.getUserAgent())); 

,没有更多的intermidiary重定向,并且重定向到原来的页面可以正常工作。