groovy中的Kerberos身份验证

时间:2017-12-19 23:22:42

标签: java groovy kerberos

我想在groovy中使用Kerberos身份验证。我有一个适合我的简单java程序,这里是代码:

import java.io.*;
import java.net.*;

public class RunHttpSpnego {

    static final String kuser = "host/myaccount"; 
    static final String kpass = "password"; 

    static class MyAuthenticator extends Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            return (new PasswordAuthentication(kuser, kpass.toCharArray()));
        }
    }

    public static void main(String[] args) throws Exception {
        Authenticator.setDefault(new MyAuthenticator());
        URL url = new URL("http://myhost/wsapi/subscriptions");

        InputStream ins = url.openConnection().getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
        String str;
        while((str = reader.readLine()) != null)
            System.out.println(str);
    }
}

加上一个像这样的配置文件:

com.sun.security.jgss.krb5.initiate {
   com.sun.security.auth.module.Krb5LoginModule 
        required useTicketCache=true;
};

然后像这样运行:

java -Djava.security.auth.login.config=/pathtoconf/spnegoLogin.conf RunHttpSpnego

所以我在groovy中写了类似的东西,但它不起作用。

代码:

import java.io.*;
import java.net.*;

def kuser = "username"; 
def kpass = "password"; 

dd = new DataDict();
dd.init();

public class DataDict {

static class MyAuthenticator extends Authenticator {
    public PasswordAuthentication getPasswordAuthentication() {
        System.err.println("Feeding username and password for "
           + getRequestingScheme());
        return (new PasswordAuthentication(kuser, kpass.toCharArray()));
    }
}

void init(String propertyPath, List options){

Authenticator.setDefault(new MyAuthenticator());

URL url = new URL("http://myhost/wsapi/subscriptions");

InputStream ins = url.openConnection().getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String str;
while((str = reader.readLine()) != null)
System.out.println(str);

}
}

我按照这样运行:

groovy -Djava.security.auth.login.config=/pathtoconf/spnegoLogin.conf PopulateDataDict.gy 

我也试过把它放在JAVA_OPTS中,但我仍然得到401.我检查过,getPasswordAuthentication()在groovy中被调用,但它看起来不像它有效。

为什么java代码有效,groovy没有,我需要更改什么才能使其正常工作?

编辑:好的,这是我的愚蠢错误,需要制作用户/密码的静态变量。

0 个答案:

没有答案