锘??xml version="1.0" encoding="utf-8" standalone="yes"?>91麻豆精品国产91久久久久推荐资源,欧美亚洲国产激情,gogogo影视剧免费观看在线观看http://www.aygfsteel.com/clin8888/category/45220.html璁板綍鐐規淮鐨勫湴鏂?/description>zh-cnFri, 04 Jun 2010 09:45:47 GMTFri, 04 Jun 2010 09:45:47 GMT60tomcat 浜岀駭鍩熷悕 鍏變韓session 鏂規硶http://www.aygfsteel.com/clin8888/archive/2010/06/04/322775.htmlworld_eyesworld_eyesFri, 04 Jun 2010 09:17:00 GMThttp://www.aygfsteel.com/clin8888/archive/2010/06/04/322775.htmlhttp://www.aygfsteel.com/clin8888/comments/322775.htmlhttp://www.aygfsteel.com/clin8888/archive/2010/06/04/322775.html#Feedback0http://www.aygfsteel.com/clin8888/comments/commentRss/322775.htmlhttp://www.aygfsteel.com/clin8888/services/trackbacks/322775.html     Tomcat涓嬶紝涓嶅悓鐨勪簩綰у煙鍚嶏紝Session榛樿鏄笉鍏變韓鐨勶紝鍥犱負Cookie鍚嶇О涓篔SESSIONID鐨凜ookie鏍瑰煙鏄粯璁ゆ槸娌¤緗殑錛岃闂? 涓嶅悓鐨勪簩綰у煙鍚嶏紝鍏禖ookie灝遍噸鏂扮敓鎴愶紝鑰宻ession灝辨槸鏍規嵁榪欎釜Cookie鏉ョ敓鎴愮殑錛屾墍浠ュ湪涓嶅悓鐨勪簩綰у煙鍚嶄笅鐢熸垚鐨凷ession涔熶笉涓鏍楓? 鎵懼埌浜嗗叾鍘熷洜錛屽氨鍙牴鎹繖涓師鍥犲Tomcat鍦ㄧ敓鎴怱ession鏃惰繘琛岀浉搴旂殑淇敼(娉細鏈枃閽堝Tomcat 6.0)銆?

     鍗曚釜web欏圭洰榪愯鍦╰omcat涓婁絾鏄嵈浣跨敤澶氫釜瀛愬煙鍚嶏紝濡傦細

 - site.com
 - www.site.com
 - sub1.site.com
 - sub2.site.com
 - etc.

 

榪欐牱浼氬鑷磗ession鐨勪笉鑳藉叡浜紝鍦ㄧ綉緇滀笂鏌ユ壘鐨勫茍鍗存渶蹇殑瑙e喅鍔炴硶銆?/p>

 

瑙e喅鍔炴硶錛?/p>

Usage:
 - compile CrossSubdomainSessionValve & put it in a .jar file
 - put that .jar file in $CATALINA_HOME/lib directory
 - include a <Valve className="org.three3s.valves.CrossSubdomainSessionValve"/> in
$CATALINA_HOME/conf/server.xml

package org.three3s.valves;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.catalina.*;
import org.apache.catalina.connector.*;
import org.apache.catalina.valves.*;
import org.apache.tomcat.util.buf.*;
import org.apache.tomcat.util.http.*;

/**
 * <p>
 * Replaces the domain of the session cookie generated by Tomcat with a domain
 * that allows that session cookie to be shared across subdomains. This valve
 * digs down into the response headers and replaces the Set-Cookie header for
 * the session cookie, instead of futilely trying to modify an existing Cookie
 * object like the example at 
http://www.esus.be/blog/?p=3. That approach does
 * not work (at least as of Tomcat 6.0.14) because the
 * <code>org.apache.catalina.connector.Response.addCookieInternal</code> method
 * renders the cookie into the Set-Cookie response header immediately, making
 * any subsequent modifying calls on the Cookie object ultimately pointless.
 * </p>
 * 
 * <p>
 * This results in a single, cross-subdomain session cookie on the client that
 * allows the session to be shared across all subdomains. However, see the
 * {
@link getCookieDomain(Request)} method for limits on the subdomains.
 * </p>
 * 
 * <p>
 * Note though, that this approach will fail if the response has already been
 * committed. Thus, this valve forces Tomcat to generate the session cookie and
 * then replaces it before invoking the next valve in the chain. Hopefully this
 * is early enough in the valve-processing chain that the response will not have
 * already been committed. You are advised to define this valve as early as
 * possible in server.xml to ensure that the response has not already been
 * committed when this valve is invoked.
 * </p>
 * 
 * <p>
 * We recommend that you define this valve in server.xml immediately after the
 * Catalina Engine as follows:
 * 
 * <pre>
 * &lt;Engine name=&quot;Catalina&quot;&gt;
 *     &lt;Valve
 * className=&quot;org.three3s.valves.CrossSubdomainSessionValve&quot;/&gt;
 * </pre>
 * 
 * </p>
 
*/
public class CrossSubdomainSessionValve extends ValveBase {
    
public CrossSubdomainSessionValve() {
        
super();
        info 
= "org.three3s.valves.CrossSubdomainSessionValve/1.0";
    }

