简体   繁体   English

JDBC / Postgres身份验证示例中的故障排除

[英]Troubleshooting in JDBC/Postgres Authentication Example

I am trying to teach myself Java Servlets and JSP, and I am having an issue with authenticating with Tomcat 7 and Postgres 9.1. 我试图自学Java Servlet和JSP,但在使用Tomcat 7和Postgres 9.1进行身份验证时遇到问题。

It seems to be error free (Tomcat is not throwing any JAVA errors in its log file) and working however it is missing something because it never lets me authenticate. 它似乎没有错误(Tomcat不会在其日志文件中抛出任何JAVA错误)并且可以正常工作,但是它丢失了一些东西,因为它永远不会让我进行身份验证。 Almost like the username and password does not match whats in my table spaces within postgres. 几乎用户名和密码与postgres中我的表空间中的内容不匹配。

Is there a way to introduce extra logging into my code so that I can see what is being queried and what is being returned and why there is a mismatch. 有没有一种方法可以在我的代码中引入额外的日志记录,以便可以查看正在查询的内容和返回的内容以及不匹配的原因。 This will certainly help my troubleshooting efforts. 这肯定会帮助我进行故障排除。

For your information I am attaching my context.xml (META_INF), web.xml (WEB_INF) and my login html) 供您参考,我附上了context.xml(META_INF),web.xml(WEB_INF)和登录html)

I appreciate your help 我感谢您的帮助

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">

    <!-- Define two security roles -->
    <security-role>
        <description>customer service testers</description>
        <role-name>testing</role-name>
    </security-role>
    <security-role>
        <description>system developers</description>
        <role-name>developer</role-name>
    </security-role>

    <!-- Restrict access to all files in the /admin folder -->
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Protected Area</web-resource-name>
            <url-pattern>/admin/*</url-pattern>
        </web-resource-collection>
        <!-- Authorize the programmer and service roles -->
        <auth-constraint>
            <role-name>developer</role-name>
            <role-name>testing</role-name>
        </auth-constraint>
    </security-constraint>

    <!-- Use form-based authentication -->
    <!--<login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/admin/login.html</form-login-page>
            <form-error-page>/admin/login_error.html</form-error-page>
        </form-login-config>
    </login-config>
    --> 

    <!-- Use basic authentication -->

    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>Admin Login</realm-name>
    </login-config>


     <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>



<?xml version="1.0" encoding="UTF-8"?>
<Context path="/AUTHExample">
    <Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
          driverName="org.postgresql.Driver"
          connectionURL="jdbc:postgresql://localhost:5432/mydb"
          connectionName="postgres" connectionPassword="postgres"
          userTable="userpass" userNameCol="user" userCredCol="passwd"
          userRoleTable="userrole" roleNameCol="rolename" 
          />

</Context>




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>Learning to Authenticate</title>
</head>

<body>
<h1>Admin Login Form</h1>
<p>Please enter your username and password to continue.</p>
<table cellspacing="5" border="0">
  <form action="j_security_check" method="get">
    <tr>
        <td align="right">Username</td>
        <td><input type="text" name="j_username"></td>
    </tr>
    <tr>
        <td align="right">Password</td>
        <td><input type="password" name="j_password"></td>
    </tr>
    <tr>
      <td><input type="submit" value="Login"></td>
    </tr>
  </form>
</table>
</body>
</html>

Ok, so I solved my problem and considering how often this seems to come up I thought I would answer it for future people who stumble across it. 好的,所以我解决了我的问题,考虑到似乎经常出现这种情况,我想我会为以后偶然发现它的人们回答。

The problem I was having above was due to different column names for username in the user/password table and the rolename table. 我上面遇到的问题是由于用户名/密码表和角色名表中用户名的列名不同。 SQL had assigned it "user" instead of user. SQL已为其分配了“用户”而不是用户。

Of note, if the role isnt defined properly in the web.xml, a 403 error displays when the username and password is correct. 值得注意的是,如果未在web.xml中正确定义角色,则用户名和密码正确时将显示403错误。

Hope that was a help 希望能有所帮助

Thank you 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM