2011-02-10 57 views
2

當我的用戶嘗試做一個動作我們的網站後,他們的會話已過期(例如,如果他們離開自己的瀏覽器中打開),服務器與HTTP狀態405響應,因爲用戶不再登錄。如何識別GWT內的http錯誤代碼?

發生這種情況時,我想將用戶重定向到登錄屏幕。

我怎麼能認識時405錯誤代碼中的GWT返回,所以我可以然後重定向用戶?

感謝

編輯: 下面是我使用的過濾器:

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內容。

+0

? – LINEMAN78 2011-02-10 21:27:37

+0

我們正在使用GWT-RPC – Cuga 2011-02-10 21:32:39

+0

這種過濾方法只會在會話過期時重定向您的rpc呼叫,您需要將405返回給客戶端。如果你在過濾器中重定向,它會得到一個200,但端點將會改變,gwt不會能夠解析數據。 – LINEMAN78 2011-02-12 22:20:25

回答

3

我目前在GWT應用程序,確實非常相似,你問什麼工作。然而,我們處理的標準javax.servlet.Filter的子類,這些重定向,我們在我們的web.xml中定義按通常和它映射到/ *所以它捕獲所有請求(我們使用此過濾器用於很多其他原因也一樣) 。

我不知道你是否還使用過濾器或者甚至只是某種在你的GWT應用程序捕獲所有的Servlet,但如果你是那麼解決您的問題是很容易的。您將擁有HttpServletRequest的句柄,並且您可以看到getSession(false)方法是否返回null,那麼您知道發送請求的用戶不再有會話。然後你可以做一個標準的response.sendRedirect(...)到你的登錄頁面。

1

在你RequestCallback實現,你可以檢查response.getStatusCode()。我們在後端有Spring Security,如果它們未通過身份驗證,則會將用戶強制登錄到基於表單的登錄頁面。身份驗證後,它會將其重定向回去。

例如:

@Override 
public void onResponseReceived(Request request, Response response) { 
    if (SC_UNAUTHORIZED == response.getStatusCode()) { 
    Window.Location.reload(); 
    } 
} 
2

對於GWT-RPC,你會得到一個StatusCodeException每當服務器返回什麼,但一個200響應代碼。可以從異常中檢索狀態代碼,並且可以使用Window.Location.assign(url)通過GWT進行重定向。對於RequestBuilder,狀態碼cqan可以通過response.getStatusCode()來訪問。

編輯:這裏是代碼重定向如果GWT的窗口不是根窗口。

private native void redirect(String url)/*-{ 
    var win = $wnd; 
    while (win.parent != null && win.parent != win) 
    win = win.parent; 
    win.location = url; 
}-*/; 
0

使用try/catch編碼器sakes!

AsyncCallback<String> callback = new AsyncCallback<String>() { 
     public void onFailure(Throwable caught) { 
      try { 
       throw caught; 
      } catch (StatusCodeException statusCodeException) { 
       Util.handleException(statusCodeException); 
      } catch (Throwable exception) { 
       Util.handleException(exception); 
      } 
     } 

     public void onSuccess(String result) { 
      Window.alert("Result : " + result); 
     } 
    }; 

然後處理您的結果

final static public void handleException(StatusCodeException exception) { 
     String errorMessage = exception.toString() + 
           "\n CODE: " + exception.getStatusCode() + 
           "\n MESSAGE: " + exception.getEncodedResponse(); 
     Window.alert("ERROR: " + errorMessage); 
    }  
final static public void handleException(Throwable exception) { 
     String errorMessage = exception.toString() + "\n"; 
     for (StackTraceElement trace : exception.getStackTrace()) { 
      errorMessage += trace.getClassName() + " :: " + 
          trace.getMethodName() + " :: " + 
          trace.getLineNumber() + "\n"; 
     } 
     Window.alert("ERROR: " + errorMessage); 
    } 

記住,超載是你的朋友!不要忘記,如果你不詳細編譯你的代碼或漂亮,你會得到堆棧跟蹤垃圾。當然,除非有人想出了你如何使用GWT-RPC或RequestBuilder破解的JSStackEmulation類

http://code.google.com/p/google-web-toolkit/wiki/WebModeExceptions