29、Tomcat只允许指定域名访问,禁用IP地址访问,防止恶意解析
1.1、测试环境说明:
Linux版本:7.6
IP地址:10.11.220.123/24
Tomcat版本:tomcat-8.5.37(端口号为8080)
Jdk版本:1.8.0_202
1.2、配置tomcat server.xml 文件(Engine组件,标红的部分是需要修改的内容):
<Engine name="Catalina" defaultHost="VirtualHost">
<!-- 设置默认虚拟主机域名,指向虚假的虚拟目录 -->
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="www.tomcat8080.com" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- 真实使用的虚拟目录,域名也是你指定的域名 -->
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127.0.0.1" deny=""/> -->
</Host>
<Host name="VirtualHost" appBase="VirtualHostDir"
unpackWARs="true" autoDeploy="true">
</Host>
<!-- 建立一个虚假的虚拟目录VirtualHostDir,这个目录是不存在的,当访问IP地址或其他域名,将被转向到访问这个虚拟空目录上。VirtualHostDir目录如果不存在会自动创建 -->
</Engine>
提示:tomcat在启动的情况下修改server.xml 文件后需要重启tomcat实例使配置生效。
1.3、测试:
1、创建tomcat测试文件:
2、在widows电脑的C:\Windows\System32\drivers\etc\hosts文件中添加解析记录如下:
10.11.220.123 www.tomcat8080.com
3、在浏览器中使用域名访问(可以正常访问):
www.tomcat8080.com:8080
4、在浏览器中使用ip地址访问(无法进行访问):
10.11.220.123:8080
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