2017-09-22 124 views
3

我到處尋找並詢問大量人,但目前爲止沒有人能夠幫助我。我試圖通過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但一直沒有解決

+0

當我看到在收到我首先想到的是防火牆的問題套接字超時。 –

+0

嗨,約翰,謝謝你的輸入。我記得有人提到,stacktrace意味着它使用UDP並且可能被防火牆阻止(?)。如果是這樣的話,我該如何設置它使用TCP/IP – mroosendaal

+0

更新:添加「udp_preference_limit = 1」到krb5.ini來使用TCP,但現在我得到「連接超時」,所以我認爲你是對的因爲這是一個防火牆問題。奇怪的是,其他人,儘管他們使用Mac可以連接到數據庫。我會與DBA的人員覈對。 – mroosendaal

回答

2

我終於得到它在我的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的組合終於得到了它的工作

+0

偉大的工作。請自己接受你的答案。 –

相關問題