Java web的安全約束--Basic驗證

來源:互聯網
上載者:User

標籤:resource   auth   pattern   post   標識   int   gui   security   dmi   

要進行basic驗證是使用者名稱/口令機制,當瀏覽器要訪問受保護的資源時,伺服器會要求一個使用者名稱和口令,只有輸入了合法的使用者名稱和口令。伺服器才發送資源。使用者名稱和口令可以儲存在安全域中。安全域是標識一個Web應用程式的合法使用者名稱和口令的“資料庫”,其中還包含了與使用者相關的角色。

例子:使用basic和MemoryRealm登入

    1、在tomcat下的/conf/tomcat-users.xml定義了角色和使用者,還有一些角色

<tomcat-users xmlns="http://tomcat.apache.org/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
version="1.0">
<user username="li" password="123456" roles="admin-gui,manager-gui" />
<role rolename="manager"/>
<user username="admin" password="123456" roles="manager"/>

</tomcat-user>

    2、在web.xml中定義如下:

 

<security-constraint>
<web-resource-collection>
<web-resource-name>adminResource</web-resource-name>
<url-pattern>/AccountServlet</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<description>allManager</description>
<role-name>manager</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>

<login-config>
<auth-method>BASIC</auth-method>
<realm-name>test</realm-name>
</login-config>



<security-role>
<role-name>manager</role-name>
</security-role>

其中<security-constraint>標籤設定受保護的資源和可以訪問的使用者角色;

<login-config>是驗證機制,訪問受保護的資源,彈出的對話方塊不同,它的<realm-name>test</realm-name>;是彈出的對話方塊中顯示的安全域的名

<security-role>是用來定義,使用的角色

這樣就可對資源進行保護,在訪問資源時,在彈出的對話方塊中輸入之前定義的使用者,密碼就可以訪問了

Java web的安全約束--Basic驗證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.