Tomcat跨二级域配置
内容转自: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跨二级域配置的更多相关文章
- tomcat跨域请求
tomcat A请求tomcat B中的数据(本人是在同一台机器上2个tomcat端口不同,在不同机器上有时间会测得) 博主用的是ajax请求 在A 请求B中数据,用下面的方法可以 在被请求的tomc ...
- Asp.Net Core跨域配置
在没有设置跨域配置的时候,Ajax请求时会报以下错误 已拦截跨源请求:同源策略禁止读取位于 http://localhost:5000/Home/gettime 的远程资源.(原因:CORS 头缺少 ...
- Springboot统一跨域配置
前言:跨域是什么? 要知道跨域的概念,我们先明确怎样算是同一个域: 同一个域指的是同一协议,同一ip,同一端口 如果这三同中有一者不同就产生了跨域. 在做前后端分离的项目中,通过ajax请求后台端口时 ...
- Asp.net跨域配置
<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Contro ...
- asp.net (webapi) core 2.1 跨域配置
原文:asp.net (webapi) core 2.1 跨域配置 官方文档 ➡️ https://docs.microsoft.com/zh-cn/aspnet/core/security/cors ...
- .net core api服务端跨域配置
第1步:添加包引用(.net core 2.2 已自带此包,可跳过此步骤) Install-Package Microsoft.AspNetCore.Cors 第2步:在Startup.cs文件的Co ...
- nginx-springboot-vue前后端分离跨域配置
nginx-springboot-vue前后端分离跨域配置 引言 接着上篇--简单的springboot-vue前后端分离登录Session拦截的demo,其中跨域是通过springboot后端全局设 ...
- Asp.net Core3.0 跨域配置
原文:http://www.zilaohu.cn/Jie/Detail_Jie?ID=78840a04-55b8-4988-80b2-f964fd822d63 下面配置后:被拒绝的域请求后,可以进入方 ...
- 常见跨域解决方案以及Ocelot 跨域配置
常见跨域解决方案以及Ocelot 跨域配置 Intro 我们在使用前后端分离的模式进行开发的时候,如果前端项目和api项目不是一个域名下往往会有跨域问题.今天来介绍一下我们在Ocelot网关配置的跨域 ...
随机推荐
- 具体解释Ajax技术
Ajax 可以做什么? 现在 Google Suggest 和 Google Maps 使用了 Ajax,通过 Ajax,我们能够使得client得到丰富的应用体验及交换操作,而用户不会感觉到有网页提 ...
- Selenium2+python自动化52-unittest执行顺序
前言 很多初学者在使用unittest框架时候,不清楚用例的执行顺序到底是怎样的.对测试类里面的类和方法分不清楚,不知道什么时候执行,什么时候不执行. 本篇通过最简单案例详细讲解unittest执行顺 ...
- 内存及字符串操作篇strlen strchar strcmp strcoll strcpy strdup strstr strtok strspn strrchr bcmp bcopy bzero index memccpy memset
bcmp(比较内存内容) 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp 表头文件 #include<string.h> 定 ...
- 学习MongoDB-应用举例(利用java操作MongoDB)
Java驱动程序是MongoDB最早的驱动,已用于生产环境很长时间,十分稳定.可以到MongoDB官方网站下载驱动,驱动版本为2.9.0. 这个jar文件中,有两个包,我们会经常用到com.mongo ...
- there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
建表语句: create table test_table( id integer not null auto_increment primary key, stamp_created tim ...
- C语言:指针的几种形式
字符串的两种形式: 1.字符数组 char name[32] = “zhangsan”;//只能在初始化时这样做 strcpy(name,”lisi”);//整体赋值只能通过s ...
- 利用Python爬虫爬取指定天猫店铺全店商品信息
本编博客是关于爬取天猫店铺中指定店铺的所有商品基础信息的爬虫,爬虫运行只需要输入相应店铺的域名名称即可,信息将以csv表格的形式保存,可以单店爬取也可以增加一个循环进行同时爬取. 源码展示 首先还是完 ...
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...
- Pow(x,n) leetcode java
题目: Implement pow(x, n). 题解: pow(x,n)就是求x的n次方.x的N次方可以看做:x^n = x^(n/2)*x^(n/2)*x^(n%2).所以利用递归求解,当n==1 ...
- 关于opacity的思考
今天在封装图片轮播的插件的时候,产生了这个opacity的小小思考. 我这个轮播的思路不是以前baidu输入法官网的设置外层容器overflow为hidden,position为relative用se ...