如何从 GWT 中识别 http 错误代码?
当我的用户在会话过期后尝试对我们的网站执行操作时(例如,如果他们保持浏览器打开状态),服务器会以 HTTP 状态 405 进行响应,因为用户不再登录。
当发生这种情况时,我希望将用户重定向到登录屏幕。
如何识别 GWT 中何时返回 405 错误代码,以便我可以重定向用户?
谢谢
编辑: 这是我正在使用的过滤器:
public class AuthenticationFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
if (req instanceof HttpServletRequest) {
boolean isLoggedIn = CustomSecurity.login((HttpServletRequest)req);
if (isLoggedIn) {
// TODO: How to redirect the user here???
}
}
chain.doFilter(req, res);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
web.xml 内容:
<filter>
<filter-name>Authentication Filter</filter-name>
<filter-class>com.company.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Authentication Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如何重定向用户?另外,有没有办法强制整个浏览器重定向?由于这进入了一个小部件,我认为 Window.Location.assign('url') 只会重定向小部件的 HTML 内容。
When my users try to do an action our website after their session has expired (for instance, if they left their browser open), the server responds with a HTTP Status 405 because the user is no longer logged in.
When this happens, I want to redirect the user to the login screen.
How can I recognize when a 405 error code is returned within GWT so I can then redirect the users?
Thanks
Edit:
Here's the filter I'm using:
public class AuthenticationFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
if (req instanceof HttpServletRequest) {
boolean isLoggedIn = CustomSecurity.login((HttpServletRequest)req);
if (isLoggedIn) {
// TODO: How to redirect the user here???
}
}
chain.doFilter(req, res);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
web.xml content:
<filter>
<filter-name>Authentication Filter</filter-name>
<filter-class>com.company.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Authentication Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
How can I make that redirect the user? Also, is there a way to force the whole browser to redirect? Since this goes into a widget, I think a Window.Location.assign('url') will only redirect the widget's HTML content.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我目前正在开发一个 GWT 应用程序,该应用程序的功能与您所要求的非常相似。然而,我们在标准 javax.servlet.Filter 子类中处理这些重定向,该子类是我们在 web.xml 中定义的,并且它映射到 /* 因此它捕获所有请求(我们也出于许多其他原因使用此过滤器) 。
我不知道您是否还在 GWT 应用程序中使用 Filter,或者甚至只是某种包罗万象的 Servlet,但如果您使用了,那么问题的解决方案就非常简单。您将拥有 HttpServletRequest 的句柄,并且可以查看 getSession(false) 方法是否返回 null,那么您就知道发送请求的用户不再拥有会话。然后您可以对登录页面执行标准的 response.sendRedirect(...) 。
I currently work on a GWT application that does something very similar to what you are asking. However, we handle these redirects in a standard javax.servlet.Filter subclass that we define in our web.xml as per usual and it is mapped to /* so it catches all requests (we use this filter for many other reasons as well).
I don't know if you are also using a Filter or perhaps even just some sort of catch-all Servlet in your GWT app, but if you are then the solution to your issue is quite easy. You will have a handle to the HttpServletRequest and you can see if the getSession(false) method returns null then you know that the user who sent the request no longer has a session. Then you can just do a standard response.sendRedirect(...) to your login page.
对于 GWT-RPC,您将获得 StatusCodeException。可以从异常中检索状态代码,并且可以使用 Window.Location.assign(url) 通过 GWT 进行重定向。对于RequestBuilder,可以通过response.getStatusCode() 访问状态代码。
编辑:如果 GWT 的窗口不是根窗口,这里是重定向的代码。
For GWT-RPC you will get a StatusCodeException whenever the server returns anything but a 200 response code. The status code can be retrieved from the exception and you can redirect via GWT by using Window.Location.assign(url). For RequestBuilder the status code cqan be accessed via response.getStatusCode().
EDIT: Here is code to redirect if GWT's window isn't that root window.
在您的 RequestCallback 实现,您可以检查
response.getStatusCode()
。我们在后端有 Spring Security,它被连接起来以强制用户如果未经过身份验证,则基于表单的登录页面。身份验证后,它将它们重定向回来。例如:
In your RequestCallback implementation, you can check
response.getStatusCode()
. We have Spring Security on the backend, which is wired up to force the user to a form based login page if they are not authenticated. Upon authentication, it redirects them back.For example:
为了编码人员的缘故,请使用 try / catch!
然后处理您的结果,
请记住,超载是您的朋友!不要忘记,如果您不详细或不漂亮地编译代码,您将得到堆栈跟踪的垃圾。当然,除非有人知道如何破解 JSStackEmulation 类
http://code .google.com/p/google-web-toolkit/wiki/WebModeExceptions
use a try / catch for coders sakes!
then to process your results
remember that overloading is your friend! Dont forgot that if you do not compile your code in detailed or pretty you will get garbage for the stack trace. Unless of course someone figured out how to hack the JSStackEmulation class
http://code.google.com/p/google-web-toolkit/wiki/WebModeExceptions