涉及内容:配置tomcat
1、迁移web应用目录
在服务器运行多个Tomcat实例,需要每个JVM单独有webapp目录,有些共享目录,但不可以共享配置文件
复制conf目录所有内容,同时创建common、logs、temp, server,shared、webapps、work
修改server.xml配置文件
2、改变默认端口8080
apache安装目录/conf/server.xml 将8080改成80
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
3、将端口80重定向成端口8080
利用linux 内核 iptables# iptables -t nat -L (列出当前linux网络配置规则)
输入如下命令(对于配置iptables规则慎重配置) 这是对于所有ip地址进行配置
# iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
# iptables -t nat -I OUTPUT -p tcp --dport 80 -j REDIRECT --to-ports 8080
如果是具体的ip地址如下配置
# iptables -t nat -I PREROUTING -p tcp --dst 192.168.1.100 --dport 80 -j REDIRECT --
to-ports 8080
# iptables -t nat -I OUTPUT -p tcp --dst 192.168.1.100 --dport 80 -j REDIRECT --toports
8080
这里只针对192.168.1.100 IP地址 进行端口重定向
可以配置server.xml 文件配置代理端口和代理IP地址
<Connector port="8080" protocol="HTTP/1.1" proxyPort="80"
connectionTimeout="20000"
redirectPort="8443" proxyName="hostname.example.com" />
proxyPort : 代理端口,proxyName:代理主机
通过一个服务包装器运行tomcat在80端口
jsvc 在linux创建一个服务启动java应用,具体操作可以百度一下,也就可以用jsvc来管理tomcat
4、通用异常
端口被占用了
5、Java VM 配置
java虚拟机配置选项
内存设置 -Xms384M 设置虚拟机启动时堆内存
内存设置 -Xmx384M 设置虚拟机最大可以扩展虚拟内存
安全调试 -Djava.security.debug=all 开启所有调试输出
调试 -enableassertions 开启断言检查
调试 -verbose:class 将虚拟机的信息输出到控制台
调试 -verbose:gc 开启垃圾回收日志输出
图形 -Djava.awt.headless=true 允许虚拟机运行没有任何展示设备上(例如键盘,显示屏)
国际化 -Duser.language=en 设置tomcat运行语言
国际化 -Dfile.encoding=UTF-8 设置tomcat默认使用字符编码
网络 -Djava.net.preferIPv4Stack=true 配置虚拟机使用IPv4替换IPv6
设置虚拟机启动项可以在CATALINA_HOME/bin/catalina.* 文件设置JAVA_OPTS
例如:(使用JPDA远程调试客户端)
JAVA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket, address=8000,server=y,suspend=n"
具体可以参考:JPDA教程
通过JMX远程连接监控
JAVA_OPTS="-Dcom.sun.management.jmxremote=true \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=false"
-Dcom.sun.management.jmxremote.password.file=/path/to/pw/file"
JMX具体可以参考:
JMX文档
6、改变JSP编译器
你可以是用JDT或JDK‘s的javac编译’
Java编译器选择:
内嵌JDTjava编译器(默认)
使用Apache Ant去编译JSP页
7、管理领域、角色和用户
7.1、领域:可以实现UserDatabaseRealm, JDBCRealm, JNDIRealm, and JAASRealm类
然后修改server.xml配置文件
<Realm className="some.realm.implementation.className"
customAttribute1="some custom value"
customAttribute2="some other custom value"/>
UserDatabaseRealm
只有在启动的时候进行验证,在$CATALINA_HOME/conf/tomcat-user.xml中进行配置
文件片段
<!--
NOTE: By default, no user is included in the "manager" role
required to operate the "/manager" web application. If you
wish to use this app, you must define such a user - the
username and password are arbitrary.
-->
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat"
roles="tomcat,role1" />
</tomcat-users>
表示将用户名密码存到数据库中
<!-- Set up a JDBC Real for JabaDot user database -->
<Realm className="org.apache.catalina.realm.JDBCRealm"
driverName="org.postgresql.Driver"
connectionURL="jdbc:postgresql:jabadot"
connectionName="system"
connectionPassword="something top secret"
userTable="users" userCredCol="passwd"
userRoleTable="controls" roleNameCol="roles"
userNameCol="nick"/>
Realm介绍
className 类名路径org.apache.catalina.realm.JDBCRealm
connectionName : 数据库用户名
connectionPassword : 数据库密码
connectionURL : 数据库URL
digest:加密算法(默认明文)
driverName : JDBC驱动类名
roleNameCol : 角色列(在角色表中)
userNameCol: 用户名列(在用户表中)
userCredCol : 密码列(在用户表中)
userRoleTable : 角色表名
userTable : 用户表名
JNDIRealm
从LDAP取出用户名、密码,角色 ,这时候可以使用JNDIRealm (在server.xml配置)
<Realm className="org.apache.catalina.realm.JNDIRealm"
connectionURL="ldap://ldap.groovywigs.com:389"
userPattern="uid={0},ou=people,dc=groovywigs,dc=com"
roleBase="ou=groups,dc=groovywigs,dc=com"
roleName="cn"
roleSearch="(uniqueMember={0})"/>
JAASRealm
它通过Java Authentication 和Authorization Service(JAAS) 取用户信息
列出JAASRealm实现属性
className 类名org.apache.catalina.realm.JAASRealm
appName : 应用名默认值为Tomcat,你可以改成任何值
userClassNames 配置规则表示失效用户javax.security.Principal (冒号分割)
roleClassNames 配置规则表示失效的角色javax.security.Principal
useContextClassLoader 告诉JAASRealm何时上下文加载类中加载
配置文件
# export JAVA_OPTS=\
'-Djava.security.auth.login.config=/root/.java.login.config'
<Realm className="org.apache.catalina.realm.JAASRealm"
userClassNames="com.sun.security.auth.UnixPrincipal"
roleClassNames="com.sun.security.auth.UnixNumericGroupPrincipal"/>
在web.xml配置安全限制
<?xml version="1.0"?>
<security-constraint>
<web-resource-collection>
<web-resource-name>Entire Application</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>0</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>My Club Members-only Area</realm-name>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/error.html</form-error-page>
</form-login-config>
</login-config>
<security-role>
<role-name>0</role-name>
</security-role>
配置 .java.login.conf 文件
Tomcat {
com.sun.security.auth.module.UnixLoginModule required debug=true;
};
8、容器安全管理
有四种不同方法获取证书
基础认证 : 用户密码通过base64编码之后
加密认证: 用户密码通过加密算法
表单认证: 用户密码通过表单提交
客户端认证: 用户使用客户端认证证书
8.1、基础认证
它替换了Apache Web Server’s .htaccess (也是限制访问方法,参考http://www.jb51.net/article/25476.htm)
<!--
Define the Members-only area, by defining
a "Security Constraint" on this Application, and
mapping it to the subdirectory (URL) that we want
to restrict.
-->
<security-constraint>
<web-resource-collection>
<web-resource-name>
Entire Application
</web-resource-name>
<url-pattern>/members/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>member</role-name>
</auth-constraint>
</security-constraint>
<!-- Define the Login Configuration for this Application -->
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>My Club Members-only Area</realm-name>
</login-config>
8.2、加密认证
在web.xml文件中配置
<!--
Define the Members-only area, by defining
a "Security Constraint" on this Application, and
mapping it to the subdirectory (URL) that we want
to restrict.
-->
<security-constraint>
<web-resource-collection>
<web-resource-name>
Entire Application
</web-resource-name>
<url-pattern>/members/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>member</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>DIGEST</auth-method>
<realm-name>My Club Members-only Area</realm-name>
</login-config>
在server.xml配置 算法为MD5
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase" digest="MD5"/>
或者通过工具手动加密密码, 然后采用UserDatabaseRealm进行配置
<?xml version='1.0'?>
<tomcat-users>
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="member"/>
<user username="jasonb"
password="9a3729201fdd376c76ded01f986481b1"
roles="member"/>
</tomcat-users>
8.3、表单认证
配置:
<login-config>
<auth-method>FORM</auth-method>
<realm-name>My Club Members-only Area</realm-name>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/error.html</form-error-page>
</form-login-config>
</login-config>
一个简单登录页面login.html
<html>
<head>
<title></title>
</head>
<body>
<center>
<!-- Begin login form -->
<form method="post" action="j_security_check" name="loginForm" id="loginForm">
<table border="0" cellspacing="5">
<tr>
<td height="50">
Please log in.
</td>
</tr><!-- Username and password prompts fields layout -->
<tr>
<td>
<table width="100%" border="0" cellspacing="2" cellpadding="5">
<tr>
<th align="right">
Username
</th>
<td align="left">
<input type="text" name="j_username" size="16" maxlength="16">
</td>
</tr>
<tr>
<th align="right">
Password
</th>
<td align="left">
<input type="password" name="j_password" size="16" maxlength="16">
</td>
</tr>
<tr>
<td width="50%" valign="top">
<div align="right">
</td>
<td width="55%" valign="top">
</td>
</tr><!-- Login and reset buttons layout -->
<tr>
<td width="50%" valign="top">
<div align="right">
<input type="submit" value='Login'>
</div>
</td>
<td width="55%" valign="top">
<input type="reset" value='Reset'>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form><!-- End login form -->
</center><script language="JavaScript" type="text/javascript">
<!--
// Focus the username field when the page loads in the browser.
document.forms["loginForm"].elements["j_username"].focus( )
// -->
</script>
</body>
</html>
错误页面error.html
<html>
<head>
<title></title>
</head>
<body>
<center>
<h2>
Login failed.<br>
Please try <a href="/">logging in again.</a>
</h2>
</center>
</body>
</html>
8.4、客户端证书验证
与之代替是发送X.509数字证书。
9、单次登陆(不要重复登陆验证)
也就是有多个web 应用都有验证,只需验证一次就好了,共享验证(也就是你都通过,我就是不需要再次验证了)
在server.xml配置
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
单次登录一些限制要求:
必须使用tomcat要求四种验证方式而不是自定义(BASIC、DIGEST,FORM 、CLIENT-CERT)
要求是HTTP cookies,标准session ID 为JSESSIONID, 而单次登录的session ID 为 JSESSIONIDSSO
10、控制会话
会话经常用于购物车等
但是会话肯定存在某一个地方,也就需要一个管理者进行管理,类似如下配置
<Manager className="some.manager.implementation.className"
customAttribute1="some custom value"
customAttribute2="some other custom value"/>
11、会话持久性
会话在再次重连的时候,保存当前状态,需要将信息保存在某个位置,方式有关系型数据库、轻量文件访问协议、或者定义保存磁盘上的文件11.1、标准管理器
没有在server.xml显式配置,默认就是StandardManager,当tomcat正常关闭时候,它会将会话信息保存到名称为SESSION.ser文件里。它一般在$CATALINA_HOME/work/Catalina/
<hostname>/<webapp-name>/目录下,在下次启动自动加载这个文件,但是如果不是正常关机,那么会话会丢失。
例子:
<Manager className="org.apache.catalina.session.StandardManager"
maxInactiveInterval="7200"/>
最大会话时间间隔为2小时,7200秒 ,它会有很多参数,可以参考其它文档
11.2、PersistentManager(持久管理器)
这个就是解决没有正常关机时候保证会话可以被保存,需要这样类去执行org.apache.catalina.session.PersistentManager ,存储方法分为文件和jdbc数据库
第一种文件存储(FileStore):
<Manager className="org.apache.catalina.session.PersistentManager"
saveOnRestart="true">
<Store className="org.apache.catalina.session.FileStore"/>
</Manager>
涉及属性:类本身相关 className,时间相关的(会话活跃度,多久保存一次,空闲间隔),安全相关的(加密,算法需要随机数)
例子:如下
<Manager className="org.apache.catalina.session.PersistentManager"
saveOnRestart="true">
<Store className="org.apache.catalina.session.FileStore"
directory="/home/jasonb/tomcat-sessions"/>
</Manager>
生成session文件名,《session ID》.session, 例如:4FF8890ED8A53D6B.session
第二种JDBC存储(JDBCStore)
在server.xml配置
<Manager className="org.apache.catalina.session.PersistentManager"
saveOnRestart="true">
<Store className="org.apache.catalina.session.JDBCStore"
driverName="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mydb?user=jb;password=pw"/>
</Manager>
这时候需要在数据库创建一个表来存储
create table tomcat$sessions (
id varchar(64) not null primary key,
data blob
valid char(1) not null,
maxinactive int not null,
lastaccess bigint not null,
);
属性:包括数据库表的对应关系,可以采用默认值如上所示。 例如sessionTable 会话表名 默认值tomcat$sessions. 规则是session{列名}Col,例如数据id sessionIdCol
className,driverName,connectionURL,sessionTable,sessionIdCol,sessionDataCol,sessionValidCol,sessionMaxInactiveCol,sessionLastAccessedCol,checkInterval
12、访问JNDI和JDBC资源
JNDI被用来定位数据库资源和其他资源。理解(JNDI是资源检索目录)
12.1、JDBCDataSources
配置JDBC连接,首先要在 web.xml配置
<resource-ref>
<description>
The database DataSource for the Acme web application.
</description>
<res-ref-name>jdbc/JabaDotDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
或者java代码
Context ctx = new InitialContext( );
DataSource ds = (DataSource)
ctx.lookup("java:comp/env/jdbc/JabaDotDB");
Connection conn = ds.getConnection( );
... Java code that accesses the database ...
conn.close( );
然后在配置资源元素(可以配置在Tomcat安装目录下conf文件的context.xml文件中,或在META-INF文件夹下创建context.xml然后添加进去)本质上一个全局,一个局部
<!-- Configure a JDBC DataSource for the user database. -->
<Resource name="jdbc/JabaDotDB"
type="javax.sql.DataSource"
auth="Container"
user="ian"
password="top_secret_stuff"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql:jabadot"
maxActive="8"
maxIdle="4"/>
最后你还要安装连接数据驱动jar复制到$CATALINA_HOME/common/lib目录下
12.2、其他JNDI资源。(认为(context)上下文,就是资源配置列表)
类似Spring自动注入感觉。应用启动时候实例化对象,然后对象可以通过工厂类获取
实例化一个日历类
<!--
How to get a Calendar on demand (real code would just
call Calendar.getInstance; we just pick on Calendar as
a handy Bean.
-->
<resource-env-ref>
<description>
Fake up a Factory for Calendar objects
</description>
<resource-env-ref-name>
bean/CalendarFactory
</resource-env-ref-name>
<resource-env-ref-type>
java.util.GregorianCalendar
</resource-env-ref-type>
</resource-env-ref>
在server.xml 中子标签Context 添加
<Resource name="bean/CalendarFactory"
type="java.util.GregorianCalendar"
auth="Container"
factory="org.apache.naming.factory.BeanFactory"/>
13、Servlet自动加载
只要文件改变,默认情况下会自动加载,对于调试很有用。其他情况需要不用重新加载。修改(server.xml的Context元素或你自定义context文件碎片属性reloadable=false)
14、自定义用户目录
个人可以在特定目录上发布自己网页,用(~)加上用户名来作为某个用户站点。例如;
http://www.cs.myuniversity.edu/~ian
http://members.mybigisp.com/~ian
配置用户映射关系
<Listener className="org.apache.catalina.startup.UserConfig"
directoryName="public_html"
userClass="org.apache.catalina.startup.PasswdUserDatabase"/>
网页文件可能存在如下目录:/home/users/ian/public_html 或 /users/
jbrittain/public_html.
固定根目录
<Listener className="org.apache.catalina.startup.UserConfig"
directoryName="public_html"
homeBase="/home"
userClass="org.apache.catalina.startup.HomesUserDatabase"/>
/home/ian/public_html 和/home/jbrittain/public_html.
如果是window可能是:C:\home. Listener标签需要写在Host标签的下,注意它重写host而不是context,例如上下文名字如tomcatbook,
http://localhost/~ian将会有效,而http://localhost/tomcatbook/~ian将会404异常
例子:
1、在server.xml配置
2、在C盘创建相应目录和文件
3、开启tomcat服务然后用浏览器输入网址 http://localhost:8080/~jack/login.html
效果:
15、Tomcat自带案例
在目录CATALINA_HOME/weabpps下
16、通用网关接口Common Gateway Interface(CGI)
简单来说理解将各种肉变成香肠的机器, 通用网关接口是将脚本变成html页面返回。
具体可以参考单独写一个博客:
17、下载历史版本tomcat
如果需要下载tomcat历史版本:https://archive.apache.org/dist/tomcat/tomcat-9/ 把数字改一下就行了
18、进入tomcat管理图形界面
http://localhost:8080/manager/html 密码可以查看 与servlet.xml同一级目录下tomcat-user.xml文件中,好像默认账户:admin 密码:password