重定向到原始页面不起作用
Redirect to original page not working
我正在尝试创建 Wicket 7.8.0 应用程序,一切正常,除了页面重定向到登录前访问的原始页面。
每当我尝试在未登录的情况下访问安全页面时,我都会被正确地重定向到登录页面,但是一旦我登录,我就会被重定向到主页而不是原始页面。
这是我的申请 class:
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);
}
}
虽然我在应用程序中至少登录过一次,但如果我再次注销,每次重新登录时重定向到原始页面都会起作用。
我做错了什么?
进一步调试后,我发现了问题。
在我的应用程序 init()
中,我正在使用 this.getRequestCycleSettings().setGatherExtendedBrowserInfo(true);
收集浏览器信息。在 SignInPage
上,我正在调用 (WebClientInfo) this.getSession().getClientInfo()
。这导致 Wicket 重定向到一个中间页面,该页面将收集浏览器信息并在 session 尚未初始化时第一次调用登录页面时将其放入 session 中。
作为中间重定向的结果,原始页面 url 丢失了。对我来说,它看起来像是 Wicket 中的一个错误。
我发现解决此问题的唯一方法是通过简单地检索原始 User-Agent header直接从请求中获取,手动处理:
- 从应用程序初始化中删除
this.getRequestCycleSettings().setGatherExtendedBrowserInfo(true);
。
在 SignInPage class 中,替换
final WebClientInfo clientInfo = (WebClientInfo) this.getSession().getClientInfo();
this.add(new Label("userAgent", clientInfo.getUserAgent()));
和
final WebRequest webRequest = ((WebRequest)this.getRequest());
this.add(new Label("userAgent", webRequest.getHeader("User-Agent")));
现在,不再有中间重定向,并且可以正确重定向到原始页面。
我正在尝试创建 Wicket 7.8.0 应用程序,一切正常,除了页面重定向到登录前访问的原始页面。
每当我尝试在未登录的情况下访问安全页面时,我都会被正确地重定向到登录页面,但是一旦我登录,我就会被重定向到主页而不是原始页面。
这是我的申请 class:
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);
}
}
虽然我在应用程序中至少登录过一次,但如果我再次注销,每次重新登录时重定向到原始页面都会起作用。
我做错了什么?
进一步调试后,我发现了问题。
在我的应用程序 init()
中,我正在使用 this.getRequestCycleSettings().setGatherExtendedBrowserInfo(true);
收集浏览器信息。在 SignInPage
上,我正在调用 (WebClientInfo) this.getSession().getClientInfo()
。这导致 Wicket 重定向到一个中间页面,该页面将收集浏览器信息并在 session 尚未初始化时第一次调用登录页面时将其放入 session 中。
作为中间重定向的结果,原始页面 url 丢失了。对我来说,它看起来像是 Wicket 中的一个错误。
我发现解决此问题的唯一方法是通过简单地检索原始 User-Agent header直接从请求中获取,手动处理:
- 从应用程序初始化中删除
this.getRequestCycleSettings().setGatherExtendedBrowserInfo(true);
。 在 SignInPage class 中,替换
final WebClientInfo clientInfo = (WebClientInfo) this.getSession().getClientInfo(); this.add(new Label("userAgent", clientInfo.getUserAgent()));
和
final WebRequest webRequest = ((WebRequest)this.getRequest()); this.add(new Label("userAgent", webRequest.getHeader("User-Agent")));
现在,不再有中间重定向,并且可以正确重定向到原始页面。