tomcat中修改角色及實現登陸驗證框功能

來源:互聯網
上載者:User

在我們ftp登入時都會出現一個的提示框,這個功能可以通過配置我們的檔案進行實現,而不需要寫任何的代碼。

一、修改角色(添加、刪除)

conf/tomcat-users.xml存放tomcat的角色,預設有admin、manager等角色,如果想要添加自訂的角色,可以通過形如

<user username="xiazdong" password="xiazdong" roles="user"/>形式,我們現在配置了一個名字為xiazdong,密碼是xiazdong,角色是user的使用者。

如果重啟tomcat,在tomcat-users.xml中會自動產生一個<role rolename="user"/>的語句。

二、實現登入驗證

在WEB-INF/web.xml中需要寫入形如

<security-constraint><web-resource-collection><web-resource-name>RegisterUsers</web-resource-name><url-pattern>/demo/security.jsp</url-pattern><!-- 驗證的網頁的位置--></web-resource-collection><auth-constraint><role-name>user</role-name><!-- 驗證的角色,別寫成使用者名稱--></auth-constraint></security-constraint><login-config><auth-method>BASIC</auth-method><!-- 關鍵--><realm-name>Registered Users</realm-name></login-config><security-role><role-name>user</role-name><!-- 驗證的角色,別寫成使用者名稱--></security-role>

重啟tomcat,則可以寫一個security.jsp驗證一下。

輸入使用者名稱為xiazdong,密碼是xiazdong即可登入網頁。

如果角色是user的話,則能夠看到網頁。

<%@ page contentType="text/html" language="java" pageEncoding="GB2312"%><html><head><title>title</title></head><body><%if(request.isUserInRole("user")){out.println("Hello world");}%></body></html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.