Simple Kerberos client in Java?

后端 未结 9 600
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 06:47

Applications such a Google\'s Chrome and IE can transparently handle Kerberos authentication; however I can not find a \"simple\" Java solution to match this transparency. A

9条回答
  •  鱼传尺愫
    2020-12-02 07:22

    Adding to David Roussels answer on url specific http based kerberos authentication:-

    The reason why your code works is because your target SPN(server side principal) is configured to with HTTP/serverhostname.realm.com@DOMAIN.COM. In that case it will work because you are not explicitly setting the token. URLConnection internally sets a token with that SPN

    1 Perform steps(from my previous answer) to get a subject

    2 Use gss api init sec context to generate a context token. There are numerous tutorials out there for this step

    3 Base 64 encode the token

    4 Attach the token to urlconnection:-

    URL url = new URL("http://myhost/myapp")
    HttpURLConnection urlConn = (HttpURLConnection)url.openConnection(); = 
    urlConn.setRequestProperty("Authorization", "Negotiate " + encodedToken);
    

    5 Implement a priviledged action:-

    //this internally calls the getInputStream
    public class PrivilegedGetInputStream implements PrivilegedExceptionAction
    

    6 Wrap the whole thing in Subject.doAs

    //use prev answer instructions to get subject
    Subject.doAs(subject, new PrivilegedGetInputStream(urlConnection)
    

提交回复
热议问题