2
A
回答
1
可以在com.liferay.portal.security.ldap.PortalLDAPImporterImpl檢查Liferay的源代碼,這可能會給你關於如何做到這一點在Liferay中更好的主意。
或
你可以試試下面的代碼在你的自定義portlet,代碼是非常基本的(我已刪除,並只保留這樣就不能編譯這將是需要的基礎知識,但仍然很少修改它應該工作):
import javax.naming.CommunicationException;
import javax.naming.Context;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import com.liferay.portal.model.User;
public class MyProgramaticLDAP {
private static final Properties ENV_PROPS = new Properties();
static {
ENV_PROPS.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
ENV_PROPS.setProperty(Context.PROVIDER_URL, "ldap://url.to.my.com:389");
ENV_PROPS.setProperty(Context.SECURITY_PRINCIPAL, "uid=myuserid,ou=people,dc=myorg,dc=com");
ENV_PROPS.setProperty(Context.SECURITY_CREDENTIALS, "mypassword");
ENV_PROPS.setProperty("PROVIDER_PROTOCOL", "ldap"));
ENV_PROPS.setProperty("PROVIDER_PORT", "389");
ENV_PROPS.setProperty("PROVIDER_HOST", "192.168.5.234");
ENV_PROPS.setProperty("LDAP_BASE_URL", "ldap://url.to.my.com:389");
ENV_PROPS.setProperty("CONTEXT_NAME", "ou=people,dc=myorg,dc=com"));
}
public User getLdapUser(String userEmail) throws PortalException,
SystemException, WebServiceAuthenticationException {
DirContext ctx = null;
String userContext = StringPool.BLANK;
String userName = null;
NamingEnumeration results = null;
//liferay user
User user = new User(); //won't compile
try {
// context and specifying LDAP service provider parameters.
ctx = new InitialDirContext(ENV_PROPS);
userContext = "uid=" + userEmail + "," + ENV_PROPS.getProperty("CONTEXT_NAME");
results = ctx.list(ENV_PROPS.getProperty("CONTEXT_NAME"));
System.out.println("User context: " + userContext);
Attributes attrs = null;
while (results.hasMore()) {
NameClassPair ncp = (NameClassPair) results.next();
userName = ncp.getName();
// the attributes for the record retrieved, your attributes may differ based upon the LDAP you use
System.out.println("Fetching attributes");
attrs = ctx.getAttributes(userName + "," + ENV_PROPS.getProperty("CONTEXT_NAME"));
System.out.println("Attribute mail: " + attrs.get("mail").get());
System.out.println("Attribute sn: " + attrs.get("sn").get());
System.out.println("Attribute title: " + attrs.get("title").get());
System.out.println("Attribute mobile: " + attrs.get("mobile").get());
System.out.println("Attribute firstname: " + attrs.get("firstname").get());
user.setFirstName(attrs.get("firstname").get());
System.out.println("Attribute department: " + attrs.get("department").get());
}// while ends here
} catch (CommunicationException cex) {
cex.printStackTrace();
} catch (Exception exp) {
exp.printStacktrace();
} finally {
// close connection and other code
}
return user;
}
}
相關問題
- 1. 如何從文件以編程方式導入用戶庫
- 2. 從Liferay 6.1中的LDAP導入用戶和用戶組
- 3. 強制Liferay調用LDAP導入用戶/組
- 4. liferay以編程方式登錄
- 5. Android:以編程方式啓用導入的帳戶聯繫人
- 6. 以編程方式查詢LDAP權限
- 7. Liferay 6.0.5 serviceBuider工作嗎?
- 8. 自動從liferay導入/導入頁面的方式
- 9. 如何在Liferay中以編程方式確定css-class-wrapper?
- 10. 如何以編程方式在liferay中創建站點?
- 11. 如何以編程方式在liferay 6.1中配置portlet?
- 12. 如何將主題從6.0.5升級到Liferay 7?
- 13. 如何以編程方式導入或創建頁面模板
- 14. 如何在Magento中以編程方式導入Category Image?
- 15. 如何以編程方式將XML數據導入Excel文件?
- 16. 如何以編程方式導入Java類
- 17. 如何以編程方式導入CRM組織?
- 18. 如何以編程方式從用戶桌面刪除快捷方式?
- 19. 如何以編程方式從mvcArea
- 20. 如何以編程方式從服務
- 21. Liferay 6.0.5:限制用戶只能登錄到他的組織
- 22. 將用戶從LDAP導入Jira時限制導入組
- 23. 如何以編程方式添加用戶帳戶策略?
- 24. 如何以編程方式將用戶帳戶添加到openDS?
- 25. 如何以編程方式從jar文件中導入插件和片段
- 26. JASIG CAS以編程方式登錄(使用Liferay)
- 27. 從應用程序中以編程方式導出數據
- 28. Prestashop以編程方式註銷用戶
- 29. 以編程方式驗證用戶
- 30. 如何以編程方式使用liferay內置電子郵件模板?
非常感謝你Prakash – radhakrishna 2012-04-30 14:19:46