隨筆 - 170  文章 - 536  trackbacks - 0
          <2007年9月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          常用鏈接

          我參與的團隊

          隨筆分類(103)

          搜索

          •  

          積分與排名

          • 積分 - 414960
          • 排名 - 135

          最新評論

          閱讀排行榜

          Java Web 應用似乎總有這樣的情況,有事沒事總是要在 URL 后面加上個 jsessionid,而且似乎不能使用配置的方式直接禁用 URL 傳遞 sessionid,這樣,就比較容易造成安全性的問題,或者在瀏覽器地址欄里留下一堆很不好看的地址,在 Struts2 中,使用了 url 標簽的所有鏈接,甚至 CSS, JS 這樣的東西,都會加上 jsessionid,如何去禁用呢,搜索國內的相關文章,無功而返,詢問我們過去的架構師,也沒有做過,只好想辦法去找國外的網站,找到了這樣的一篇文章。

          http://randomcoder.com/articles/jsessionid-considered-harmful

          通過加入 Filter 的方式過濾掉 URL 中包含的 jsessionid,再重新包裝 Response 返回給瀏覽器。

          因為沒有太多東西,就不多解釋了,大家拿了用就可以了。

          import javax.servlet.*;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import javax.servlet.http.HttpServletResponseWrapper;
          import javax.servlet.http.HttpSession;
          import java.io.IOException;

          /**
           * Servlet filter which disables URL-encoded session identifiers.
           * <p/>
           * <pre>
           * Copyright (c) 2006, Craig Condit. All rights reserved.
           * <p/>
           * Redistribution and use in source and binary forms, with or without
           * modification, are permitted provided that the following conditions are met:
           * <p/>
           *   * Redistributions of source code must retain the above copyright notice,
           *     this list of conditions and the following disclaimer.
           *   * Redistributions in binary form must reproduce the above copyright notice,
           *     this list of conditions and the following disclaimer in the documentation
           *     and/or other materials provided with the distribution.
           * <p/>
           * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
           * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
           * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
           * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
           * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
           * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
           * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
           * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
           * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
           * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
           * POSSIBILITY OF SUCH DAMAGE.
           * </pre>
           
          */
          @SuppressWarnings(
          "deprecation")
          public class DisableUrlSessionFilter implements Filter {

              
          /**
               * Filters requests to disable URL-based session identifiers.
               
          */
              
          public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
                  
          // skip non-http requests
                  if (!(request instanceof HttpServletRequest)) {
                      chain.doFilter(request, response);
                      
          return;
                  }

                  HttpServletRequest httpRequest 
          = (HttpServletRequest) request;
                  HttpServletResponse httpResponse 
          = (HttpServletResponse) response;

                  
          // clear session if session id in URL
                  if (httpRequest.isRequestedSessionIdFromURL()) {
                      HttpSession session 
          = httpRequest.getSession();
                      
          if (session != null) session.invalidate();
                  }

                  
          // wrap response to remove URL encoding
                  HttpServletResponseWrapper wrappedResponse = new HttpServletResponseWrapper(httpResponse) {
                      @Override
                      
          public String encodeRedirectUrl(String url) {
                          
          return url;
                      }

                      @Override
                      
          public String encodeRedirectURL(String url) {
                          
          return url;
                      }

                      @Override
                      
          public String encodeUrl(String url) {
                          
          return url;
                      }

                      @Override
                      
          public String encodeURL(String url) {
                          
          return url;
                      }
                  };

                  
          // process next request in chain
                  chain.doFilter(request, wrappedResponse);
              }

              
          /**
               * Unused.
               
          */
              
          public void init(FilterConfig config) throws ServletException {
              }

              
          /**
               * Unused.
               
          */
              
          public void destroy() {
              }

          -------------------------------------------------------------------------------------------------
          順便做個小廣告 鏈客中國 www.linkedcast.cn 上線運行,歡迎廣大 Blogger 使用

          鏈客中國,寫文章也可以賺錢哦
          posted on 2007-09-08 20:13 steady 閱讀(5688) 評論(4)  編輯  收藏 所屬分類: Java

          FeedBack:
          # re: 禁用JavaWeb應用中URL上包含的jsessionid 2007-09-10 13:41 編程、設計是職業更是愛好
          ha, I hate struts tag. overrun jsp tags will make things worse and worse.  回復  更多評論
            
          # re: 禁用JavaWeb應用中URL上包含的jsessionid 2008-05-23 16:25 學習
          為什么我按照你的寫法做了,頁面卻出來亂碼呢?
          不過sessionid是禁用成功了
          請賜教  回復  更多評論
            
          # re: 禁用JavaWeb應用中URL上包含的jsessionid 2008-12-13 00:22 好風
          @學習
          類里添加這個常量
          private static String encoding = "GB2312";
          就不亂碼了  回復  更多評論
            
          # re: 禁用JavaWeb應用中URL上包含的jsessionid 2009-04-09 16:11 putin
          好文章 謝謝你了  回復  更多評論
            
          主站蜘蛛池模板: 锡林浩特市| 江津市| 东乡| 田阳县| 双牌县| 鹿邑县| 西贡区| 襄汾县| 遂昌县| 邵东县| 巫山县| 瓮安县| 玛纳斯县| 连云港市| 呼玛县| 靖州| 大港区| 门源| 南漳县| 南澳县| 林甸县| 新巴尔虎左旗| 海林市| 大石桥市| 琼中| 保靖县| 邯郸县| 江油市| 晋城| 台中市| 鄱阳县| 嘉黎县| 繁峙县| 栾川县| 安岳县| 常山县| 廉江市| 恩平市| 古蔺县| 全州县| 怀集县|