我已将应用程序配置为使用基于表单的身份验证,并在server.xml中设置所需的设置。
当我尝试访问受保护的页面时,我被正确地重定向到登录页面。在登录页面上,我提供了正确的用户ID和密码,但它没有登录,而是显示登录错误页面。
我正在使用Eclipse在Tomcat上运行项目以及Mac OS X上的MySQL数据库。
提前致谢。
答案 0 :(得分:4)
最后得到了这个工作!
当我使用eclipse部署我的应用程序时,eclipse添加了一个名为Servers的项目,其中包含server.xml,当使用eclipse启动tomcat时,它实际上由tomcat使用。
因此,解决方案是在eclipse中的Servers项目中将域更改为server.xml。
感谢各位的帮助和支持。
祝你好运, Zaheer这样
答案 1 :(得分:2)
以下是定义web.xml中资源安全性和基于表单的身份验证声明的部分。
<security-constraint>
<web-resource-collection>
<web-resource-name>profile</web-resource-name>
<url-pattern>/myProfile</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>member</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/signin.jsp</form-login-page>
<form-error-page>/signin_error.jsp</form-error-page>
</form-login-config>
</login-config>
<security-role><role-name>member</role-name></security-role>
这是server.xml中的领域定义。
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/dbname?user=root&password=root"
userTable="users" userNameCol="email" userCredCol="password"
userRoleTable="user_roles" roleNameCol="role_name"/>
另外请注意我在tomcat的lib文件夹中包含了所需的mysql jar文件。
这是我的登录表单。
<form class="form" id="login_form" action="j_security_check" method="post">
<input class="element" id="element_1" style="WIDTH: 255px" maxlength="200" name="j_username"/>
<input class="element" id="element_2" style="WIDTH: 255px" type="password" maxlength="200" name="j_password"/>
</form>
感谢各位帮助我的人,我真的被这个困住了!
答案 2 :(得分:0)
您的配置对我来说是正确的。两个可能的问题:
您是否将应用程序部署到非root上下文?如果是这样,您可能希望将表单操作更改为/j_security_check
。你可能还是想尝试一下,实际上 - 我记得有些Tomcat版本对此非常挑剔。
您确定users(email, password)
和user_roles(email, role_name)
表中包含适当的行,并且Realm配置中指定的用户可以访问它们吗?我知道你说过你这么做,但这是唯一可能出错的其他事情,所以仔细检查也不会有什么坏处。
如果上述两种方法都没有帮助,我唯一可以建议您下载Tomcat源代码并在Eclipse下运行时逐步执行它。对于Tomcat 6,您需要在org.apache.catalina.realm.JDBCRealm.authenticate(String username, String credentials)
中设置断点(第341行,尽管我可能没有最新的源代码)并逐步执行open()
和第二authenticate()
方法。