从Java/Windows7连接到带有Kerberos的Postgres数据库
我到处寻找并询问大量人,但目前为止没有人能够帮助我。我试图通过Java(8)应用程序从我的Windows(7)笔记本电脑连接到远程计算机上的postgres(9.6)数据库。我们使用Kerberos来保护访问,但我拥有一个有效的Kerberos帐户,并且可以通过de Ticket Manager创建票据。我也可以登录到需要Kerberos身份验证的其他“服务”,尽管不是通过java而是通过浏览器。从Java/Windows7连接到带有Kerberos的Postgres数据库
但无论我尝试,我无法让我的java程序工作。这里就是我的了:
krb5.ini
[libdefaults]
default_realm = <domain>
forwardable = true
kdc_timesync = 1
ccache_type = 4
proxiable = true
dns_lookup_kdc = true
dns_lookup_realm = true
[realms]
<domain>.NET = {
admin_server = <domain-server>
default_domain = <domain>
}
[domain_realm]
.<domain> = <domain>
<domain> = <domain>
.local.nl.<company>.com = <domain>
local.nl.<company>.com = <domain>
[login]
krb4_convert = true
krb4_get_tickets = false
的Jaas.conf:
pgjdbc {
com.sun.security.auth.module.Krb5LoginModule required
refreshKrb5Config=true
doNotPrompt=false
useTicketCache=false
renewTGT=false
useKeyTab=true
keyTab="<location>/<filename>.keytab"
debug=true
client=true
principal="<username>@<domain>";
};
.keytab文件
public class KerberosPostgresClient {
static {
System.setProperty("java.security.krb5.conf","c:/tmp/krb5.ini");
System.setProperty("java.security.krb5.realm","<domain>");
System.setProperty("java.security.krb5.kdc","<domain>");
System.setProperty("javax.security.auth.useSubjectCredsOnly","false");
System.setProperty("java.security.auth.login.config","c:/tmp/jaas.conf"); }
@Test
public void test() throws Exception {
String url = "jdbc:postgresql://<hostname>:<port>/<database>";
Properties properties = new Properties();
properties.setProperty("JAASConfigName", "pgjdbc");
try (Connection conn = DriverManager.getConnection(url, connInfo)) {
conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
}
很简单的java代码可以找到keytab,jaas.conf。我在另一台机器上创建了keytab文件,但使用相同的主体和密码。
当我运行该程序,我看到:
Debug is true storeKey false useTicketCache false useKeyTab true doNotPrompt false ticketCache is null isInitiator true KeyTab is c:/tmp/<username>.keytab refreshKrb5Config is true principal is <username>@<domain> tryFirstPass is false useFirstPass is false storePass is false clearPass is false
Refreshing Kerberos configuration
过了片刻我得到一个异常:
[Krb5LoginModule] authentication failed
Receive timed out
org.postgresql.util.PSQLException: GSS Authentication failed
at org.postgresql.gss.MakeGSS.authenticate(MakeGSS.java:65)
....
Caused by: java.net.SocketTimeoutException: Receive timed out
at java.net.DualStackPlainDatagramSocketImpl.socketReceiveOrPeekData(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.receive0(DualStackPlainDatagramSocketImpl.java:120)
at java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:144)
at java.net.DatagramSocket.receive(DatagramSocket.java:812)
at sun.security.krb5.internal.UDPClient.receive(NetClient.java:206)
at sun.security.krb5.KdcComm$KdcCommunication.run(KdcComm.java:411)
at sun.security.krb5.KdcComm$KdcCommunication.run(KdcComm.java:364)
at java.security.AccessController.doPrivileged(Native Method)
at sun.security.krb5.KdcComm.send(KdcComm.java:348)
at sun.security.krb5.KdcComm.sendIfPossible(KdcComm.java:253)
at sun.security.krb5.KdcComm.send(KdcComm.java:229)
at sun.security.krb5.KdcComm.send(KdcComm.java:200)
at sun.security.krb5.KrbAsReqBuilder.send(KrbAsReqBuilder.java:316)
at sun.security.krb5.KrbAsReqBuilder.action(KrbAsReqBuilder.java:361)
at com.sun.security.auth.module.Krb5LoginModule.attemptAuthentication(Krb5LoginModule.java:776)
... 45 more
经常拿这表明它找不到其他异常keytab文件,但与上述设置似乎工作。我也可以从我的机器上ping postgres数据库。
我发现:Error connecting to PostgreSQL 9.4 with MIT Kerberos via JDBC vs CLI但一直没有解决
我终于得到它在我的Jaas.conf以下设置工作:
pgjdbc {
com.sun.security.auth.module.Krb5LoginModule required
refreshKrb5Config=true
doNotPrompt=true
useTicketCache=true
renewTGT=true
useKeyTab=true
keyTab="c:/<locationto>/<user>.keytab"
debug=true
client=true
principal="<user>@<domain>";
};
即doNotPrompt,useTicketCache,renewTGT的组合终于得到了它的工作
伟大的工作。请自己接受你的答案。 –
当我看到在收到我首先想到的是防火墙的问题套接字超时。 –
嗨,约翰,谢谢你的输入。我记得有人提到,stacktrace意味着它使用UDP并且可能被防火墙阻止(?)。如果是这样的话,我该如何设置它使用TCP/IP – mroosendaal
更新:添加“udp_preference_limit = 1”到krb5.ini来使用TCP,但现在我得到“连接超时”,所以我认为你是对的因为这是一个防火墙问题。奇怪的是,其他人,尽管他们使用Mac可以连接到数据库。我会与DBA的人员核对。 – mroosendaal