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