大概是這學期開學沒多久吧,4月份的時候,為了學習javaEE,裝了Tomcat。過了這么久早就忘記用戶名和密碼了,所以無法進入Tomcat的管理界面。百度(其實我也很想用google)了一堆,幾乎都是修改用戶配置文件,可能是版本的差異,對於我這個版本都沒有太大用處。其實只要按照tomcat的提示一步步去做就可以解決,前提是英語基礎要好,其實技術文章比文學作品和四級考試好懂多了,語言很直白,從來不拐彎抹角。
下面是解決這個問題的步驟:
一、找到Tomcat配置文件的位置,修改tomcat-users.xml
我的配置文件在D:\Program Files\Apache Software Foundation\Tomcat 8.0\conf。在這個目錄下存放了所有有關Tomcat的配置文件,包括 content.xml,server.xml,web.xml,tomcat-users.xml,每個配置文件都有自己的作用,我們需要修改的是跟用戶相關的tomcat-users.xml。
打開tomcat-users.xml配置文件(我省略了部分注釋):
1 <?xml version='1.0' encoding='cp936'?>
2 <tomcat-users xmlns="http://tomcat.apache.org/xml"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
5 version="1.0">
6 <!--
7 NOTE: By default, no user is included in the "manager-gui" role required 8 to operate the "/manager/html" web application. If you wish to use this app, 9 you must define such a user - the username and password are arbitrary. 10 -->
11 <!--
12 NOTE: The sample user and role entries below are wrapped in a comment 13 and thus are ignored when reading this file. Do not forget to remove 14 <!.. ..> that surrounds them. 15 -->
16 <role rolename="tomcat"/>
17 <role rolename="role1"/>
18 <user username="tomcat" password="tomcat" roles="tomcat"/>
19 <user username="both" password="tomcat" roles="tomcat,role1"/>
20 <user username="role1" password="tomcat" roles="role1"/>
21 </tomcat-users>
配置文件里7-11行的注釋說的很清楚:默認情況下沒有用戶包括在可以操作"/manager/html"頁面的"manager-gui"的角色中,如果你想開啟這種應用,你可以定義一個這樣的用戶,用戶名和密碼隨意。於是我添加了幾行代碼,修改后的配置文件是這樣的:
1 <?xml version='1.0' encoding='cp936'?> 2 <tomcat-users xmlns="http://tomcat.apache.org/xml" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd" 5 version="1.0"> 6 <!-- 7 NOTE: By default, no user is included in the "manager-gui" role required 8 to operate the "/manager/html" web application. If you wish to use this app, 9 you must define such a user - the username and password are arbitrary. 10 --> 11 <!-- 12 NOTE: The sample user and role entries below are wrapped in a comment 13 and thus are ignored when reading this file. Do not forget to remove 14 <!.. ..> that surrounds them. 15 --> 16 <role rolename="tomcat"/> 17 <role rolename="role1"/> 18 <user username="tomcat" password="tomcat" roles="tomcat"/> 19 <user username="both" password="tomcat" roles="tomcat,role1"/> 20 <user username="role1" password="tomcat" roles="role1"/>
21 <role rolename="manager-gui"/> 22 <user username="admin" password="admin" roles="admin-gui"/> 23 </tomcat-users>
添加了 <role rolename="manager-gui"/> <user username="admin" password="admin" roles="manager-gui"/> 這兩行代碼后就添加了一個角色manager-gui,並且添加了一個用戶,用戶名是admin,密碼是admin,角色是manager-gui,記得修改完后要Ctrl+S保存一下。
二、重啟Tomcat
命令行怎么重啟我還不太清楚,以后轉移到linux平台的時候更多的肯定還是用命令行,姑且先用圖形化工具吧。不過它好像沒提供restart選項,我只好先stop service,再start。重新啟動的時候,tomcat會重新讀取xml配置文件,這時剛才具有管理員權限的用戶就被添加進去了。在瀏覽器輸入192.168.1.106:8080,瀏覽器會訪問默認頁面
在這個頁面的右辺有3個按鈕,我們剛才的配置只會對第1個和第2個按鈕起作用,點擊這兩個button,輸入用戶名和密碼就可以訪問/manage/html 和/manage/status頁面了
三、遇到的新問題
然而如果點擊第1個或者第3個按鈕,就會出現下面的情況:
在這個報錯頁面里已經給出了詳細的解決方案,如果英文水平還可以並且願意靜下心來看一看,就知道怎么解決這個問題了。剛才我們添加了一個manager-gui的角色,並且添加了一個具有這種角色相應權限的用戶。這次我們只需要再添加一個admin-gui角色,並添加這樣一個屬於這種角色的用戶就可以了。我在原來的配置文件里又添加了39,40兩行代碼:
修改后保存,這次,重啟Tomcat,就可以訪問原來403禁止訪問的/host-manager/html頁面了
四、最終的解決之道
然而,顧此失彼, 雖然/host-manager/html頁面可以訪問了,但是原來的兩個頁面卻被403forbidden了,我思考了一會兒,覺得問題還是出在配置文件上。在剛才的方法中,我添加了兩個名字相同的用戶admin(暫且不論他們的密碼是否相同),分別賦予他們不同的角色,但是tomcat在處理這個信息的時候會覆蓋這個前一條配置信息,tomcat會以為你想修改這個用戶的角色,把他從manager-gui改為admin-gui。如果我當時在創建角色為admin-gui的用戶是名字不和manager-gui角色的用戶名相同,比如取名admin2,這樣在登錄/host-manager/html頁面時用admin2的用戶名登錄,並輸入相應的密碼就不會403forbidden了。但是要記兩個用戶名和密碼很麻煩,怎么辦呢?
從配置文件前面的部分 <user username="tomcat" password="tomcat" roles="tomcat"/><user username="both" password="tomcat" roles="tomcat,role1"/>可以看出,這樣就給一個用戶同時賦予兩種角色了。於是我將配置文件修改成了這樣:<role rolename="admin-gui"/> <user username="admin" password="admin" roles="admin-gui,manager-gui"/> 保存后重啟tomcat,這次終於奏效了。
五、感悟
遇到問題的時候,文檔或者配置文件本身就是最好的資料,要善於模仿文檔的書寫方式試着去猜測並理解每個參數的含義,同時一定要具備一定的英語閱讀理解能力。