    @Override
    
public void invoke(Request request, Response response) throws IOException, ServletException {
        
// this will cause Request.doGetSession to create the session cookie if
        
// necessary
        request.getSession(true);

        
// replace any Tomcat-generated session cookies with our own
        Cookie[] cookies = response.getCookies();
        
if (cookies != null) {
            
for (int i = 0; i < cookies.length; i++) {
                Cookie cookie 
= cookies[i];
                
// System.out.println("CrossSubdomainSessionValve: Cookie name is "
                
// + cookie.getName());
                if (Globals.SESSION_COOKIE_NAME.equals(cookie.getName())) replaceCookie(request, response, cookie);
            }
        }

        
// process the next valve
        getNext().invoke(request, response);
    }

    
/**
     * Replaces the value of the response header used to set the specified
     * cookie to a value with the cookie's domain set to the value returned by
     * <code>getCookieDomain(request)</code>
     * 
     * 
@param request
     * 
@param response
     * 
@param cookie
     *            cookie to be replaced.
     
*/
    
protected void replaceCookie(Request request, Response response, Cookie cookie) {
        
// copy the existing session cookie, but use a different domain
        Cookie newCookie = new Cookie(cookie.getName(), cookie.getValue());
        
// System.out.println("CrossSubdomainSessionValve: CookiePath is " +
        
// cookie.getPath());
        if (cookie.getPath() != null) newCookie.setPath(cookie.getPath());
        String domain 
= getCookieDomain(request);
        
if (!".piaoyoo.com".equals(domain)) domain = request.getServerName();
        
// System.out.println("CrossSubdomainSessionValve: CookieDomain is " +
        
// domain);
        newCookie.setDomain(domain);
        newCookie.setMaxAge(cookie.getMaxAge());
        newCookie.setVersion(cookie.getVersion());
        
// System.out.println("CrossSubdomainSessionValve: CookieComment is " +
        
// cookie.getComment());
        if (cookie.getComment() != null) newCookie.setComment(cookie.getComment());
        newCookie.setSecure(cookie.getSecure());

        
// if the response has already been committed, our replacement strategy
        
// will have no effect
        if (response.isCommitted()) System.out.println("Error CrossSubdomainSessionValve: response was already committed!");

        
// find the Set-Cookie header for the existing cookie and replace its
        
// value with new cookie
        MimeHeaders headers = response.getCoyoteResponse().getMimeHeaders();
        
for (int i = 0, size = headers.size(); i < size; i++) {
            
if (headers.getName(i).equals("Set-Cookie")) {
                MessageBytes value 
= headers.getValue(i);
                
if (value.indexOf(cookie.getName()) >= 0) {
                    StringBuffer buffer 
= new StringBuffer();
                    ServerCookie.appendCookieValue(buffer, newCookie.getVersion(), newCookie.getName(), newCookie.getValue(),
                            newCookie.getPath(), newCookie.getDomain(), newCookie.getComment(), newCookie.getMaxAge(), newCookie
                                    .getSecure());
                    
// System.out.println("CrossSubdomainSessionValve: old Set-Cookie value: "
                    
// + value.toString());
                    
// System.out.println("CrossSubdomainSessionValve: new Set-Cookie value: "
                    
// + buffer);
                    
// System.out.println("-----------------------------");
                    value.setString(buffer.toString());
                }
            }
        }
    }

    
/**
     * Returns the last two parts of the specified request's server name
     * preceded by a dot. Using this as the session cookie's domain allows the
     * session to be shared across subdomains. Note that this implies the
     * session can only be used with domains consisting of two or three parts,
     * according to the domain-matching rules specified in RFC 2109 and RFC
     * 2965.
     * 
     * <p>
     * Examples:
     * </p>
     * <ul>
     * <li>foo.com => .foo.com</li>
     * <li>www.foo.com => .foo.com</li>
     * <li>bar.foo.com => .foo.com</li>
     * <li>abc.bar.foo.com => .foo.com - this means cookie won't work on
     * abc.bar.foo.com!</li>
     * </ul>
     * 
     * 
@param request
     *            provides the server name used to create cookie domain.
     * 
@return the last two parts of the specified request's server name
     *         preceded by a dot.
     
*/
    
protected String getCookieDomain(Request request) {
        String cookieDomain 
= request.getServerName();
        String[] parts 
= cookieDomain.split("\\.");
        
if (parts.length >= 2) cookieDomain = parts[parts.length - 2+ "." + parts[parts.length - 1];
        
return "." + cookieDomain;
    }

    
public String toString() {
        
return ("CrossSubdomainSessionValve[container=" + container.getName() + ']');
    }
}





]]>
主站蜘蛛池模板: 阿克陶县| 大厂| 基隆市| 桃源县| 浠水县| 康保县| 河间市| 拉萨市| 湛江市| 深州市| 绥化市| 修武县| 江北区| 侯马市| 江城| 阿城市| 鄂伦春自治旗| 阿尔山市| 枣庄市| 洪泽县| 泊头市| 洛扎县| 祁阳县| 呼玛县| 新绛县| 禄丰县| 桐乡市| 称多县| 合川市| 黔南| 龙山县| 新竹市| 婺源县| 五峰| 漳州市| 垫江县| 大理市| 泾川县| 尚义县| 原平市| 商都县|