JDBC 使用正确凭据时领域身份验证 HTTP 状态 403(使用错误凭据转到错误页面)

JDBC Realm Authentication HTTP Status 403 when using correct credentials (Goes to error page with bad credentials)

我正在尝试使用 JDBC Realms 为我的站点获取身份验证,但我收到 HTTP 状态 403 - 当我使用正确的凭据时,对请求的资源的访问已被拒绝。当我使用不正确的凭据时,它会像它应该的那样进入错误页面。我能够查询数据库并取回响应,因此问题不在于 java 连接器。此外,如果我更改 table 的名称,我会收到一条错误消息,指出 table 不存在,因此它似乎可以很好地连接到数据库并查询它,因为它知道用户名和密码何时不正确并将我发送到错误页面。问题是当我使用正确的 username/password 时。我正在使用 tomcat 7.0.61。希望有足够的信息来找出问题所在。

我的数据库叫做 brew4me。

人 table 具有以下属性:

| email|name|city |password|state_providence|country|

user_roles table 具有以下属性:

|email|role_name|

user_roles table 的示例行是

email@example.com|user

还有 tables 的 email=email。

server.xml 文件中的我的领域:

<Realm className="org.apache.catalina.realm.JDBCRealm" 
          connectionURL="jdbc:mysql://localhost:3306/brew4me" 
          connectionName="root" connectionPassword="#cs564brew4me"
          driverName="com.mysql.jdbc.Driver" 
          roleNameCol="email" 
          userCredCol="password" 
          userNameCol="email" 
          userRoleTable="user_roles" 
          userTable="person"/>

我在web.xml中的配置:

<security-role>
    <role-name>user</role-name>
</security-role>

<security-constraint>
    <display-name>HomepageConstraint</display-name>
    <web-resource-collection>
        <web-resource-name>brew4me</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>user</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>FORM</auth-method>
    <form-login-config>
        <form-login-page>/login.html</form-login-page>
        <form-error-page>/error.html</form-error-page>
    </form-login-config>
</login-config>

这是我的登录页面的代码:

<form method=post action="j_security_check" >
    Email:<input type="text"  name= "j_username" >
    Password:<input type="password"  name= "j_password" >
    <input type="submit" value="Login">
</form>

控制台输出(如果有帮助):

 Apr 28, 2015 12:05:18 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:brew4me' did not find a matching property.
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/7.0.61
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Mar 27 2015 12:03:56 UTC
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         7.0.61.0
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 7
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.1
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jre1.8.0_40
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_40-b26
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\apache-tomcat-7.0.61
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\apache-tomcat-7.0.61
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\apache-tomcat-7.0.61
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\apache-tomcat-7.0.61
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\apache-tomcat-7.0.61\wtpwebapps
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\apache-tomcat-7.0.61\endorsed
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Apr 28, 2015 12:05:18 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_40\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_40/bin/server;C:/Program Files/Java/jre1.8.0_40/bin;C:/Program Files/Java/jre1.8.0_40/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\Common Files\Ulead Systems\MPEG;C:\SWTOOLS\ReadyApps;C:\Program Files (x86)\Common Files\Lenovo;C:\Program Files (x86)\Common Files\lenovo\easyplussdk\bin;C:\Modeltech_pe_edu_10.3c\win32pe_edu;C:\Program Files (x86)\Google\google_appengine\;C:\eclipse;;.
Apr 28, 2015 12:05:18 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Apr 28, 2015 12:05:18 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Apr 28, 2015 12:05:18 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1203 ms
Apr 28, 2015 12:05:18 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Apr 28, 2015 12:05:18 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.61
Apr 28, 2015 12:05:20 AM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [193] milliseconds.
Apr 28, 2015 12:05:20 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.61\webapps\docs
Apr 28, 2015 12:05:20 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-7.0.61\webapps\docs has finished in 264 ms
Apr 28, 2015 12:05:20 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.61\webapps\examples
Apr 28, 2015 12:05:21 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Apr 28, 2015 12:05:21 AM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Apr 28, 2015 12:05:21 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@115992e1')
Apr 28, 2015 12:05:21 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-7.0.61\webapps\examples has finished in 744 ms
Apr 28, 2015 12:05:21 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.61\webapps\host-manager
Apr 28, 2015 12:05:21 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-7.0.61\webapps\host-manager has finished in 217 ms
Apr 28, 2015 12:05:21 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.61\webapps\manager
Apr 28, 2015 12:05:21 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-7.0.61\webapps\manager has finished in 223 ms
Apr 28, 2015 12:05:21 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.61\webapps\ROOT
Apr 28, 2015 12:05:21 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-7.0.61\webapps\ROOT has finished in 133 ms
Apr 28, 2015 12:05:21 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Apr 28, 2015 12:05:21 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Apr 28, 2015 12:05:21 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3331 ms

在您的 Realm 中,您错误地定义了 roleNameCol 值。

尝试将其设置为 roleNameCol="role_name"