KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > archive > authenticate > WindowsAuthentication


1 /*
2  * Created on Jul 10, 2006
3  */

4 package com.openedit.archive.authenticate;
5
6 import java.net.InetAddress JavaDoc;
7
8 import jcifs.UniAddress;
9 import jcifs.smb.NtlmPasswordAuthentication;
10 import jcifs.smb.SmbSession;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 import com.openedit.OpenEditException;
16
17 public class WindowsAuthentication
18 {
19     private static final Log log = LogFactory.getLog(WindowsAuthentication.class);
20
21     public boolean login(String JavaDoc inName, String JavaDoc inPassword, String JavaDoc inDomain ) throws OpenEditException
22     {
23         return login( inName, inPassword, inDomain, null);
24     }
25     public boolean login(String JavaDoc inName, String JavaDoc inPassword, String JavaDoc inDomainOrBlank, String JavaDoc inServer) throws OpenEditException
26     {
27     
28         // http://support.microsoft.com/default.aspx?scid=kb;EN-US;180548
29
if( inDomainOrBlank == null)
30         {
31             inDomainOrBlank = "";
32         }
33         NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(inDomainOrBlank, inName, inPassword);
34         
35         try
36         {
37             InetAddress JavaDoc ip = null;
38             if( inServer == null)
39             {
40                 String JavaDoc address = InetAddress.getLocalHost().getHostAddress(); //Localhost needs to use external IP
41
ip = InetAddress.getByName(address);
42             }
43             else
44             {
45                 ip = InetAddress.getByName(inServer); // ip address of your windows controller
46
}
47             UniAddress controller = new UniAddress(ip);
48             SmbSession.logon(controller, auth);
49             return true;
50         }
51         catch ( Exception JavaDoc ex )
52         {
53             log.error(ex);
54             return false;
55         }
56 /*
57         
58         // Obtain a LoginContext, needed for authentication. Tell it
59         // to use the LoginModule implementation specified by the
60         // entry named "JaasSample" in the JAAS login configuration
61         // file and to also use the specified CallbackHandler.
62         LoginContext lc = null;
63         try
64         {
65             lc = new LoginContext("JaasSample", new TextCallbackHandler());
66             
67             LoginContext loginContext = new LoginContext(
68                 "Sample", new UsernamePasswordCallbackHandler
69                 (username, password));
70             
71             loginContext.login();
72
73 // Now we're logged in, so we can get the current subject.
74             Subject subject = loginContext.getSubject();
75
76 // Display the subject
77             System.out.println(subject);
78
79             
80         }
81         catch (LoginException le)
82         {
83             System.err.println("Cannot create LoginContext. " + le.getMessage());
84             System.exit(-1);
85         }
86         catch (SecurityException se)
87         {
88             System.err.println("Cannot create LoginContext. " + se.getMessage());
89             System.exit(-1);
90         }
91
92         try
93         {
94
95             // attempt authentication
96             lc.login();
97
98         }
99         catch (LoginException le)
100         {
101
102             System.err.println("Authentication failed:");
103             System.err.println(" " + le.getMessage());
104             System.exit(-1);
105
106         }
107
108         System.out.println("Authentication succeeded!");
109 */

110     }
111 }
112 /*
113
114
115 Sample {
116 PasswordLoginModule required;
117 };
118
119 */
Popular Tags