【问题标题】:basic authentication does not work even for correct credentials即使是正确的凭据,基本身份验证也不起作用
【发布时间】:2013-04-23 00:59:41
【问题描述】:

我正在尝试配置 Tomcat 7 JDBC 领域配置。 我完全按照本教程进行操作: http://www.avajava.com/tutorials/lessons/how-do-i-use-a-jdbc-realm-with-tomcat-and-mysql.html

我收到基本身份验证弹出窗口,但即使我输入正确的凭据,用户也未通过身份验证。 我没有收到任何错误消息。

教程指定 Tomcat 5.5,但我使用的是 Tomcat 7。 我刚刚更改了connectionPaswordconnectionName 以及动态Web 项目的名称。

这里是server.xml JDBC 领域配置

    <Realm  className="org.apache.catalina.realm.JDBCRealm"
            driverName="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/tomcat_realm"
            connectionName="root" 
            connectionPassword="root"
            userTable="tomcat_users" 
            userNameCol="user_name" 
            userCredCol="password"
            userRoleTable="tomcat_users_roles" 
            roleNameCol="role_name" />

这里是web.xml

<servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/test</url-pattern>
</servlet-mapping>

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Wildcard means whole app requires authentication</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>dude</role-name>
    </auth-constraint>

    <user-data-constraint>
        <!-- transport-guarantee can be CONFIDENTIAL, INTEGRAL, or NONE -->
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>

<login-config>
    <auth-method>BASIC</auth-method>
</login-config>

我只能看到,我收到了这条关于安全的消息:

Security role name dude used in an <auth-constraint> without being defined in a <security-role>

你能帮我解决这个问题吗?这个问题与 Tomcat 7 有关吗?

【问题讨论】:

    标签: tomcat7 web.xml jaas jdbcrealm


    【解决方案1】:

    根据Java Servlet Spec,您需要将dude 角色定义为安全角色。为此,请将&lt;security-role&gt; 元素添加到您的web.xml,如下所示:

    <servlet>
    <!-- ... -->
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Wildcard means whole app requires authentication</web-resource-name>
            <url-pattern>/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>dude</role-name>
        </auth-constraint>
    <!-- ... -->
    </security-constraint>
    
    <login-config>
         <auth-method>BASIC</auth-method>
    </login-config>
    
    <security-role>
        <role-name>dude</role-name>
    </security-role>
    

    这将允许GET/POST 向任何具有dude 角色的用户发出请求。

    我建议您不要包含 &lt;http-method&gt; 元素,因为它们不会像您预期的那样工作。为GETPOST 包含此元素意味着安全约束仅适用于这两种方法;任何其他方法都是允许的。这是 Servlet 规范所说的:

    子元素 web-resource-collection 标识 Web 应用程序中应用安全约束的资源和这些资源上的 HTTP 方法的子集。

    详情请见this reference

    【讨论】:

    • 感谢您提供有用的信息,但它似乎不起作用。我遇到同样的问题。即使我输入正确的凭据,用户也没有获得身份验证。另外,我没有看到任何登录tomcat。如何在tomcat中生成日志?
    • 您是否仍然看到您提到的原始错误:角色名称 dude 未在 中定义,还是另一个?
    • 感谢帮助的人。现在我不再收到该消息了。起初,即使您提出更改建议,它也不起作用,但后来,我更换了 tomcat 并再次配置,然后它就开始工作了。感谢您的回答,这很有帮助:)我会在获得特权后立即投票:)
    • 很高兴听到这对您有用。我鼓励您也考虑从安全约束中消除 GET 和 POST 方法,因为包含它们意味着安全约束仅适用于这两种方法,因此默认情况下,可能会启用其他方法并且不需要用户进行身份验证.
    • Federico Raggi 是的,我有想法,我只是想实现教程中的 tomcat 7 jaas 示例,并且在我的实际项目中对 http-method 进行了必要的更改。再次感谢您的帮助:) 非常感谢
    猜你喜欢
    • 2016-08-26
    • 2014-05-21
    • 1970-01-01
    • 2021-07-09
    • 2016-01-10
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 2014-09-06
    相关资源
    最近更新 更多