![](/img/trans.png)
[英]Authentication failure for Iam Authentication on Postgres Cloud SQL through jdbc Hikari
[英]Troubleshooting in JDBC/Postgres Authentication Example
我試圖自學Java Servlet和JSP,但在使用Tomcat 7和Postgres 9.1進行身份驗證時遇到問題。
它似乎沒有錯誤(Tomcat不會在其日志文件中拋出任何JAVA錯誤)並且可以正常工作,但是它丟失了一些東西,因為它永遠不會讓我進行身份驗證。 幾乎用戶名和密碼與postgres中我的表空間中的內容不匹配。
有沒有一種方法可以在我的代碼中引入額外的日志記錄,以便可以查看正在查詢的內容和返回的內容以及不匹配的原因。 這肯定會幫助我進行故障排除。
供您參考,我附上了context.xml(META_INF),web.xml(WEB_INF)和登錄html)
我感謝您的幫助
<?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>
好的,所以我解決了我的問題,考慮到似乎經常出現這種情況,我想我會為以后偶然發現它的人們回答。
我上面遇到的問題是由於用戶名/密碼表和角色名表中用戶名的列名不同。 SQL已為其分配了“用戶”而不是用戶。
值得注意的是,如果未在web.xml中正確定義角色,則用戶名和密碼正確時將顯示403錯誤。
希望能有所幫助
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.