import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類 /** * This method validates absoluteName and credential against referral LDAP and returns used user DN. * * <ol> * <li> Parses given absoluteName to URL and DN * <li> creates initial LDAP context of referral LDAP to validate credential * <li> closes the initial context * </ol> * * It uses all options from login module setup except of ProviderURL. * * @param userDN - userDN which has to be used instead of parsed absoluteName (if is null, use absoluteName) - value is gained using distinguishedNameAttribute * @param absoluteName - absolute user DN * @param credential * @return used user DN for validation * @throws NamingException */ private String bindDNReferralAuthentication(final String userDN, String absoluteName, Object credential) throws NamingException { URI uri; try { uri = new URI(absoluteName); } catch (URISyntaxException e) { throw PicketBoxMessages.MESSAGES.unableToParseReferralAbsoluteName(e, absoluteName); } String name = (userDN != null ? userDN : uri.getPath().substring(1)); String namingProviderURL = uri.getScheme() + "://" + uri.getAuthority(); Properties refEnv = constructLdapContextEnvironment(namingProviderURL, name, credential); InitialLdapContext refCtx = new InitialLdapContext(refEnv, null); refCtx.close(); return name; }
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類 /** * <p>Test method for 'javax.naming.ldap.ControlFactory.getControlInstance(Control)'</p> * <p>Here we are going to test if we can get an instance with the controls sended.</p> */ public void testGetControlInstanceControl() { try { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.harmony.jndi.tests.javax.naming.spi.mock.ldap.MockContextFactory"); MockControl[] cs = { new MockControl("c1", false, new byte[] { 1, 2, 3, 4 }), new MockControl("c1", true, new byte[] { 'a', 'b', 'c', 'd' }), }; MockControl cs2 = new MockControl("c1", false, new byte[] { 1, 2, 3, 4 }); InitialLdapContext ilc=new InitialLdapContext(env, cs); assertEquals(cs2,ControlFactory.getControlInstance(cs2,ilc,env)); } catch (NamingException e) { } }
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類 /** * <p> * Test method for * 'javax.naming.ldap.InitialLdapContext.reconnect(Control[])' * </p> * <p> * Here we are testing if this method correctly reconnects to the LDAP * server. In this case we are using a different set of controls for the * reconnection. * </p> * <p> * The expected result is a reconection with the new set of controls. * </p> */ public void testReconnect003() throws Exception { System .setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.harmony.jndi.tests.javax.naming.spi.mock.ldap.MockContextFactory"); Control[] cs = { new MockControl("c1", false, new byte[] { 1, 2, 3, 4 }), new MockControl("c1", true, new byte[] { 'a', 'b', 'c', 'd' }), }; Control[] cs2 = { new MockControl("c2", false, new byte[] { 1, 2, 3, 4 }), new MockControl("c2", true, new byte[] { 'a', 'b', 'c', 'd' }), }; InitialLdapContext ilc = new InitialLdapContext(null, cs); ilc.reconnect(cs2); assertEquals(cs2, ilc.getConnectControls()); ilc.close(); }
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類 /** * <p>Test method for 'javax.naming.ldap.ControlFactory.getControlInstance(Control)'</p> * <p>Here we are gonna test if we can get an instance with the controls sended.</p> */ public void testGetControlInstanceControl() { try { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.harmony.jndi.tests.javax.naming.spi.mock.ldap.MockContextFactory"); MockControl[] cs = { new MockControl("c1", false, new byte[] { 1, 2, 3, 4 }), new MockControl("c1", true, new byte[] { 'a', 'b', 'c', 'd' }), }; MockControl cs2 = new MockControl("c1", false, new byte[] { 1, 2, 3, 4 }); InitialLdapContext ilc=new InitialLdapContext(env, cs); assertEquals(cs2,ControlFactory.getControlInstance(cs2,ilc,env)); } catch (NamingException e) { } }
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類 /** * <p> * Test method for * 'javax.naming.ldap.InitialLdapContext.reconnect(Control[])' * </p> * <p> * Here we are testing if this method reconnects to the LDAP server using * the supplied controls and this context's environment. In this case we are * sending a new set of controls to reconection. * </p> * <p> * The expected result is a reconection with the new set of controls. * </p> */ public void testReconnect003() throws Exception { System .setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.harmony.jndi.tests.javax.naming.spi.mock.ldap.MockContextFactory"); Control[] cs = { new MockControl("c1", false, new byte[] { 1, 2, 3, 4 }), new MockControl("c1", true, new byte[] { 'a', 'b', 'c', 'd' }), }; Control[] cs2 = { new MockControl("c2", false, new byte[] { 1, 2, 3, 4 }), new MockControl("c2", true, new byte[] { 'a', 'b', 'c', 'd' }), }; InitialLdapContext ilc = new InitialLdapContext(null, cs); ilc.reconnect(cs2); assertEquals(cs2, ilc.getConnectControls()); ilc.close(); }
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類 /** * Return the session which the current thread holds. If not exist, a new * session will be created and bind to current thread. * * @return */ @Override public Session getCurrentSession() { Map.Entry<Session, InitialLdapContext> current = sessions.get(); if (null == current) { try { InitialLdapContext ldapConnection = getContext(); Session session = new SessionImpl(this, ldapConnection, true); current = new AbstractMap.SimpleEntry<Session, InitialLdapContext>( session, ldapConnection); } catch (NamingException e) { throw new ODMException("Cannot instantiate a session", e); } } return current.getKey(); }
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類 @Override public List<LdapUser> getUsersInGroup(final String groupName, final LdapContext context) throws NamingException { if (StringUtils.isBlank(groupName)) { throw new IllegalArgumentException("ldap group name cannot be blank"); } final String basedn = _ldapConfiguration.getBaseDn(); if (StringUtils.isBlank(basedn)) { throw new IllegalArgumentException("ldap basedn is not configured"); } final SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(_ldapConfiguration.getScope()); searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes()); final NamingEnumeration<SearchResult> results = context.search(basedn, generateADGroupSearchFilter(groupName), searchControls); final List<LdapUser> users = new ArrayList<>(); while (results.hasMoreElements()) { final SearchResult result = results.nextElement(); users.add(createUser(result)); } return users; }
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類 public LdapUser searchUser(final String basedn, final String searchString, final LdapContext context) throws NamingException, IOException { final SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(_ldapConfiguration.getScope()); searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes()); final NamingEnumeration<SearchResult> results = context.search(basedn, searchString, searchControls); final List<LdapUser> users = new ArrayList<>(); while (results.hasMoreElements()) { final SearchResult result = results.nextElement(); users.add(createUser(result)); } if (users.size() == 1) { return users.get(0); } else { throw new NamingException("No user found for basedn " + basedn + " and searchString " + searchString); } }
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類 @Override public List<LdapUser> getUsersInGroup(String groupName, LdapContext context, Long domainId) throws NamingException { if (StringUtils.isBlank(groupName)) { throw new IllegalArgumentException("ldap group name cannot be blank"); } String basedn = _ldapConfiguration.getBaseDn(domainId); if (StringUtils.isBlank(basedn)) { throw new IllegalArgumentException("ldap basedn is not configured"); } final SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(_ldapConfiguration.getScope()); searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes(domainId)); NamingEnumeration<SearchResult> results = context.search(basedn, generateADGroupSearchFilter(groupName, domainId), searchControls); final List<LdapUser> users = new ArrayList<LdapUser>(); while (results.hasMoreElements()) { final SearchResult result = results.nextElement(); users.add(createUser(result, domainId)); } return users; }
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類 @Override public List<LdapUser> getUsersInGroup(final String groupName, final LdapContext context) throws NamingException { final String attributeName = _ldapConfiguration.getGroupUniqueMemeberAttribute(); final SearchControls controls = new SearchControls(); controls.setSearchScope(_ldapConfiguration.getScope()); controls.setReturningAttributes(new String[]{attributeName}); final NamingEnumeration<SearchResult> result = context.search(_ldapConfiguration.getBaseDn(), generateGroupSearchFilter(groupName), controls); final List<LdapUser> users = new ArrayList<>(); //Expecting only one result which has all the users if (result.hasMoreElements()) { final Attribute attribute = result.nextElement().getAttributes().get(attributeName); final NamingEnumeration<?> values = attribute.getAll(); while (values.hasMoreElements()) { final String userdn = String.valueOf(values.nextElement()); try { users.add(getUserForDn(userdn, context)); } catch (final NamingException e) { s_logger.info("Userdn: " + userdn + " Not Found:: Exception message: " + e.getMessage()); } } } Collections.sort(users); return users; }
Date date = new Date(13159609200000L);
Date date2 = new Date(118,0,5,15,0,0);
System.out.println(date2);
System.out.println(date2.getTime());
System.out.println(13159609200000L - date2.getTime());
System.out.println(new Date(13205468290468L - 11644473600000L));
lastLogonTimeStamp seems to be the no of 100 nano second intervals starting from 0000 hrs 1/1/1601.
This is similiar to windows file time format and .NET has support to create a System.DateTime object out of this value.. Note the sample here, using the long value that you had provided and watch the date being recreated..
.NET Sample :
import System.*;
class Test
{
public static void main(String args[])
{
DateTime dt=DateTime.FromFileTime(127877417297554938L);
System.out.println(dt);
}
}
Java Sample :
If you want to create a java date object out of this value, please try following this sample, where i have used an adjustment factor to convert the lastLogonTimestamp value into a Date object..
import java.util.*;
class Test
{
public static void main(String args[])
{
long llastLogonAdjust=11644473600000L; // adjust factor for converting it to java
//date Epoch
Date lastLogon = new Date(127877417297554938L/10000-llastLogonAdjust); //
System.out.println(lastLogon);
}
}
lastLogonTimeStamp seems to be the no of 100 nano second intervals starting from 0000 hrs 1/1/1601.
This is similiar to windows file time format and .NET has support to create a System.DateTime object out of this value.. Note the sample here, using the long value that you had provided and watch the date being recreated..
.NET Sample :
import System.*;
class Test
{
public static void main(String args[])
{
DateTime dt=DateTime.FromFileTime(127877417297554938L);
System.out.println(dt);
}
}
Java Sample :
If you want to create a java date object out of this value, please try following this sample, where i have used an adjustment factor to convert the lastLogonTimestamp value into a Date object..
import java.util.*;
class Test
{
public static void main(String args[])
{
long llastLogonAdjust=11644473600000L; // adjust factor for converting it to java
//date Epoch
Date lastLogon = new Date(127877417297554938L/10000-llastLogonAdjust); //
System.out.println(lastLogon);
}
}