tomcat下的https项目创建与部署
1.1 生成keystore文件及导出证书
步奏1:打开控制台,运行:
%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSA
(如果你已经配置过javahome,直接输入)
keytool -genkey -alias tomcat -keyalg RSA
首先会要求输入密码,这里使用123123
步奏2:输入基本信息 (除了名称与姓氏其他都可以直接回车)
输入密钥库口令:
再次输入新口令:
您的名字与姓氏是什么?
[Unknown]: tuhao(这里可以输入你的项目的域名:如localhost)
您的组织单位名称是什么?
[Unknown]: tuhaojia
您的组织名称是什么?
[Unknown]: fnic
您所在的城市或区域名称是什么?
[Unknown]: didu
您所在的省/市/自治区名称是什么?
[Unknown]: didu
该单位的双字母国家/地区代码是什么?
[Unknown]: cn
CN=tuhao, OU=tuhaojia, O=fnic, L=didu, ST=didu, C=cn是否正确?
[否]: y
之后输入 <tomcat> 的密钥口令 (如果和密钥库口令相同, 按回车):
步奏3:拷贝keystore文件
完毕后会在当前目录下,会产生一个:.keystore文件,window系统默认在“C:\Users\你机器的名称” 下,将它拷贝到tomcat的bin目录下。
步奏4:导出证书
从控制台进入tomcat的bin目录,本机环境是:D:\Tomcat7\bin> ,然后输入:
D:\Tomcat7\bin>keytool -selfcert -alias tomcat -keystore .keystore
D:\Tomcat7\bin>keytool -export -alias tomcat -keystore .keystore -storepass 你的密码(这里是123123) -rfc -file tomcat.cer
此时会在D:\Tomcat7\bin>下生成tomcat.cer证书文件。
1.2 配置tomcat
步奏1:
打开tomcat目录下的/conf/server.xml 找到“SSL HTTP/1.1 Connector” 那一块,取消注释并将它改成:
<Connector port="443" protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
keystoreFile="bin/.keystore" keystorePass="123123"
clientAuth="false" sslProtocol="TLS"/>
请注意,这里我已经将tomcat的端口改成了80,相应的,https的端口我也改成了443(即默认的https端口)。
接下来重启tomcat,用https:// localhost/访问网站验证一下就行了。
可选步奏:
在tomcat的conf目录下的web.xml文件下配置以下内容则可以强制让整个项目使用https访问,请根据需求配置。
<security-constraint>
<web-resource-collection >
<web-resource-name >SSL</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection> <user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
3 创建项目访问
注意:从https下的链接跳转到http的连接会造成session丢失,原因在于tomcat会给不同协议的请求创建不同的cookie
,并生成不同的sessionid,所以造成与服务端的session索引失败而无法获取session数据。
解决方法:可以编写一个过滤器,对所有的请求做处理,如果session是新的https session,则创建一个JSESSIONID cookie,
并设置到Servlet Response中,这样就突破了tomcat的限制,把https下的session传递到http协议下
3.1 建立Wrapper类,用于处理所有的Serverlet Request:
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; //处理所有的Serverlet Request
public class MyRequestWrapper extends HttpServletRequestWrapper { private HttpServletResponse response = null; public MyRequestWrapper(HttpServletRequest request) {
super(request);
} public void setResponse(HttpServletResponse response) {
this.response = response;
} public HttpSession getSession() {
HttpSession session = super.getSession();
processSessionCookie(session);
return session;
} public HttpSession getSession(boolean create) {
HttpSession session = super.getSession(create);
processSessionCookie(session);
return session;
} /**
* 当从https重定向到http时,tomcat会创建一个新的sessionid,所以造成session跟踪不到,
* 这里获取先从cookie中获取sessionid,保存到新的cookie
* @param session
*/
private void processSessionCookie(HttpSession session) {
if (null == response || null == session) {
// No response or session object attached, skip the pre processing
return;
} // cookieOverWritten - 用于过滤多个Set-Cookie头的标志
Object cookieOverWritten = getAttribute("COOKIE_OVERWRITTEN_FLAG");
if (null == cookieOverWritten && isSecure() && isRequestedSessionIdFromCookie() && session.isNew()) {
// 当是https协议,且新session时,创建JSESSIONID cookie以欺骗浏览器
Cookie cookie = new Cookie("JSESSIONID", session.getId());
cookie.setMaxAge(-1); // 有效时间为浏览器打开或超时
String contextPath = getContextPath();
if ((contextPath != null) && (contextPath.length() > 0)) {
cookie.setPath(contextPath);
} else {
cookie.setPath("/");
}
response.addCookie(cookie); // 增加一个Set-Cookie头到response
setAttribute("COOKIE_OVERWRITTEN_FLAG", "true");// 过滤多个Set-Cookie头的标志
}
}
}
3.2 再把上述Wrapper类与Filter建立关联:
import java.io.IOException; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class HttpsFilter implements Filter { @Override
public void destroy() {
// TODO Auto-generated method stub } @Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp; MyRequestWrapper myrequest = new MyRequestWrapper(request);
myrequest.setResponse(response);
chain.doFilter(myrequest, response);
} @Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub } }
3.3 控制层执行代码
import java.io.IOException;
import java.util.UUID; import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import entity.User; public class AdminServlet extends HttpServlet { /**
*
*/
private static final long serialVersionUID = 7179221326322012461L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if("login".equals(action)){
login(request,response);
} else if("index".equals(action)){
index(request, response);
} else if("httplink".equals(action)){
httplink(request,response);
} else if("tohttps".equals(action)){
tohttps(request,response);
} else if("httpslink".equals(action)){
httpslink(request,response);
} else if("tohttp".equals(action)){
tohttp(request,response);
}
} public void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ /*Cookie cookie = new Cookie("JSESSIONID", request.getSession().getId());
response.addCookie(cookie); //
*/ response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
user.setId(UUID.randomUUID().toString().replaceAll("-", ""));
user.setUserName(username);
user.setPassword(password);
request.getSession().setAttribute("user", user);
System.out.println(request.getRequestURI());
System.out.println(request.getRequestURL());
System.out.println(request.getContextPath());
System.out.println(request.getLocalName());
System.out.println(request.getLocalPort());
//重定向到http
response.sendRedirect("http://"+request.getServerName()+":"+"80"+request.getContextPath()+"/admin?action=index");
} public void index(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
request.getRequestDispatcher("/WEB-INF/page/index.jsp").forward(request, response);
System.out.println("主页的sessionid:"+request.getSession(false).getId());
Cookie[] cookies = request.getCookies();
for (Cookie c : cookies) {
if (c.getName().equalsIgnoreCase("JSESSIONID")) {
System.out.println("cookie中的sessionid:"+c.getValue());
}
}
} public void httpslink(HttpServletRequest request,HttpServletResponse response) throws IOException{
response.sendRedirect("https://"+request.getServerName()+request.getContextPath()+"/admin?action=tohttps");
}
public void tohttps(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
request.getRequestDispatcher("/WEB-INF/page/https.jsp").forward(request, response);
System.out.println("https页面的sessionid:"+request.getSession(false).getId());
Cookie[] cookies = request.getCookies();
for (Cookie c : cookies) {
if (c.getName().equalsIgnoreCase("JSESSIONID")) {
System.out.println("cookie中的sessionid:"+c.getValue());
}
}
} public void httplink(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
response.sendRedirect("http://"+request.getServerName()+":"+"80"+request.getContextPath()+"/admin?action=tohttp");
}
public void tohttp(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
request.getRequestDispatcher("/WEB-INF/page/index.jsp").forward(request, response);
System.out.println("http页面的sessionid:"+request.getSession(false).getId());
Cookie[] cookies = request.getCookies();
for (Cookie c : cookies) {
if (c.getName().equalsIgnoreCase("JSESSIONID")) {
System.out.println("cookie中的sessionid:"+c.getValue());
}
}
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request, response);
} }
3.4 页面代码
login.jsp
<body>
<form action="${pageContext.request.contextPath}/admin?action=login" method="post">
账号:<input id="username" name="username" type="text" ><br/>
密码:<input id="password" name="password" type="text" ><br/>
<input id="btn" type="submit" value="登录">
</form> <script type="text/javascript">
//强制让页面发送https请求
var targetProtocol = "https:";
if (window.location.protocol != targetProtocol)
window.location.href = targetProtocol + window.location.href.substring(window.location.protocol.length);
</script>
</body>
index.jsp
<body>
<table>
<tr>
<th>id:</th>
<th>账号:</th>
<th>密码:</th>
</tr>
<tr>
<td>${sessionScope.user.id}</td>
<td>${sessionScope.user.userName}</td>
<td>${sessionScope.user.password}</td>
</tr>
</table>
<a href="${pageContext.request.contextPath}/admin?action=httpslink" >跳转到https</a>
</body>
https.jsp
<body>
<table>
<tr>
<th>id:</th>
<th>账号:</th>
<th>密码:</th>
</tr>
<tr>
<td>${sessionScope.user.id}</td>
<td>${sessionScope.user.userName}</td>
<td>${sessionScope.user.password}</td>
</tr>
</table>
<a href="${pageContext.request.contextPath}/admin?action=httplink" >跳转到http</a> </body>
tomcat下的https项目创建与部署的更多相关文章
- Asp.Net Core 第01局:项目创建和部署 转载https://www.jianshu.com/p/9c9750e23b3e
总目录 一.前言 本文通过从项目创建到部署,简单介绍Asp.Net Core. 二.环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 三.开局 第一手:创建项目 ...
- tomcat下配置https方式
[本地HTTPS]①.<Connector SSLEnabled="true" clientAuth="false" keystoreFile=" ...
- tomcat下配置https环境
在网上搜了一下,内容不是非常完好. 现进行整理,做个学习笔记,以备以后使用. (1)进入到jdk下的bin文件夹 (2)输入例如以下指令"keytool -v -genkey -alias ...
- 【CAS单点登录视频教程】 第04集 -- tomcat下配置https环境
目录 ----------------------------------------- [CAS单点登录视频教程] 第06集[完] -- Cas认证 学习 票据认证FormsAuthenticati ...
- tomcat下配置https环境(windows环境)
在网上搜了一下,内容不是很完善.现进行整理,做个学习笔记,以备以后使用. (1)进入到jdk下的bin目录 (2)输入如下指令“keytool -v -genkey -alias tomcat -ke ...
- linux下的C++项目创建
CMake项目的完整构建 Linux下的CMake项目通常由几个文件夹组成.小伙伴们可以先在自己的电脑上新建一个文件夹,作为你代码的根目录,然后往里面建几个子文件夹,这里并不涉及具体的代码,只是可以作 ...
- IDEA的Tomcat配置Web的项目创建以及Servlet简单运行。
相关软件: 1.IDEA编译器 2.JDK 3.Tomcat (相关软件都可以到官网上下载,老表提示:不要下载最新版本因为不要做试验品) IDEA的安装非常简单,找好安装的盘,n ...
- Tomcat 下启用 https:// 访问
步骤: 1 创建 .keystore 文件 JDK中自带了keytool工具用于生成证书文件 keytool工具在$JAVA_HOME/bin 目录下可以使用命令 keytool -genkey -a ...
- tomcat下程序包的安装与部署
还没亲自在服务器进行部署,但是参考了公司文档,等自己安装部署的时候,再将文档补充完整. 1.初始化数据库 2.修改war包的数据 主要包括的一些配置是数据库的连接配置. 3.将包发布 这个步骤下有几个 ...
随机推荐
- CSS垂直居中指南
大概整理下css中的垂直居中问题 主要分两大类 1 知道块元素尺寸,一般就是指知道元素高度. 方法一:最简单的,类比水平居中. 思路:子元素设置为absolute; top bottom都设置为0 ...
- LazyLoad使用注意
今天使用ProgressHUD,进行网络请求后显示加载完成提示框,但是无效,检查以后发现数据源数组使用了懒加载,在调用数组之前调用ProgressHUD里的方法,根本无效啊!以后用懒加载注意.
- hdu 2084
ps:这道题...是DP题..所以我去看了百度一些东西,才知道了什么是状态方程,状态转移方程.. 做的第一个DP题,然后TLE一次.贴上TLE的代码: #include "stdio.h&q ...
- Web页中table导出到execl(带模板)
1.将excel另存为html,将其复制到aspx文件中 2.输出格式为excel InitData(); Response.Clear(); Response.Buffer = true; Resp ...
- ntlk_data安装小结
<Python自然语言处理>用nltk.download()的方法安装书中所用语料库数据,不太好使.一是部分网友反映的下载很慢很慢,二是下载链接,无论书上.NLTK官网(http://nl ...
- Warning: Attempt to dismiss from view controller <UIViewController: 0x17d71c10> while a presentation or dismiss is in progress!
昨天 调试程序 已经快要上线了 突然有个BUG 找了半天 才找到是因为这个警告 但是 解决这个警告又花了一天的时间 试了各种消除控制器的方法 都不可用 其中 并且 有这个bug 手机真机测试完全没问 ...
- javascript 中的 delete
那么,为什么我们能删除一个对象的属性: var x = { a: 1 }; delete x.a; // true x.a; // undefined 但却不能删除一个变量: var x = 1; d ...
- VS2010 刷新工具箱(刷新自定义控件)
有时候自己自定义了控件,定义完后却不见工具箱中刷新出来自定义的控件,解决方案有了三种: 点评:在项目中增加了几个自定义控件,想在窗口上添加时却发现工具箱根本就没有些控件,晕了.记得2008都可以自动出 ...
- Python12期培训班-day1-三级菜单代码分享
#!/usr/bin/env python3 import sys import os zonecode = { '广东省': {'广州市':['越秀区','海珠区','荔湾区','天河区'], '深 ...
- LPTHW 笨办法学python 20章
本章节讲述了,函数和文件的综合操作. 分别 执行了.1.读出文件所有内容,2.把文件重置至文件开头.3.打印一行. 我在本节作了一个小小的改良,设置了一个全局变量,记录当前应该输入哪一行,如果执行过一 ...