How to avoid ;jsessionid=XXX on the first call to a page? it works if first page is jsp

前端 未结 3 1628
情书的邮戳
情书的邮戳 2020-12-14 19:41

I have an application which uses the welcome-page index.jsp with an the contents of the iframe is a jsf page. If I access index.js

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 20:24

    Based on Christopher Schultz recommendation I tried this and it works.

        package com.rama.test.jsessionfilter
    
        public class JsessionIdAvoiderFilter implements Filter {
    
            protected static final Logger LOGGER = LogManager.getLogger(JsessionIdAvoiderFilter.class);
    
            public void doFilter(ServletRequest req, ServletResponse res,
                    FilterChain chain) throws IOException, ServletException {
    
                if (!(req instanceof HttpServletRequest)) {
                    chain.doFilter(req, res);
                    return;
                }
    
                HttpServletRequest request = (HttpServletRequest) req;
                HttpServletResponse response = (HttpServletResponse) res;
    
            // Redirect requests with JSESSIONID in URL to clean old links
            /* If you really want clean up some old links which have Jsession id bookmarked clean it. If its new app 
                this  below check is not required. */
                if (request.isRequestedSessionIdFromURL()) {
                    String url = request.getRequestURL().append(request.getQueryString() != null ? "?"
                                    + request.getQueryString() : "").toString();
                    response.setHeader("Location", url);
                    response.sendError(HttpServletResponse.SC_MOVED_PERMANENTLY);
                    LOGGER.info(" Found url with jsession id in it:"+ request.getRequestURL() +": url="+url);
                    return;
                }
    
                // Prevent rendering of JSESSIONID in URLs for all outgoing links
                HttpServletResponseWrapper wrappedResponse = new HttpServletResponseWrapper(
                        response) {
                    @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;
                    }
                };
                chain.doFilter(req, wrappedResponse);
    
            }
    
            public void destroy() {
            }
    
            public void init(FilterConfig arg0) throws ServletException {
            }
        }
    

    and the following entry in web.xml

     
            JsessionId Filter 
            jsessionIdAvoiderFilter 
            com.rama.test.jsessionfilter.JsessionIdAvoiderFilter 
         
         
            jsessionIdAvoiderFilter
            /*
            REQUEST
        
    

    Works great !!!.

提交回复
热议问题