`

Tomcat -- 安全认证 About</security-constraint>

 
阅读更多
做过WEB项目的都知道,一但涉及用户,我们不得不为用户登录写一堆繁杂的验证代码。当然Spring AOP的诞生为我们的权限管理提供了不少的便利。甚至你也以用自己写的Filter(过滤器)来对你的程序进行登录时的验证。今天在这里和大家分享一种更为简便的方法,它就是Java Web的安全验证机制。
      这种机制是对立地WEB的容器之上的,如Tomcat,JBoss等,你只须在你应用中的web.xml文件中做一定的配置就可以完成一个简单的登录验证的功能。确切地说WEB安全验证机制不仅可以让我们完成登录的功能,它更侧重于安全两字。你可以把受保护的资源全部定义在你的资源集里面,这样用户只有在取得了合法的身份验证之后才可以访问,它可以保护  WEB的整个应用、某个子文件夹、甚至于特定的一类文件,这将取决于你对资源集的定义。
      Tomcat容器支持以下四种认证方式:
     1. BASIC认证:这种方式被认为是最不安全的认证,因为它没有提供强烈的加密措施。
     <login-config>
          <auth-method>BASIC</auth-method>
    </login-config
    2. DIGEST认证:相比于BASIC认证,它是种比较安全的认证,它在认证时将请求数据 通过MD5的加密方式进行认证。      <login-config>
         <auth-method>DIGEST</auth-method>
     </login-config>

     3.FORM认证:这是种基础自定义表单的认证,你可以指定登录时的验证表单。
      <login-config>
         <auth-method>FORM</auth-method>
         <form-login-config>
             <!—创建登录表单 -->
             <form-login-pages>/login.htm</form-login-pages>
             <!—创建错误表单 -->
             <form-error-pages>/error.html</form-error-pages>
          </form-login-config>
       </login-config>
    
       以下是login.htm的内容:(注意form的action还有input元素的name,这些是固定的)
       <form action="j_security_check" method="post">
           Username<input type="text" name="j_username" /><br />
           Password<input type="password" name="j_password" /><br />
          <input type="submit" />
       </form>
    4.CLIENT-CERT认证:这是一种基于客户端证书的认证方式,比较安全。但缺陷是在没有安全证书的客户端无法使用。
    <login-config>
         <auth-method>CLIENT-CERT</auth-method>
    </login-config>

     下面介绍一下,配置认证的步骤:
     1.定义角色:这些角色可以是TOMCAT的tomcat-user.xml文件中定义的默认角色及用户,也可以是自己创建的数据库角色与用户表。以下分别介绍:
     1.1 基本TOMCAT已有用户及角色的认证配置:
     tomcat-users.xml中的role元素
    <tomcat-users>
      <role rolename=”Admin”/>
      <role rolename=”Manager”/>
      <user username=”admin”password=”admin” role=”Admin, Manager”/>
    </tomcat-users>
    web.xml文件中的security-role元素
    <security-role>
    <!—该角色须在tomcat-users.xml定义过-->
        <role-name>Admin</role-name>
    </security-role>

    1.2 基本自定义数据库用户表的认证配置:
     如果你想基于自己定义的数据表进行认证,那你得在META-INF文件夹下创建一个名为 context.xml文件,配置如下内容:

    <?xml version="1.0" encoding="UTF-8"?>
    <Context path="/mycms">
    <Realm className="org.apache.catalina.realm.JDBCRealm"
                 connectionName="root"
                 connectionPassword="123456"
                 connectionURL="jdbc:mysql://localhost:3306/mycms"
                 driverName="com.mysql.jdbc.Driver"
                 roleNameCol="rolename"
                 userCredCol="password"
                 userNameCol="username"
                 userRoleTable="mc_userroles"
                 userTable="mc_users"/>
      </Context>
     这里配置了数据库的连接信息以及指定了认证的用户表及角色表。
     2 定义资源/方法约束
     在web.xml文件中定义如元素:
     <security-constraint>
        <display-name>MyCMS</display-name>
        <web-resource-collection>
            <web-resource-name>Protected Area</web-resource-name>
            <!-- Define the context-relative URL(s) to be protected -->
            <url-pattern>/admin/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <!-- Anyone with one of the listed roles may access this area -->
            <role-name>Admin</role-name>
        </auth-constraint>
    </security-constraint>
    <!-- Login configuration uses form-based authentication -->
    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>MyCMS</realm-name>
    </login-config>
   
    <!-- Security roles referenced by this web application -->
    <security-role>
        <description>
            The role that is required to log in to the Administration Application
        </description>
        <role-name>Admin</role-name>
    </security-role>
     至此我们基本TOMCAT的安全认证配置就完成了。至于其它三种方式,配置类似,可以参考2中定义的web.xml文件

分享到:
评论

相关推荐

    启用了不安全的http方法漏洞

    &lt;security-constraint&gt; &lt;web-resource-collection&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;http-method&gt;PUT&lt;/http-method&gt; &lt;http-method&gt;DELETE&lt;/http-method&gt; &lt;http-method&gt;HEAD&lt;/http-method&gt; &lt;http-method&gt;...

    Tomcat 的安全方面设置 简单配置过程 说明

    &lt;security-constraint&gt; &lt;display-name&gt;sessiontestsecruityconstraint&lt;/display-name&gt; &lt;web-resource-collection&gt; &lt;web-resource-name&gt;ProtectedArea&lt;/web-resource-name&gt; &lt;url-pattern&gt;/admin/*&lt;/url-pattern&gt; ...

    Tomcat安全验证机制

    - **作用**:通过`&lt;security-constraint&gt;`元素定义了对特定URL模式的访问限制,只有属于`strutssample`角色的用户才能访问这些页面。`&lt;login-config&gt;`元素则指定了使用表单验证方法,并且指定了登录页面和错误页面...

    基于Tomcat的安全验证机制

    2. **定义受保护的资源**:使用`&lt;security-constraint&gt;`元素定义哪些URL或资源需要认证才能访问。 3. **配置认证和授权**:使用`&lt;login-config&gt;`元素定义认证方法,然后使用`&lt;security-role-ref&gt;`元素将角色映射到...

    keytool+tomcat配置HTTPS双向证书认证

    `&lt;security-constraint&gt; &lt;web-resource-collection&gt; &lt;web-resource-name&gt;SSL&lt;/web-resource-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/web-resource-collection&gt; &lt;user-data-constraint&gt; &lt;transport-guarantee&gt;...

    jboss安全性 jboss设置安全性

    这里的`&lt;security-domain&gt;`标签指定了一个JAAS(Java Authentication and Authorization Service)安全域,它关联了一个特定的登录模块,用于验证用户身份。 ##### 2. 配置 `web.xml` 文件 接下来,还需要配置同...

    tomcat搭建git私服

    注意`&lt;servlet&gt;`和`&lt;security-constraint&gt;`元素的配置。具体如下: ```xml &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi=...

    webService添加basic验证

    2. **添加安全约束**:在`web.xml`中增加`&lt;security-constraint&gt;`元素来定义需要验证的资源。在这个例子中,通过`&lt;url-pattern&gt;`指定所有以`/services/*`开头的URL都必须经过验证。 3. **指定认证方式**:在`&lt;login...

    不安全的http方法、CSRF等漏洞修复问题

    &lt;security-constraint&gt; &lt;web-resource-collection&gt; &lt;web-resource-name&gt;securedapp&lt;/web-resource-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;http-method&gt;DELETE&lt;/http-method&gt; &lt;http-method&gt;HEAD&lt;/http-method&gt; ...

    Web.xml常用元素

    #### &lt;security-constraint&gt; - **作用**:指定哪些URL路径需要认证和授权,以及所使用的认证方式。 - **示例**: ```xml &lt;security-constraint&gt; &lt;web-resource-collection&gt; &lt;web-resource-name&gt;Secure Area&lt;/...

    各中间件禁用不安全的HTTP方法

    &lt;security-constraint&gt; &lt;web-resource-collection&gt; &lt;web-resource-name&gt;test&lt;/web-resource-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;http-method&gt;PUT&lt;/http-method&gt; &lt;http-method&gt;DELETE&lt;/http-method&gt; ...

    jsp web.xml文件的作用及基本配置.docx

    - 通过 `&lt;security-constraint&gt;` 元素配置安全约束。 - 可以定义哪些资源需要认证才能访问。 - 示例代码: ```xml &lt;security-constraint&gt; &lt;web-resource-collection&gt; &lt;web-resource-name&gt;Admin Pages&lt;/web-...

    apache-tomcat-9.0.35-solr7.7.3.rar

    &lt;security-constraint&gt; &lt;web-resource-collection&gt; &lt;web-resource-name&gt;Solr&lt;/web-resource-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/web-resource-collection&gt; &lt;auth-constraint&gt; &lt;role-name&gt;solr-admin&lt;/...

    tomcat文档

    &lt;/security-constraint&gt; ``` **3.2.9 配置安全验证登录界面** 登录界面配置示例: ```xml &lt;login-config&gt; &lt;auth-method&gt;BASIC&lt;/auth-method&gt; &lt;realm-name&gt;My Realm&lt;/realm-name&gt; &lt;/login-config&gt; ``` **3.2.10...

    tomcat-manager的用户名密码配置

    &lt;/security-constraint&gt; &lt;/Connector&gt; ``` 3. **更改默认端口**:为了增加安全性,可以考虑更改Tomcat Manager默认使用的端口。 4. **测试登录**:启动Tomcat服务后,在浏览器中输入`...

    在Tomcat中配置用户和虚拟目录

    一旦配置了用户和角色,你可以在Web应用的`web.xml`中定义受保护的URL模式,使用`&lt;security-constraint&gt;`和`&lt;login-config&gt;`元素: ```xml &lt;security-constraint&gt; &lt;web-resource-collection&gt; &lt;web-resource-name&gt;...

    web.xml详解(web-app_2_3.dtd)

    &lt;/security-constraint&gt; ``` #### 19. `login-config`元素 `login-config`元素用于配置登录机制,包括认证方法和表单登录页。DTD定义如下: ```xml &lt;!ELEMENT login-config (auth-method, form-login-config?)&gt; ...

    tomcat禁止PUT等方法

    &lt;security-constraint&gt; &lt;web-resource-collection&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;http-method&gt;PUT&lt;/http-method&gt; &lt;http-method&gt;DELETE&lt;/http-method&gt; &lt;http-method&gt;HEAD&lt;/http-method&gt; &lt;http-method&gt;...

    web.xml详细说明

    16. **`&lt;security-constraint&gt;`** - **作用**:指定哪些URL或资源需要保护。 - **示例**: ```xml &lt;security-constraint&gt; &lt;web-resource-collection&gt; &lt;web-resource-name&gt;Admin Pages&lt;/web-resource-name&gt; ...

Global site tag (gtag.js) - Google Analytics