Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JDBCRealm时会发生异常_Java_Tomcat_Servlets_Jakarta Ee - Fatal编程技术网

Java 使用JDBCRealm时会发生异常

Java 使用JDBCRealm时会发生异常,java,tomcat,servlets,jakarta-ee,Java,Tomcat,Servlets,Jakarta Ee,如何调试由request.login()方法引起的javax.servlet.ServletException Login.java protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub PrintWriter out = res

如何调试由
request.login()方法引起的
javax.servlet.ServletException
Login.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    PrintWriter out = response.getWriter();
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    try {       
        request.login(username, password); // Exceptions happen.
        out.println("logged in");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
tomcat server.xml

  <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>


<!-- added by myself>
    <Context>
      <Realm className="org.apache.catalina.realm.JDBCRealm" 
          connectionName="root" 
          connectionPassword="password" 
          connectionURL="jdbc:mysql://localhost/forum?autoReconnectForPools=true&amp;characterEncoding=UTF-8" 
          digest="MD5" 
          driverName="com.mysql.jdbc.Driver" 
          roleNameCol="role_name" 
          userCredCol="user_pass"
          userNameCol="user_name"
          userRoleTable="user_roles"
          userTable="users" />
     </Context>


您将获得登录失败,因为您用于在数据库中创建哈希密码的方法与Tomcat
JDBCRealm
使用的方法不同

您不应该使用MySql
MD5
函数,而应该使用Tomcat提供的
digest
工具(在Tomcat安装的
bin
目录中)生成散列密码

例如(密码为“1”):

我希望这个结果(
$1$c4ca4238a0…
)与您将在数据库表中观察到的结果不同


此外,根据,您应该消化值
{username}:{realm}:{cleartext password}
,而不仅仅是
{cleartext password}
,并将其存储在数据库中。
{realm}
值来自web.xml文件中的

在登录之前创建会话。代码:if(request.getUserPrincipal()==null){request.getSession();//创建会话请求.login(用户名、密码);}如何填充数据库中的密码列user_pass?它应该是MD5哈希的it@Steve我使用
将值('1',MD5('1')插入到用户(用户名,用户密码)中。只是为了测试。@Jay Smith你的意思是注释掉行
request.login(用户名、密码)?如果是,如何测试用户名和密码是否正确?
request.getUserPrincipal()
在我的程序中返回null。不要对这一行进行注释。如果
request.getUserPrincipal()
返回null,则没有经过身份验证的用户,因此您可以创建会话并进行登录。您可以在创建的会话对象中存储经过身份验证的用户是的,它不同。每次我使用密码“1”运行摘要命令时,值都不同。那么如何存储密码呢?似乎需要指定
salt
长度
0
@Steve C我使用的是tomcat-7.0.78。根据doc,我使用RealmBase.Digest(“1:Authentication required:1”、“MD5”、“UTF-8”)
的变量来测试密码为“1”的用户“1”,并将其存储在数据库中。但同样的例外仍然存在。
[steve@Steves-MacBook-Pro apache-tomcat-8.0.18]$ bin/digest.sh -s 0 -a MD5 1
1:$1$c4ca4238a0b923820dcc509a6f75849b