内容转自:http://blog.csdn.net/luka2008/article/details/38385703/,请直接看原文,不过这篇“原文”也是转的。。。

1,Tomcat下

代码:

import com.sun.javaws.Globals;

import javax.xml.soap.MimeHeaders;
import java.io.IOException; /**
* Created by liu.yuxiang on 2017/9/26.
*/
public class SessionCrossDomainValve extends ValveBase { public SessionCrossDomainValve() {
super();
info = "com.jinfuzi.SessionCrossDomainValve";
} public void invoke(Request request, Response response) throws IOException, ServletException {
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;
containerLog.debug("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);
} @SuppressWarnings("unchecked")
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());
if (cookie.getPath() != null)
newCookie.setPath(cookie.getPath());
newCookie.setDomain(getCookieDomain(request));
newCookie.setMaxAge(cookie.getMaxAge());
newCookie.setVersion(cookie.getVersion());
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())
containerLog.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()); //如果是tomcat6.020,这里需要多加一个true.
containerLog.debug("CrossSubdomainSessionValve: old Set-Cookie value: " + value.toString());
containerLog.debug("CrossSubdomainSessionValve: new Set-Cookie value: " + buffer);
value.setString(buffer.toString());
}
}
}
} 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() + ']');
}
}

这个类就是查看response要写入的cookie有没有符合规则,有就对cookie,domain进行变更,变为二级域名

将这个类打包成jar包,放进{catalina_home}/lib下,并在server.xml中注册: 
<Valve className="SessionCrossDomainValve"/>

2、改变获取Session的方式

public Session getSession(HttpServletRequest request, HttpServletResponse response){
HttpSession session = request.getSession(false);
if (session==null){
session = request.getSession(true);
String session_id = session.getId();
Cookie c = new Cookie("JSESSIONID",session_id);
c.setDomain(".vinceruan.info");
c.setPath("/");
response.addCookie();
}
}

Tomcat跨二级域配置的更多相关文章

  1. tomcat跨域请求

    tomcat A请求tomcat B中的数据(本人是在同一台机器上2个tomcat端口不同,在不同机器上有时间会测得) 博主用的是ajax请求 在A 请求B中数据,用下面的方法可以 在被请求的tomc ...

  2. Asp.Net Core跨域配置

    在没有设置跨域配置的时候,Ajax请求时会报以下错误 已拦截跨源请求:同源策略禁止读取位于 http://localhost:5000/Home/gettime 的远程资源.(原因:CORS 头缺少 ...

  3. Springboot统一跨域配置

    前言:跨域是什么? 要知道跨域的概念,我们先明确怎样算是同一个域: 同一个域指的是同一协议,同一ip,同一端口 如果这三同中有一者不同就产生了跨域. 在做前后端分离的项目中,通过ajax请求后台端口时 ...

  4. Asp.net跨域配置

    <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Contro ...

  5. asp.net (webapi) core 2.1 跨域配置

    原文:asp.net (webapi) core 2.1 跨域配置 官方文档 ➡️ https://docs.microsoft.com/zh-cn/aspnet/core/security/cors ...

  6. .net core api服务端跨域配置

    第1步:添加包引用(.net core 2.2 已自带此包,可跳过此步骤) Install-Package Microsoft.AspNetCore.Cors 第2步:在Startup.cs文件的Co ...

  7. nginx-springboot-vue前后端分离跨域配置

    nginx-springboot-vue前后端分离跨域配置 引言 接着上篇--简单的springboot-vue前后端分离登录Session拦截的demo,其中跨域是通过springboot后端全局设 ...

  8. Asp.net Core3.0 跨域配置

    原文:http://www.zilaohu.cn/Jie/Detail_Jie?ID=78840a04-55b8-4988-80b2-f964fd822d63 下面配置后:被拒绝的域请求后,可以进入方 ...

  9. 常见跨域解决方案以及Ocelot 跨域配置

    常见跨域解决方案以及Ocelot 跨域配置 Intro 我们在使用前后端分离的模式进行开发的时候,如果前端项目和api项目不是一个域名下往往会有跨域问题.今天来介绍一下我们在Ocelot网关配置的跨域 ...

随机推荐

  1. sql语句分组/排序/计算总数/连接等sql语句书写

    1.什么是表连接? 答:比如两张表,要获取的信息来自两张表,就需要通过外键的形式进行两张表的连接.最后产后组合信息. 表连接是通过join连接的.表连接说白了就是产生一个大表.表连接也都是用于查询上的 ...

  2. JAVA自定义注解SpringAOP

    原文:https://my.oschina.net/wangnian/blog/801348 前言:Annotation(注解)是JDK5.0及以后版本引入的,它的作用就是负责注解其他注解.现在开发过 ...

  3. 用Redis实现分布式锁以及redission使用

    原文:https://my.oschina.net/wangnian/blog/668830 前言:分布式环境有很多问题,比如你前一个请求访问的是服务器A,第二个请求访问到了服务器B,就会发生并发重复 ...

  4. iOS 5解决Could not instantiate class named NSLayoutConstraint问题

    如果使用Xcode 4.5来新建项目,默认是支持AutoLayout的,但是AutoLayout是iOS 6的新特性,如果在iOS 5的simulator上运行程序,会出现Could not inst ...

  5. ie不支持max-height的解决之法

    .div{ max-height: 100px; _height:expression(this.scrollHeight > 100 ? "100px" : "a ...

  6. ubuntu C++开发环境

    最近在VM中装了Ubuntu,为了开发程序,于是在网上找了些由于C/C++开发环境搭建的资料,供大家参考. 以下文字主要讲如何搭建Code::Blocks+wxWidgets. 搭建步骤: 1.安装编 ...

  7. 理解与学习linux 文件系统的目录结构

    1. linux文件系统的结构 linux文件系统是以一种树形结构存在,Linux的文件系统的入口就是/,所有的目录.文件.设备都在/之下,/就是Linux文件系统的组织者,也是最上级的领导者. 2. ...

  8. [翻译] EnterTheMatrix

    Enter The Matrix https://github.com/mpospese/EnterTheMatrix The sample application to accompany my c ...

  9. 为apache安装mod_wsgi的时候出现-fpic的问题

    1.为了在apache里跑python项目,需要安装模块mod_wsgi 2.但是由于yum只支持python2.6,所以通过yum install mod_wsgi方式安装的mod_wsgi是pyt ...

  10. 【BZOJ】【2434】【NOI2011】阿狸的打字机

    AC自动机+DFS序+BIT 好题啊……orz PoPoQQQ 大爷 一道相似的题目:[BZOJ][3172][TJOI2013]单词 那道题也是在fail树上数有多少个点,只不过这题是在x的fail ...