Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.36 KB | None | 0 0
  1. SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
  2. builder.bind("someJNDIname",someObject); //e.g. for some datasource
  3. builder.activate();
  4.  
  5. <bean id="ldapContextSource" class="org.springframework.ldap.core.support.LdapContextSource">
  6. <property name="url" value="ldap://ourserver:389" />
  7. <property name="base" value="CN=Groups,CN=ourcompany,DC=com" />
  8. <property name="userDn" value="CN=binduser" />
  9. <property name="password" value="password" />
  10. </bean>
  11.  
  12. <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
  13. <constructor-arg ref="ldapContextSource" />
  14. </bean>
  15.  
  16. @RunWith(SpringJUnit4ClassRunner.class)
  17. @ContextConfiguration(locations = {"classpath:application-context-test.xml"})
  18. public class TestClass {
  19. public TestClass(){
  20. .. //init the SimpleNamingContextBuilder
  21. }
  22.  
  23. @Autowired
  24. private LdapTemplate template;
  25.  
  26. @Test
  27. public void someTestcase(){
  28. ldapTemplate.search("", "(objectclass=user)" ,new LdapUserMapper());
  29. }
  30. }
  31.  
  32. org.springframework.ldap.NotContextException: DirContext object is required.; nested exception is javax.naming.NotContextException: DirContext object is required.
  33. at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:198)
  34. at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:319)
  35. at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:259)
  36. at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:571)
  37. at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:556)
  38. at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:411)
  39. at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:431)
  40. at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:451)
  41. at com.somecompany.TestClass.someTestcase(TestClass.java:30)
  42. [...]
  43. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  44. Caused by: javax.naming.NotContextException: DirContext object is required.
  45. at javax.naming.directory.InitialDirContext.castToDirContext(InitialDirContext.java:106)
  46. at javax.naming.directory.InitialDirContext.getURLOrDefaultInitDirCtx(InitialDirContext.java:112)
  47. at javax.naming.directory.InitialDirContext.search(InitialDirContext.java:245)
  48. at org.springframework.ldap.core.LdapTemplate$4.executeSearch(LdapTemplate.java:253)
  49. at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:293)
  50. ... 35 more
  51.  
  52. InitialContext.doLookup(String name)
  53.  
  54. public class DirContextBuilder implements InitialContextFactoryBuilder {
  55.  
  56. ...
  57.  
  58. public InitialContextFactory createInitialContextFactory(Hashtable<?,?> environment) {
  59. if (environment != null) {
  60. ...
  61. }
  62.  
  63. @BeforeClass
  64. public static void setUp(){
  65. OracleDataSource ods = null;
  66. try {
  67. ods= new OracleDataSource();
  68. } catch (SQLException e) {
  69. e.printStackTrace();
  70. }
  71. ods.setURL("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=....;
  72. ods.setUser(..);
  73. ods.setPassword(..);
  74.  
  75. SimpleNamingContextBuilder builder = null;
  76. try {
  77. builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
  78. builder.bind("some/name", ods);
  79. } catch (NamingException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83.  
  84. @Before
  85. public void beforeTest(){
  86. SimpleNamingContextBuilder.getCurrentContextBuilder().deactivate();
  87. }
  88.  
  89. @Test
  90. public void yourTest(){
  91. .....
  92. }
  93.  
  94. package com.stackoverflow.question8325740.config;
  95.  
  96.  
  97. import com.stackoverflow.question8325740.JndiExplorer;
  98. import com.stackoverflow.question8325740.LdapSettings;
  99. import org.springframework.beans.factory.annotation.Autowired;
  100. import org.springframework.context.annotation.Bean;
  101. import org.springframework.context.annotation.Configuration;
  102. import org.springframework.ldap.core.ContextSource;
  103. import org.springframework.ldap.core.LdapOperations;
  104. import org.springframework.ldap.core.LdapTemplate;
  105. import org.springframework.ldap.core.support.AbstractContextSource;
  106. import org.springframework.ldap.core.support.LdapContextSource;
  107.  
  108. import javax.naming.Context;
  109. import javax.naming.NamingException;
  110. import javax.naming.NoInitialContextException;
  111. import javax.naming.directory.DirContext;
  112. import javax.naming.ldap.Control;
  113. import javax.naming.ldap.InitialLdapContext;
  114. import javax.naming.spi.InitialContextFactory;
  115. import javax.naming.spi.NamingManager;
  116. import java.util.HashMap;
  117. import java.util.Hashtable;
  118. import java.util.Map;
  119.  
  120. @Configuration
  121. public class CommonConfig {
  122.  
  123. private static class CustomLdapContextSource extends AbstractContextSource {
  124. @Override
  125. protected DirContext getDirContextInstance(Hashtable<String, Object> environment) throws NamingException {
  126. return new CustomLdapContext(environment, null);
  127. }
  128. }
  129.  
  130. private static class CustomLdapContext extends InitialLdapContext {
  131. public CustomLdapContext() throws NamingException {
  132. }
  133.  
  134. public CustomLdapContext(Hashtable<?, ?> environment, Control[] connCtls) throws NamingException {
  135. super(environment, connCtls);
  136. }
  137.  
  138. @Override
  139. protected Context getDefaultInitCtx() throws NamingException {
  140. String className = "com.sun.jndi.ldap.LdapCtxFactory";
  141. InitialContextFactory factory;
  142. try {
  143. factory = (InitialContextFactory) Class.forName(className).newInstance();
  144. } catch (Exception e) {
  145. NoInitialContextException ne =
  146. new NoInitialContextException(
  147. "Cannot instantiate class: " + className);
  148. ne.setRootCause(e);
  149. throw ne;
  150. }
  151. return factory.getInitialContext(myProps);
  152. }
  153.  
  154.  
  155. }
  156.  
  157. private static boolean checkTestMode() {
  158. //checking test mode using reflection in order to not collapse in real execution
  159. try {
  160. Class clazz = Class.forName("org.springframework.mock.jndi.SimpleNamingContextBuilder");
  161. Object result = clazz.getMethod("getCurrentContextBuilder").invoke(null);
  162. return NamingManager.hasInitialContextFactoryBuilder() && result != null;
  163. } catch (Exception e) {
  164. return false;
  165. }
  166.  
  167.  
  168. }
  169.  
  170. @Bean
  171. @Autowired
  172. public ContextSource ldapContextSource(LdapSettings ldapSettings) {
  173. AbstractContextSource contextSource;
  174. if (checkTestMode()) {
  175. contextSource = new CustomLdapContextSource();
  176. } else {
  177. contextSource = new LdapContextSource();
  178. }
  179. contextSource.setUrl(ldapSettings.getUrl());
  180. contextSource.setUserDn(ldapSettings.getLogin());
  181. contextSource.setPassword(ldapSettings.getPassword());
  182. contextSource.setPooled(true);
  183. contextSource.setAnonymousReadOnly(false);
  184.  
  185. Map<String, Object> baseEnvironmentProperties = new HashMap<String, Object>();
  186. baseEnvironmentProperties.put(Context.SECURITY_AUTHENTICATION, "simple");
  187. baseEnvironmentProperties.put(Context.REFERRAL, "follow");
  188. contextSource.setBaseEnvironmentProperties(baseEnvironmentProperties);
  189. return contextSource;
  190. }
  191.  
  192. @Bean
  193. @Autowired
  194. public LdapOperations ldapTemplate(ContextSource ldapContextSource) {
  195. return new LdapTemplate(ldapContextSource);
  196. }
  197.  
  198. @Bean
  199. public JndiExplorer jndiExplorer() {
  200. return new JndiExplorer();
  201. }
  202.  
  203. }
  204.  
  205. package com.stackoverflow.question8325740;
  206.  
  207. import com.stackoverflow.question8325740.config.CommonConfig;
  208. import org.junit.jupiter.api.AfterAll;
  209. import org.junit.jupiter.api.Assertions;
  210. import org.junit.jupiter.api.BeforeAll;
  211. import org.junit.jupiter.api.Test;
  212. import org.junit.jupiter.api.extension.ExtendWith;
  213. import org.springframework.beans.factory.annotation.Autowired;
  214. import org.springframework.ldap.core.AttributesMapper;
  215. import org.springframework.ldap.core.LdapOperations;
  216. import org.springframework.ldap.query.LdapQueryBuilder;
  217. import org.springframework.ldap.test.LdapTestUtils;
  218. import org.springframework.mock.jndi.SimpleNamingContextBuilder;
  219. import org.springframework.test.context.ContextConfiguration;
  220. import org.springframework.test.context.junit.jupiter.SpringExtension;
  221.  
  222. import javax.naming.NamingException;
  223. import javax.naming.directory.Attributes;
  224. import javax.naming.directory.BasicAttributes;
  225. import java.util.List;
  226.  
  227.  
  228. @ExtendWith(SpringExtension.class)
  229. @ContextConfiguration(classes = {ApacheDsEmbededConfiguration.class, CommonConfig.class})
  230. public class MainTest {
  231. public static final String TEST_VALUE = "testValue";
  232.  
  233.  
  234. private static SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
  235.  
  236. @BeforeAll
  237. public static void setUp() throws Exception {
  238. builder.bind(JndiExplorer.JNDI_TEST, TEST_VALUE);
  239. builder.activate();
  240. LdapTestUtils.startEmbeddedServer(ApacheDsEmbededConfiguration.PORT, ApacheDsEmbededConfiguration.DEFAULT_PARTITION_SUFFIX, "test");
  241. Thread.sleep(1000);
  242. }
  243.  
  244. @AfterAll
  245. public static void shutdown() throws Exception {
  246. LdapTestUtils.shutdownEmbeddedServer();
  247. builder.deactivate();
  248. }
  249.  
  250.  
  251. @Autowired
  252. private JndiExplorer jndiExplorer;
  253.  
  254. @Autowired
  255. private LdapOperations ldapOperations;
  256.  
  257. @Test
  258. public void testLdapTemplateWithSimpleJndi() {
  259. Assertions.assertEquals(TEST_VALUE, jndiExplorer.getValue());
  260. String cn = "testCN";
  261. String sn = "testSN";
  262. Attributes attrs = new BasicAttributes();
  263. attrs.put("objectClass", "inetOrgPerson");
  264. attrs.put("cn", cn);
  265. attrs.put("sn", sn);
  266. ldapOperations.bind("cn=" + cn + "," + ApacheDsEmbededConfiguration.DEFAULT_PARTITION_SUFFIX, null, attrs);
  267.  
  268. AttributesMapper<String> mapper = new AttributesMapper<String>() {
  269. @Override
  270. public String mapFromAttributes(Attributes attributes) throws NamingException {
  271. return (String) attributes.get("sn").get();
  272. }
  273. };
  274. List<String> sns = ldapOperations.search(LdapQueryBuilder.query().base(ApacheDsEmbededConfiguration.DEFAULT_PARTITION_SUFFIX).attributes("*").where("sn").is(sn), mapper);
  275.  
  276. Assertions.assertEquals(1, sns.size());
  277. String resultSn = sns.get(0);
  278. Assertions.assertEquals(sn, resultSn);
  279.  
  280. }
  281.  
  282. }
  283.  
  284. package com.stackoverflow.question8325740;
  285.  
  286. import org.springframework.context.annotation.Bean;
  287. import org.springframework.context.annotation.Configuration;
  288.  
  289. @Configuration
  290. public class ApacheDsEmbededConfiguration {
  291.  
  292. //default password
  293. static final String PASSWORD = "secret";
  294. //default admin DN
  295. static final String PRINCIPAL = "uid=admin,ou=system";
  296.  
  297. static final String DEFAULT_PARTITION_SUFFIX = "dc=stackoverflow,dc=com";
  298. static final int PORT = 1888;
  299.  
  300. @Bean
  301. public LdapSettings ldapSettings() {
  302. LdapSettings settings = new LdapSettings();
  303. settings.setUrl("ldap://localhost:" + PORT);
  304. settings.setLogin(PRINCIPAL);
  305. settings.setPassword(PASSWORD);
  306. return settings;
  307.  
  308. }
  309.  
  310. }
  311.  
  312. package com.stackoverflow.question8325740;
  313.  
  314.  
  315. public class LdapSettings {
  316. private String url;
  317. private String login;
  318. private String password;
  319.  
  320. public String getUrl() {
  321. return url;
  322. }
  323.  
  324. public void setUrl(String url) {
  325. this.url = url;
  326. }
  327.  
  328. public String getLogin() {
  329. return login;
  330. }
  331.  
  332. public void setLogin(String login) {
  333. this.login = login;
  334. }
  335.  
  336. public String getPassword() {
  337. return password;
  338. }
  339.  
  340. public void setPassword(String password) {
  341. this.password = password;
  342. }
  343. }
  344.  
  345. package com.stackoverflow.question8325740;
  346.  
  347. import javax.annotation.Resource;
  348.  
  349. public class JndiExplorer {
  350. public static final String JNDI_TEST = "com/anything";
  351.  
  352. @Resource(mappedName = JNDI_TEST)
  353. private String value;
  354.  
  355. public String getValue() {
  356. return value;
  357. }
  358.  
  359. public void setValue(String value) {
  360. this.value = value;
  361. }
  362. }
  363.  
  364. <?xml version="1.0" encoding="UTF-8"?>
  365. <project xmlns="http://maven.apache.org/POM/4.0.0"
  366. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  367. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  368. <modelVersion>4.0.0</modelVersion>
  369.  
  370. <groupId>com.stackoverflow</groupId>
  371. <artifactId>question-8325740</artifactId>
  372. <version>1.0-SNAPSHOT</version>
  373.  
  374. <properties>
  375. <maven.compiler.source>1.8</maven.compiler.source>
  376. <maven.compiler.target>1.8</maven.compiler.target>
  377. <junit.version>5.3.2</junit.version>
  378. <spring.version>5.1.4.RELEASE</spring.version>
  379. <spring.ldap.version>2.3.2.RELEASE</spring.ldap.version>
  380. <apacheDirectoryService.version>1.5.5</apacheDirectoryService.version>
  381. <apacheDirectoryService.shared.version>0.9.15</apacheDirectoryService.shared.version>
  382. </properties>
  383. <dependencies>
  384.  
  385. <dependency>
  386. <groupId>org.springframework</groupId>
  387. <artifactId>spring-context</artifactId>
  388. <version>${spring.version}</version>
  389. </dependency>
  390. <dependency>
  391. <groupId>org.springframework.ldap</groupId>
  392. <artifactId>spring-ldap-core</artifactId>
  393. <version>${spring.ldap.version}</version>
  394. </dependency>
  395.  
  396. <dependency>
  397. <groupId>org.springframework</groupId>
  398. <artifactId>spring-test</artifactId>
  399. <version>${spring.version}</version>
  400. <scope>test</scope>
  401. </dependency>
  402. <dependency>
  403. <groupId>org.junit.jupiter</groupId>
  404. <artifactId>junit-jupiter-engine</artifactId>
  405. <version>${junit.version}</version>
  406. <scope>test</scope>
  407. </dependency>
  408. <dependency>
  409. <groupId>org.springframework.ldap</groupId>
  410. <artifactId>spring-ldap-test</artifactId>
  411. <version>${spring.ldap.version}</version>
  412. <scope>test</scope>
  413. </dependency>
  414. <dependency>
  415. <groupId>org.apache.directory.server</groupId>
  416. <artifactId>apacheds-core</artifactId>
  417. <version>${apacheDirectoryService.version}</version>
  418. <scope>test</scope>
  419. </dependency>
  420. <dependency>
  421. <groupId>org.apache.directory.server</groupId>
  422. <artifactId>apacheds-core-entry</artifactId>
  423. <version>${apacheDirectoryService.version}</version>
  424. <scope>test</scope>
  425. </dependency>
  426. <dependency>
  427. <groupId>org.apache.directory.server</groupId>
  428. <artifactId>apacheds-protocol-shared</artifactId>
  429. <version>${apacheDirectoryService.version}</version>
  430. <scope>test</scope>
  431. </dependency>
  432. <dependency>
  433. <groupId>org.apache.directory.server</groupId>
  434. <artifactId>apacheds-protocol-ldap</artifactId>
  435. <version>${apacheDirectoryService.version}</version>
  436. <scope>test</scope>
  437. </dependency>
  438. <dependency>
  439. <groupId>org.apache.directory.server</groupId>
  440. <artifactId>apacheds-server-jndi</artifactId>
  441. <version>${apacheDirectoryService.version}</version>
  442. <scope>test</scope>
  443. </dependency>
  444. <dependency>
  445. <groupId>org.apache.directory.shared</groupId>
  446. <artifactId>shared-ldap</artifactId>
  447. <version>${apacheDirectoryService.shared.version}</version>
  448. <scope>test</scope>
  449. </dependency>
  450. </dependencies>
  451. </project>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement