2012-05-20 39 views
0

我在攔截和更改請求url以將其鏈接到正確的URL時遇到問題。 我正在使用條紋框架,並希望將用戶重定向到正確的子域。 例如。如果用戶屬於abc.sitename.com並且請求來自xyz.sitename.com,那麼他應該被重定向到包含所有請求帖子的abc.sitename.com。 對於getparameter,我通過在生命週期解析執行之前簡單地獲取請求url和請求查詢並從定製攔截類重定向用戶來完成此操作。但是所有的post參數都會刷新,因爲它是重定向的。 另一種解決方案是正向解析,但它可以框架內採取用戶,我需要重寫它:攔截和更改請求url使用Stripes框架

resolution = new OnwardResolution<ForwardResolution>(reponseUrl) { 
        @Override 
        public void execute(HttpServletRequest request, 
          HttpServletResponse response) throws Exception { 
         request = ctx.getRequest(); 
         response = ctx.getResponse(); 
         String path = reponseUrl; //getUrl(request.getLocale()); 
         // Set event name as a request attribute 
         String oldEvent = (String) request.getAttribute(StripesConstants.REQ_ATTR_EVENT_NAME); 
         //request.setAttribute(StripesConstants.REQ_ATTR_EVENT_NAME, event); 
         log.info("check: {}", path); 
         // Revert event name to its original value 
         request.setAttribute(StripesConstants.REQ_ATTR_EVENT_NAME, oldEvent); 
         // Figure out if we're inside an include, and use an include instead of a forward 
         RequestDispatcher dispatcher = request.getRequestDispatcher(path); 
         //request.getRequestDispatcher(path).forward(request, response); 
         dispatcher.forward(request, response); 

        } 
       }; 

但它調用應用程序上下文路徑,而應該稱之爲完整的URL。 另一個解決方案,如果我使用重定向解析並設置includerequest參數true將執行該作業,但是將所有請求參數放入url中,這使得它不可接受。

請求url應該在解析executuion之前更改,因爲在它之後它將無用,我試過了。有什麼方法可以在HTTPServeletRequest中設置請求URL。

我也試圖在ActionBeanResolution期間攔截,這是做這種活動的最佳場所。我的主要問題是POST參數。我無法重定向(因爲它們被刷新,或者如果使用包括請求參數,它們在URL中可見)。在前後關係的前提下解決。如果有辦法將其轉發到新的URL,它將完成這項工作。

在此先感謝。

回答

0

@JB Nizet和@Pointy感謝您的時間和回覆。我想出瞭解決問題的辦法。 @Pointy我正在改變需要重定向的用戶的子域,但我也不想在該請求中丟失發佈參數。我希望現在它是有道理的。

這裏我做了什麼(在步驟):

  • 首先我想,當我需要做的重定向和需要保存之後的參數。
  • 保存會話中的所有發佈數據(暫時)。
  • 如果我在會話中有任何臨時後值,我將它置於新請求中,並從會話中刪除後 值。

代碼: 對於設置之後的參數在會話:

resolution = new RedirectResolution(reponseUrl); 
      Iterator<Map.Entry<String, String[]>> requestParamter = ctx.getRequest().getParameterMap().entrySet().iterator(); 
      List<IdName> requestParams = new ArrayList<IdName>(); 
      while(requestParamter.hasNext()){ 
       IdName param = new IdName(); 
       Map.Entry<String, String[]> entry = requestParamter.next(); 
       param.setName(entry.getKey()); 
       param.setParameterValues(entry.getValue()); 
       log.trace("intercept - Adding Key: {} with value: {} to redirect request parameter.", entry.getKey(), entry.toString()); 
       requestParams.add(param); 
      } 
      ctx.setRedirectRequestParameter(requestParams); 

爲了得到回:

if(ctx.getRedirectRequestParameter() != null && !ctx.getRedirectRequestParameter().isEmpty()){ 
       //Removes parameter which are already in request from redirect request parameter. 
       Map<String, String[]> additionalParams = new TreeMap<String, String[]>(); 
       Map requestParameterMap = ctx.getRequest().getParameterMap(); 
       ListIterator<IdName> oldRequestParams = ctx.getRedirectRequestParameter().listIterator(); 
       while(oldRequestParams.hasNext()){ 
        IdName oldRequestParam = oldRequestParams.next(); 
        log.trace("Lopping requestparameter key: {} ", oldRequestParam.getName()); 
        if (!requestParameterMap.containsValue(oldRequestParam.getName())) { 
         additionalParams.put(oldRequestParam.getName(), oldRequestParam.getParameterValues()); 
        } 
       } 
       HttpServletRequest newRequest = new MyperksHttpServletRequestWrapper(ctx.getRequest(), additionalParams); 
       ctx.setRequest(newRequest); 
       ctx.setRedirectRequestParameter(null); 
      } 

我還創建了一個包裝類增加了新的參數要求其創建一個新的請求封裝器,它會將附加參數合併到請求對象中,而不會從原始請求中過早讀取參數。有關此課程的詳細信息,請點擊鏈接:PrettyFaceRequestWrapper

2

使用POST不可能重定向。故事結局。

只要確保一個ABC用戶永遠不會看到一個鏈接或表單指向XYZ

重定向GET請求(如果用戶直接在地址欄中輸入xyz URL),並重定向到POST請求的某個錯誤頁面或主頁,因爲它永遠不會發生。

+0

好的..但我們可以轉發請求與帖子。我只是無法在Stripes框架中正確使用RequestDispatcher轉發方法。當我設置「http://xyz.sitename.com」時,它會在製作「/http:/xyz.sitename.com」時附加「/」。你有沒有遇到這樣的問題..並感謝您的回覆.. –

+0

轉發只能轉發到同一個應用程序的資源,在同一臺服務器上。這是一個內部應用的事情。對於瀏覽器來說它是完全透明的,它將保持在同一個URL中。它與重定向無關。 –

+0

有沒有什麼辦法可以在HTTPServletRequest中設置請求URL ..我弄清楚了..如果這可能會解決這個問題.. –