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. Liferay 6.0.5 - 以编程方式在新页面中设置权限
- 2. 如何从文件以编程方式导入用户库
- 3. 从Liferay 6.1中的LDAP导入用户和用户组
- 4. 从Liferay portal 6.2中的LDAP导入用户和用户组
- 5. 如何以编程方式使Liferay Portlet进入全屏模式
- 6. 如何以编程方式从Liferay定制portlet登录
- 7. Liferay和将用户导出到LDAP中
- 8. 如何以编程方式从.zip文件导入Eclipse项目?
- 9. liferay以编程方式登录
- 10. 强制Liferay调用LDAP导入用户/组
- 11. 以编程方式查询LDAP权限
- 12. Android:以编程方式启用导入的帐户联系人
- 13. 如何在liferay中以编程方式分配网站并注册用户?
- 14. 如何以编程方式导入/导出SQL数据库表
- 15. 如何以编程方式更新和删除SQL中的LDAP用户?
- 16. Liferay 6.0.5 serviceBuider工作吗?
- 17. 以编程方式动态导入
- 18. 以编程方式批量导入LDIF
- 19. 自动从liferay导入/导入页面的方式
- 20. 如何在Liferay中以编程方式确定css-class-wrapper?
- 21. 如何以编程方式在liferay 6.1中配置portlet?
- 22. 如何以编程方式在liferay中创建站点?
- 23. 如何以编程方式将用户从Facebook中注销?
- 24. 如何将主题从6.0.5升级到Liferay 7?
- 25. 如何忽略Liferay用户从LDAP错误中删除?
- 26. 如何在Magento中以编程方式导入Category Image?
- 27. 如何以编程方式导入Java类
- 28. 如何以编程方式删除Groovy中的默认导入?
- 29. 如何以编程方式导入CRM组织?
- 30. 如何以编程方式将XML数据导入Excel文件?
非常感谢你Prakash – radhakrishna 2012-04-30 14:19:46