<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns="http://xmlns.jcp.org/xml/ns/javaee"

    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

    version="3.1">

 

    <security-role><!-- 這是一個群組,設定應用程式會用到的role(群組) -->

       <role-name>Manager</role-name>

       <!-- 設定一個應用程式內合法的使用者role,在deploy時對應到container裡的設定 -->

    </security-role>

    <security-role>

       <role-name>Employee</role-name>

    </security-role>

    <security-role>

       <role-name>People</role-name>

    </security-role>

 

<!--   <login-config> -->

<!--       <auth-method>BASIC</auth-method> -->

<!--       <realm-name>有人跟你要密碼</realm-name> -->

<!--   </login-config> -->

 

    <login-config>

    <auth-method>FORM</auth-method>

       <form-login-config><!-- 只有 FORM有這個方法 -->

           <form-login-page>/WEB-INF/form/login.jsp</form-login-page><!--這個定義用來負責收集帳密的頁面必須包含一個form表單-->

           <form-error-page>/WEB-INF/form/error.jsp</form-error-page>

       </form-login-config>

    </login-config>

 

    <security-constraint><!-- 設定哪些網頁屬於機密 -->

       <web-resource-collection><!-- 使用子標籤設定哪些網頁要被保護 -->

           <web-resource-name>DEMO01</web-resource-name><!-- 在此設定一個web.xml中不重複的名稱 -->

           <url-pattern>/dir1/*</url-pattern><!-- 設定需要被保護的網頁資源URL,現在dir資料夾底下的需要帳密 -->

       </web-resource-collection>

       <auth-constraint>

           <role-name>Manager</role-name><!-- 設定可以使用被保護網頁的role(群組),此群組需對應到上面定義的security-role -->

       </auth-constraint>

       <user-data-constraint>

           <transport-guarantee>NONE</transport-guarantee>

           <!-- 設定資料傳送是否需要加密,NONE(不加密)CONFIDENTIAL(加密) -->

       </user-data-constraint>

    </security-constraint>

 

 

    <display-name>SecureWeb</display-name>

    <welcome-file-list>

       <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

</web-app>



<?xml version="1.0" encoding="UTF-8"?>

--><tomcat-users>

  <role rolename="Manager"/>

  <role rolename="Employee"/>

  <user username="user1" password="user1" roles="Manager" /> //代表Manager這個role底下(以此例來說就是dir1底下)的都需要先登入,登入帳號密碼:user1

  <user username="user2" password="user2" roles="Manager" />

  <user username="user3" password="user3" roles="Employee" />

</tomcat-users>


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Login</title>

</head>

<body>

<h3>Login</h3>

 

<form action="j_security_check" method="get">

<table>

    <tr>

       <td>ID : </td>

       <td><input type="text" name="j_username"></td>

       <td></td>

    </tr>

    <tr>

       <td>PWD : </td>

       <td><input type="text" name="j_password"></td>

       <td></td>

    </tr>

    <tr>

       <td> </td>

       <td align="right"><input type="submit" value="Login"></td>

    </tr>

</table>

</form>

 

</body>

</html>



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Error</title>

</head>

<body>

 

<h3>You shall not pass!</h3>

 

</body>

</html>

arrow
arrow
    全站熱搜

    乙方 發表在 痞客邦 留言(0) 人氣()