zoukankan      html  css  js  c++  java
  • 在Tomcat中采用基于表单的安全验证

    1、概述

     

    (1)基于表单的验证

    基于From的安全认证可以通过TomcatServer对Form表单中所提供的数据进行验证,基于表单的验证使系统开发者可以自定义用户的登陆页面和报错页面。这种验证方法与基本HTTP的验证方法的唯一区别就在于它可以根据用户的要求制定登陆和出错页面。

    通过拦截并检查用户的请求,检查用户是否在应用系统中已经创建好login session。如果没有,则将用户转向到认证服务的登录页面。但在Tomcat中的基于表单的验证凭证不被保护并以纯文本发送。

     

    (2)在Tomcat 中的实现

    在Tomcat中,用户、用户组和角色都是在XML配置文件(C:jakarta-tomcat-5.0.19conf omcat-users.xml)中指定的,我们只需要提供一个登陆页面,包含一个名为j_security_check的Form表单,一个名为j_username的TextBox和一个名为j_password的PasswordBox,然后在/WEB-INF/web.xml中配置即可使用Tomcat默认的JAAS身份验证。

    使用JAAS验证的好处是,验证逻辑从页面中分离,对页面的限制访问是通过/WEB-INF/web.xml中的配置指定的,无需自定义过滤器。

     

    (3)为了实现Web应用程序的安全,Tomcat Web容器执行下面的步骤:

    l  在受保护的Web资源被访问时,判断用户是否被认证。

    l  如果用户没有得到认证,则通过重定向到部署描述符中定义的注册页面,要求用户提供安全信任状。

    l  根据为该容器配置的安全领域,确认用户的信任状有效。

    l  判断得到认证的用户是否被授权访问部署描述符(web.xml)中定义的Web资源。

     

     

    2、设计步骤

     

     

    (1)编写登录页面和错误处理页面:请见FormSafeWebApp 程序中的页面

     

     

    (2)登录的页面文件的内容如下

    基于FORM的用户认证要求你返回一个包括用户名和密码的HTML表单,这个表单相对应与用户名和密码的元素必须是j_username和j_password,并且表单的action描述必须为j_security_check(其实是一个Servlet)。该表单的具体操作以及j_username和j_password名字在Servlet中定义。当这个表单到达服务器的时候,由内部的Tomcat Server安全区对它进行确认。

    包括这个表单的资源可以是一个HTML页面、一个JSP页面或者一个Servlet。你可以在<form-login-page>元素中定义。基于表单的认证能够使开发人员定制认证的用户界面。在web.xml的login-config标签项目定义了认证机制的类型、登录的URI和错误页面。

    下面为该页面的内容:

    <%@ page contentType="text/html;charset=GBK" %>
    <html><head><title>在Tomcat 中采用Form 验证方式实现的安全Web应用程序的登录页</title>
    </head><bodybgcolor="#ffffff">
    <form method="post"name="Login" action="j_security_check">
    <table width="500" border="1"align="center"> <tr>
        <td colspan="2"> <divalign="center"><strong>在Tomcat中采用</strong><strong>基于表单的安全验证的登录表单</strong> </div></td> </tr>
      <tr><tdwidth="224"><div align="right">用户名称:</div></td>
        <td width="260"><inputtype="text" name="j_username"></td></tr>
      <tr><td><div align="right">密码:</div></td>
    <td><inputtype="password" name="j_password"></td>
      </tr>
     <tr><td><div align="right"><inputtype="submit" name="Submit" value="提交">
       </div></td> <td><input type="reset"name="Submit2" value="重置"></td>
     </tr></table></form></body></html>


     

    (3)修改web.xml文件

    <?xml version="1.0"encoding="UTF-8"?>
    <!DOCTYPEweb-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
           <welcome-file-list>
              <welcome-file>index.jsp</welcome-file>
            </welcome-file-list>
     <!--Security is active on entire directory -->
      <security-constraint>
        <display-name>Tomcat Server FormSecurity Constraint</display-name>
        <web-resource-collection>
         <web-resource-name>ProtectedArea</web-resource-name>
          <description>A Page of LoginSuccess</description>
          <url-pattern>/ProtectedDirOne/index.jsp</url-pattern>
          </web-resource-collection>
        <auth-constraint>
          <!-- Anyone with one of the listedroles may access this area -->
          <role-name>admin</role-name>
        </auth-constraint>
     </security-constraint>
      <!-- Login configuration uses form-basedauthentication -->
      <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>Tomcat ServerConfiguration Form-Based Authentication Area</realm-name>
        <form-login-config>
                 <form-login-page>/login.jsp</form-login-page>
                  <form-error-page>/Error.htm</form-error-page>
     
        </form-login-config>
      </login-config>
      <!-- Security roles referenced by this webapplication -->
      <security-role>
       <description>
          The role is Administration
        </description>
        <role-name>admin</role-name>
      </security-role>
    </web-app>


     

    (4)在C:jakarta-tomcat-5.0.19conf omcat-users.xml文件中配置admin的角色以及与该 admin角色相匹配的用户名称和密码

     

    (5)执行该页面

    在浏览器中直接输入受保护的页面的URL地址:

    http://127.0.0.1:8080/FormSafeWebApp/ProtectedDirOne/,将出现要求登录的页面。

    在表单中输入用户名称为admin(前面在tomcat-users.xml文件中所设置的某一用户名称),密码为12345678。然后点击“提交”,将出现如下页面


    如果用户名称或者密码输入不正确,将出现如下的页面也就是错误页面


    (6)在页面中获得当前登录成功后的用户名称和实体名称

           利用request对象中的getRemoteUser()方法获得当前登录成功后的用户名称和利用getUserPrincipal()方法获得当前登录成功后的实体名称。
  • 相关阅读:
    微软职位内部推荐-Senior Software Engineer
    微软职位内部推荐-Senior Software Engineer
    微软职位内部推荐-Software Development Engineer
    微软职位内部推荐-Senior Dev Lead
    微软职位内部推荐-Software Engineer II_HPC
    微软职位内部推荐-Senior Software Engineer
    微软职位内部推荐-Senior SW Engineer for Application Ecosystem
    微软职位内部推荐-Senior Software Engineer-Eco
    微软职位内部推荐-Software Development Engineer II
    微软职位内部推荐-Senior Software Lead-Index Gen
  • 原文地址:https://www.cnblogs.com/LinkinPark/p/5233084.html
Copyright © 2011-2022 走看看