本文整理汇总了Java中org.springframework.ldap.core.ContextSource类的典型用法代码示例。如果您正苦于以下问题:Java ContextSource类的具体用法?Java ContextSource怎么用?Java ContextSource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ContextSource类属于org.springframework.ldap.core包,在下文中一共展示了ContextSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: uid2ext
import org.springframework.ldap.core.ContextSource; //导入依赖的package包/类
public String uid2ext(String uid) {
String externalIdAttribute = ApplicationProperty.AuthenticationLdapIdAttribute.value();
if ("uid".equals(externalIdAttribute)) return uid; // Nothing to translate
try {
ContextSource source = (ContextSource)SpringApplicationContextHolder.getBean("unitimeLdapContextSource");
String query = ApplicationProperty.AuthenticationLdapLogin2UserId.value();
SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(source);
DirContextOperations user = template.retrieveEntry(query.replaceAll("\\{0\\}", uid), new String[] {externalIdAttribute});
return user == null ? null : user.getStringAttribute(externalIdAttribute);
} catch (Exception e) {
sLog.warn("Unable to translate uid to " + externalIdAttribute + ": " + e.getMessage());
}
return null;
}
示例2: ext2uid
import org.springframework.ldap.core.ContextSource; //导入依赖的package包/类
public String ext2uid(String externalUserId) {
String externalIdAttribute = ApplicationProperty.AuthenticationLdapIdAttribute.value();
if ("uid".equals(externalIdAttribute)) return externalUserId; // Nothing to translate
try {
ContextSource source = (ContextSource)SpringApplicationContextHolder.getBean("unitimeLdapContextSource");
String query = ApplicationProperty.AuthenticationLdapUserId2Login.value().replace("%", externalIdAttribute);
SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(source);
DirContextOperations user = template.retrieveEntry(query.replaceAll("\\{0\\}", externalIdAttribute), new String[] {"uid"});
return user == null ? null : user.getStringAttribute("uid");
} catch (Exception e) {
sLog.warn("Unable to translate " + externalIdAttribute + " to uid: " + e.getMessage());
}
return null;
}
示例3: setContextSource
import org.springframework.ldap.core.ContextSource; //导入依赖的package包/类
/**
* Set the ContextSource to work on. Even though the actual ContextSource
* sent to the LdapTemplate instance should be a
* {@link TransactionAwareContextSourceProxy}, the one sent to this method
* should be the target of that proxy. If it is not, the target will be
* extracted and used instead.
*
* @param contextSource
* the ContextSource to work on.
*/
public void setContextSource(ContextSource contextSource) {
if (contextSource instanceof TransactionAwareContextSourceProxy) {
TransactionAwareContextSourceProxy proxy = (TransactionAwareContextSourceProxy) contextSource;
this.contextSource = proxy.getTarget();
} else {
this.contextSource = contextSource;
}
if (contextSource instanceof AbstractContextSource) {
AbstractContextSource abstractContextSource = (AbstractContextSource) contextSource;
if(abstractContextSource.isAnonymousReadOnly()) {
throw new IllegalArgumentException(
"Compensating LDAP transactions cannot be used when context-source is anonymous-read-only");
}
}
}
示例4: doWithSingleContext
import org.springframework.ldap.core.ContextSource; //导入依赖的package包/类
/**
* Construct a SingleContextSource and execute the LdapOperationsCallback using the created instance.
* This makes sure the same connection will be used for all operations inside the LdapOperationsCallback,
* which is particularly useful when working with e.g. Paged Results as these typically require the exact
* same connection to be used for all requests involving the same cookie..
* The SingleContextSource instance will be properly disposed of once the operation has been completed.
*
* @param contextSource The target ContextSource to retrieve a DirContext from
* @param callback the callback to perform the Ldap operations
* @param useReadOnly if <code>true</code>, use the {@link org.springframework.ldap.core.ContextSource#getReadOnlyContext()}
* method on the target ContextSource to get the actual DirContext instance, if <code>false</code>,
* use {@link org.springframework.ldap.core.ContextSource#getReadWriteContext()}.
* @param ignorePartialResultException Used for populating this property on the created LdapTemplate instance.
* @param ignoreNameNotFoundException Used for populating this property on the created LdapTemplate instance.
* @return the result returned from the callback.
* @since 2.0
*/
public static <T> T doWithSingleContext(ContextSource contextSource,
LdapOperationsCallback<T> callback,
boolean useReadOnly,
boolean ignorePartialResultException,
boolean ignoreNameNotFoundException) {
SingleContextSource singleContextSource;
if (useReadOnly) {
singleContextSource = new SingleContextSource(contextSource.getReadOnlyContext());
} else {
singleContextSource = new SingleContextSource(contextSource.getReadWriteContext());
}
LdapTemplate ldapTemplate = new LdapTemplate(singleContextSource);
ldapTemplate.setIgnorePartialResultException(ignorePartialResultException);
ldapTemplate.setIgnoreNameNotFoundException(ignoreNameNotFoundException);
try {
return callback.doWithLdapOperations(ldapTemplate);
} finally {
singleContextSource.destroy();
}
}
示例5: setUp
import org.springframework.ldap.core.ContextSource; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.clearSynchronization();
}
contextSourceMock = mock(ContextSource.class);
contextMock = mock(DirContext.class);
transactionDefinitionMock = mock(TransactionDefinition.class);
transactionDataManagerMock = mock(CompensatingTransactionOperationManager.class);
renamingStrategyMock = mock(TempEntryRenamingStrategy.class);
tested = new ContextSourceTransactionManager();
tested.setContextSource(contextSourceMock);
tested.setRenamingStrategy(renamingStrategyMock);
}
示例6: verifyReferences
import org.springframework.ldap.core.ContextSource; //导入依赖的package包/类
@Test
public void verifyReferences() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-references.xml");
ContextSource outerContextSource = ctx.getBean(ContextSource.class);
AuthenticationSource authenticationSource = ctx.getBean(AuthenticationSource.class);
DirContextAuthenticationStrategy authenticationStrategy = ctx.getBean(DirContextAuthenticationStrategy.class);
Object baseEnv = ctx.getBean("baseEnvProps");
assertThat(outerContextSource).isNotNull();
assertThat(outerContextSource instanceof TransactionAwareContextSourceProxy).isTrue();
ContextSource contextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
assertThat(authenticationSource).isSameAs(getInternalState(contextSource, "authenticationSource"));
assertThat(authenticationStrategy).isSameAs(getInternalState(contextSource, "authenticationStrategy"));
assertThat(baseEnv).isEqualTo(getInternalState(contextSource, "baseEnv"));
}
示例7: supportsSpel
import org.springframework.ldap.core.ContextSource; //导入依赖的package包/类
@Test
public void supportsSpel() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-spel.xml");
ContextSource outerContextSource = ctx.getBean(ContextSource.class);
assertThat(outerContextSource).isNotNull();
assertThat(outerContextSource instanceof TransactionAwareContextSourceProxy).isTrue();
ContextSource contextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
assertThat(LdapUtils.newLdapName("dc=261consulting,dc=com")).isEqualTo(getInternalState(contextSource, "base"));
assertThat("uid=admin").isEqualTo(getInternalState(contextSource, "userDn"));
assertThat("apassword").isEqualTo(getInternalState(contextSource, "password"));
assertThat(new String[]{"ldap://localhost:389"}).isEqualTo((Object[]) getInternalState(contextSource, "urls"));
}
示例8: verifyParseWithDefaultTransactions
import org.springframework.ldap.core.ContextSource; //导入依赖的package包/类
@Test
public void verifyParseWithDefaultTransactions() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-transactional-defaults.xml");
ContextSource outerContextSource = ctx.getBean(ContextSource.class);
PlatformTransactionManager transactionManager = ctx.getBean(PlatformTransactionManager.class);
assertThat(outerContextSource).isNotNull();
assertThat(transactionManager).isNotNull();
assertThat(outerContextSource instanceof TransactionAwareContextSourceProxy).isTrue();
ContextSource contextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
assertThat(transactionManager instanceof ContextSourceTransactionManager).isTrue();
Object delegate = getInternalState(transactionManager, "delegate");
assertThat(contextSource).isSameAs(getInternalState(delegate, "contextSource"));
TempEntryRenamingStrategy renamingStrategy =
(TempEntryRenamingStrategy) getInternalState(delegate, "renamingStrategy");
assertThat(renamingStrategy instanceof DefaultTempEntryRenamingStrategy).isTrue();
assertThat("_temp").isEqualTo(getInternalState(renamingStrategy, "tempSuffix"));
}
示例9: verifyParsePoolingSizeSet
import org.springframework.ldap.core.ContextSource; //导入依赖的package包/类
@Test
public void verifyParsePoolingSizeSet() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-pooling-configured-poolsize.xml");
ContextSource outerContextSource = ctx.getBean(ContextSource.class);
assertThat(outerContextSource).isNotNull();
ContextSource pooledContextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
assertThat(pooledContextSource).isNotNull();
GenericKeyedObjectPool objectPool = (GenericKeyedObjectPool) getInternalState(pooledContextSource, "keyedObjectPool");
assertThat(objectPool.getMaxActive()).isEqualTo(10);
assertThat(objectPool.getMaxTotal()).isEqualTo(12);
assertThat(objectPool.getMaxIdle()).isEqualTo(11);
assertThat(objectPool.getMaxWait()).isEqualTo(13);
assertThat(objectPool.getMinIdle()).isEqualTo(14);
assertThat(objectPool.getWhenExhaustedAction()).isEqualTo((byte)0);
}
示例10: verifyParsePoolWithPlaceholders
import org.springframework.ldap.core.ContextSource; //导入依赖的package包/类
@Test
public void verifyParsePoolWithPlaceholders() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-pooling-config-with-placeholders.xml");
ContextSource outerContextSource = ctx.getBean(ContextSource.class);
assertThat(outerContextSource).isNotNull();
ContextSource pooledContextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
assertThat(pooledContextSource).isNotNull();
GenericKeyedObjectPool objectPool = (GenericKeyedObjectPool) getInternalState(pooledContextSource, "keyedObjectPool");
assertThat(objectPool.getTimeBetweenEvictionRunsMillis()).isEqualTo(10);
assertThat(objectPool.getMinEvictableIdleTimeMillis()).isEqualTo(20);
assertThat(objectPool.getMaxWait()).isEqualTo(10);
assertThat(objectPool.getMaxTotal()).isEqualTo(11);
assertThat(objectPool.getMaxActive()).isEqualTo(15);
assertThat(objectPool.getMinIdle()).isEqualTo(16);
assertThat(objectPool.getMaxIdle()).isEqualTo(17);
assertThat(objectPool.getNumTestsPerEvictionRun()).isEqualTo(18);
}
示例11: verifyParsePool2WithPlaceholders
import org.springframework.ldap.core.ContextSource; //导入依赖的package包/类
@Test
public void verifyParsePool2WithPlaceholders() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-pooling2-config-with-placeholders.xml");
ContextSource outerContextSource = ctx.getBean(ContextSource.class);
assertThat(outerContextSource).isNotNull();
ContextSource pooledContextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
assertThat(pooledContextSource).isNotNull();
org.apache.commons.pool2.impl.GenericKeyedObjectPool objectPool =
(org.apache.commons.pool2.impl.GenericKeyedObjectPool) getInternalState(pooledContextSource, "keyedObjectPool");
assertThat(objectPool.getTimeBetweenEvictionRunsMillis()).isEqualTo(10);
assertThat(objectPool.getMinEvictableIdleTimeMillis()).isEqualTo(20);
assertThat(objectPool.getMaxWaitMillis()).isEqualTo(10);
assertThat(objectPool.getMaxTotal()).isEqualTo(11);
assertThat(objectPool.getMinIdlePerKey()).isEqualTo(12);
assertThat(objectPool.getMaxIdlePerKey()).isEqualTo(13);
assertThat(objectPool.getMaxTotalPerKey()).isEqualTo(14);
assertThat(objectPool.getNumTestsPerEvictionRun()).isEqualTo(18);
}
示例12: detectSingleContextSourceShouldReturnSingleDependency
import org.springframework.ldap.core.ContextSource; //导入依赖的package包/类
@Test
public void detectSingleContextSourceShouldReturnSingleDependency() {
ContextSource ctx = Mockito.mock(ContextSource.class);
LdapRelationshipDetector detector = new LdapRelationshipDetector(Arrays.asList(ctx));
Set<Relationship> actual = detector.detect();
Dependency dependency = Dependency.on(Component.of("default", "directory/ldap"));
Set<Relationship> expected = new HashSet<>();
expected.add(dependency);
Assertions.assertThat(actual).isEqualTo(expected);
}
示例13: detectManyContextSourcesShouldReturnSingleDependency
import org.springframework.ldap.core.ContextSource; //导入依赖的package包/类
@Test
public void detectManyContextSourcesShouldReturnSingleDependency() {
List<ContextSource> contextSources = Arrays.asList(Mockito.mock(ContextSource.class),
Mockito.mock(ContextSource.class));
LdapRelationshipDetector detector = new LdapRelationshipDetector(contextSources);
Set<Relationship> actual = detector.detect();
Dependency dependency = Dependency.on(Component.of("default", "directory/ldap"));
Set<Relationship> expected = new HashSet<>();
expected.add(dependency);
Assertions.assertThat(actual).isEqualTo(expected);
}
示例14: detectContextSourceWithCustomNameShouldCustomizeDependency
import org.springframework.ldap.core.ContextSource; //导入依赖的package包/类
@Test
public void detectContextSourceWithCustomNameShouldCustomizeDependency() {
ContextSource ctx = Mockito.mock(ContextSource.class);
LdapRelationshipDetector detector = new LdapRelationshipDetector(Arrays.asList(ctx));
String name = "dir";
detector.setDefaultName(name);
Set<Relationship> actual = detector.detect();
Dependency dependency = Dependency.on(Component.of(name, "directory/ldap"));
Set<Relationship> expected = new HashSet<>();
expected.add(dependency);
Assertions.assertThat(detector.getDefaultName()).isEqualTo(name);
Assertions.assertThat(actual).isEqualTo(expected);
}
示例15: doLookup
import org.springframework.ldap.core.ContextSource; //导入依赖的package包/类
@Override
public UserInfo doLookup(String uid) throws Exception {
try {
ContextSource source = (ContextSource)SpringApplicationContextHolder.getBean("unitimeLdapContextSource");
String query = ApplicationProperty.AuthenticationLdapIdentify.value();
String idAttributeName = ApplicationProperty.AuthenticationLdapIdAttribute.value();
SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(source);
DirContextOperations user = template.retrieveEntry(query.replaceAll("\\{0\\}", uid), new String[] {"uid", idAttributeName, "cn", "givenName", "sn", "mail"});
if (user == null || user.getStringAttribute(idAttributeName) == null)
return null;
UserInfo info = new UserInfo();
info.setExternalId(user.getStringAttribute(idAttributeName));
info.setUserName(user.getStringAttribute("uid"));
if (info.getUserName() == null) info.setUserName(uid);
info.setName(user.getStringAttribute("cn"));
info.setFirstName(user.getStringAttribute("givenName"));
info.setLastName(user.getStringAttribute("sn"));
info.setEmail(user.getStringAttribute("mail"));
if (info.getEmail() == null) {
String email = info.getUserName() + "@";
for (String x: user.getNameInNamespace().split(","))
if (x.startsWith("dc=")) email += (email.endsWith("@") ? "" : ".") + x.substring(3);
if (!email.endsWith("@")) info.setEmail(email);
}
return info;
} catch (Exception e) {
sLog.warn("Lookup for " + uid + " failed: " + e.getMessage());
}
return null;
}