扩展wso2用户存储

时间:2016-03-01 10:02:05

标签: wso2 wso2carbon

我正在尝试实现扩展默认ActiveDirectoryUserStoreManager用户存储的自定义用户存储。但是,在dropins目录中安装生成的osgi包之后,服务器启动时出现错误。

我正在WSO2 API Manager 1.10.0中测试此自定义用户存储。以下是我正在使用的代码。

文件pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>pt.ipleiria.wso2</groupId>
    <artifactId>pt.mydomain.wso2.carbon.user.core</artifactId>
    <version>0.5.0</version>
    <packaging>bundle</packaging>

<dependencies>
    <dependency>
        <groupId>org.wso2.carbon</groupId>
        <artifactId>org.wso2.carbon.identity.application.authentication.framework</artifactId>
        <version>4.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.wso2.carbon</groupId>
        <artifactId>org.wso2.carbon.user.core</artifactId>
        <version>4.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.osgi.core</artifactId>
        <version>1.4.0</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>2.5.0</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
                    <Bundle-Name>${project.artifactId}</Bundle-Name>
                    <Bundle-Version>0.0.1</Bundle-Version>
                    <Export-Package>pt.mydomain.wso2.carbon.user.core.ldap</Export-Package>
                    <Import-Package>
                        *
                    </Import-Package>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>wso2-nexus</id>
        <name>WSO2 internal Repository</name>
        <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
        <releases>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
            <checksumPolicy>fail</checksumPolicy>
        </releases>
    </repository>
</repositories>

</project>

文件MyDomainActiveDirectoryUserStoreManager.java:

package pt.mydomain.wso2.carbon.user.core.ldap;

import java.util.HashMap;
import java.util.Map;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchResult;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.user.core.UserRealm;
import org.wso2.carbon.user.core.UserStoreException;
import org.wso2.carbon.user.core.claim.ClaimManager;
import org.wso2.carbon.user.core.config.RealmConfiguration;
import org.wso2.carbon.user.core.ldap.ActiveDirectoryUserStoreManager;
import org.wso2.carbon.user.core.ldap.LDAPConstants;
import org.wso2.carbon.user.core.profile.ProfileConfigurationManager;
import org.wso2.carbon.user.core.util.JNDIUtil;


public class MyDomainActiveDirectoryUserStoreManager extends ActiveDirectoryUserStoreManager {

Log log = LogFactory.getLog(MyDomainActiveDirectoryUserStoreManager.class);

public MyDomainActiveDirectoryUserStoreManager(
        RealmConfiguration realmConfig, 
        ClaimManager claimManager, 
        ProfileConfigurationManager profileConfigManager) throws UserStoreException {
    super(realmConfig, claimManager, profileConfigManager);
}

public MyDomainActiveDirectoryUserStoreManager(
        RealmConfiguration realmConfig,
        Map<String, Object> properties,
        ClaimManager claimManager,
        ProfileConfigurationManager profileConfigManager, 
        UserRealm userRealm,
        Integer tenantId) throws UserStoreException {
    super(realmConfig, properties, claimManager, profileConfigManager, userRealm, tenantId);
}

public MyDomainActiveDirectoryUserStoreManager() {
    super();
}


public Map<String, String> getUserPropertyValues(String userName, String[] propertyNames,
        String profileName) throws UserStoreException {
    Map<String, String> values = new HashMap<String, String>();
    String searchFilter = realmConfig.getUserStoreProperty(LDAPConstants.USER_NAME_LIST_FILTER);
    String userNameProperty = realmConfig
            .getUserStoreProperty(LDAPConstants.USER_NAME_ATTRIBUTE);
    searchFilter = "(&" + searchFilter + "(" + userNameProperty + "=" + userName + "))";

    DirContext dirContext = this.connectionSource.getContext();
    NamingEnumeration<?> answer = null;
    NamingEnumeration<?> attrs = null;
    try {
        answer = this.searchForUser(searchFilter, propertyNames, dirContext);
        while (answer.hasMoreElements()) {
            SearchResult sr = (SearchResult) answer.next();
            Attributes attributes = sr.getAttributes();
            if (attributes != null) {
                for (String name : propertyNames) {
                    Attribute attribute = attributes.get(name);
                    if (attribute != null) {
                        StringBuffer attrBuffer = new StringBuffer();
                        for (attrs = attribute.getAll(); attrs.hasMore();) {
                            String attr = (String) attrs.next();
                            if (attr != null && attr.trim().length() > 0) {
                                attrBuffer.append(attr + ",");
                            }
                        }
                        String value = attrBuffer.toString();
                        /*
                         * Length needs to be more than one for a valid attribute, since we
                         * attach ",".
                         */
                        if (value != null && value.trim().length() > 1) {
                            value = value.substring(0, value.length() - 1);
                            values.put(name, value);
                        }
                    }
                }
            }
        }

    } catch (NamingException e) {
        log.error(e.getMessage(), e);
        throw new UserStoreException(e.getMessage(), e);
    } finally {
        // close the naming enumeration and free up resources
        JNDIUtil.closeNamingEnumeration(attrs);
        JNDIUtil.closeNamingEnumeration(answer);
        // close directory context
        JNDIUtil.closeContext(dirContext);
    }
    return values;
}

}

文件Activator.java:

    package pt.mydomain.wso2.carbon.user.core.ldap;

    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;

    public class Activator implements BundleActivator {
    public void start(BundleContext bundleContext) throws Exception {
        System.out.println("Start bundle pt.mydomain.wso2.carbon.user.core.ldap");

    }

    public void stop(BundleContext bundleContext) throws Exception {
        System.out.println("Stop bundle pt.mydomain.wso2.carbon.user.core.ldap");       
    }

}

这是我在服务器启动时获得的堆栈跟踪:

    [2016-03-01 09:59:02,231]  INFO - CarbonCoreActivator Starting WSO2 Carbon...
[2016-03-01 09:59:02,234]  INFO - CarbonCoreActivator Operating System : Linux 4.3.4-300.fc23.x86_64, amd64
[2016-03-01 09:59:02,234]  INFO - CarbonCoreActivator Java Home        : /usr/java/jdk1.7.0_79/jre
[2016-03-01 09:59:02,234]  INFO - CarbonCoreActivator Java Version     : 1.7.0_79
[2016-03-01 09:59:02,234]  INFO - CarbonCoreActivator Java VM          : Java HotSpot(TM) 64-Bit Server VM 24.79-b02,Oracle Corporation
[2016-03-01 09:59:02,234]  INFO - CarbonCoreActivator Carbon Home      : /home/mickael/wso2/wso2am-1.10.0
[2016-03-01 09:59:02,234]  INFO - CarbonCoreActivator Java Temp Dir    : /home/mickael/wso2/wso2am-1.10.0/tmp
[2016-03-01 09:59:02,235]  INFO - CarbonCoreActivator User             : mickael, en-US, Europe/Lisbon
[2016-03-01 09:59:02,315]  WARN - ValidationResultPrinter Carbon is configured to use the default keystore (wso2carbon.jks). To maximize security when deploying to a production environment, configure a new keystore with a unique password in the production server profile.
[2016-03-01 09:59:02,369]  INFO - AgentHolder Agent created !
[2016-03-01 09:59:02,384]  INFO - AgentDS Successfully deployed Agent Client
[2016-03-01 09:59:03,644] ERROR - DefaultRealm Cannot initialize pt.mydomain.wso2.carbon.user.core.ldap.MyDomainActiveDirectoryUserStoreManager. Error pt.mydomain.wso2.carbon.user.core.ldap.MyDomainActiveDirectoryUserStoreManager.<init>(org.wso2.carbon.user.api.RealmConfiguration, java.util.Map)Type class org.wso2.carbon.user.core.UserStoreException
org.wso2.carbon.user.core.UserStoreException: Cannot initialize pt.mydomain.wso2.carbon.user.core.ldap.MyDomainActiveDirectoryUserStoreManager. Error pt.mydomain.wso2.carbon.user.core.ldap.MyDomainActiveDirectoryUserStoreManager.<init>(org.wso2.carbon.user.api.RealmConfiguration, java.util.Map)Type class org.wso2.carbon.user.core.UserStoreException
    at org.wso2.carbon.user.core.common.DefaultRealm.createObjectWithOptions(DefaultRealm.java:382)
    at org.wso2.carbon.user.core.common.DefaultRealm.initializeObjects(DefaultRealm.java:203)
    at org.wso2.carbon.user.core.common.DefaultRealm.init(DefaultRealm.java:108)
    at org.wso2.carbon.user.core.common.DefaultRealmService.initializeRealm(DefaultRealmService.java:262)
    at org.wso2.carbon.user.core.common.DefaultRealmService.<init>(DefaultRealmService.java:99)
    at org.wso2.carbon.user.core.common.DefaultRealmService.<init>(DefaultRealmService.java:112)
    at org.wso2.carbon.user.core.internal.Activator.startDeploy(Activator.java:68)
    at org.wso2.carbon.user.core.internal.BundleCheckActivator.start(BundleCheckActivator.java:61)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
    at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:390)
    at org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1176)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:559)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:544)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:457)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:243)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:438)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:1)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
    at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
Caused by: org.wso2.carbon.user.core.UserStoreException: Cannot initialize pt.mydomain.wso2.carbon.user.core.ldap.MyDomainActiveDirectoryUserStoreManager. Error pt.mydomain.wso2.carbon.user.core.ldap.MyDomainActiveDirectoryUserStoreManager.<init>(org.wso2.carbon.user.api.RealmConfiguration, java.util.Map)
    at org.wso2.carbon.user.core.common.DefaultRealm.createObjectWithOptions(DefaultRealm.java:374)
    ... 22 more
Caused by: java.lang.NoSuchMethodException: pt.mydomain.wso2.carbon.user.core.ldap.MyDomainActiveDirectoryUserStoreManager.<init>(org.wso2.carbon.user.api.RealmConfiguration, java.util.Map)
    at java.lang.Class.getConstructor0(Class.java:2892)
    at java.lang.Class.getConstructor(Class.java:1723)
    at org.wso2.carbon.user.core.common.DefaultRealm.createObjectWithOptions(DefaultRealm.java:365)
    ... 22 more
[2016-03-01 09:59:03,646] ERROR - Activator Cannot start User Manager Core bundle
org.wso2.carbon.user.core.UserStoreException: Cannot initialize the realm.
    at org.wso2.carbon.user.core.common.DefaultRealmService.initializeRealm(DefaultRealmService.java:272)
    at org.wso2.carbon.user.core.common.DefaultRealmService.<init>(DefaultRealmService.java:99)
    at org.wso2.carbon.user.core.common.DefaultRealmService.<init>(DefaultRealmService.java:112)
    at org.wso2.carbon.user.core.internal.Activator.startDeploy(Activator.java:68)
    at org.wso2.carbon.user.core.internal.BundleCheckActivator.start(BundleCheckActivator.java:61)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
    at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:390)
    at org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1176)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:559)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:544)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:457)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:243)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:438)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:1)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
    at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
Caused by: org.wso2.carbon.user.core.UserStoreException: Cannot initialize pt.mydomain.wso2.carbon.user.core.ldap.MyDomainActiveDirectoryUserStoreManager. Error pt.mydomain.wso2.carbon.user.core.ldap.MyDomainActiveDirectoryUserStoreManager.<init>(org.wso2.carbon.user.api.RealmConfiguration, java.util.Map)Type class org.wso2.carbon.user.core.UserStoreException
    at org.wso2.carbon.user.core.common.DefaultRealm.initializeObjects(DefaultRealm.java:303)
    at org.wso2.carbon.user.core.common.DefaultRealm.init(DefaultRealm.java:108)
    at org.wso2.carbon.user.core.common.DefaultRealmService.initializeRealm(DefaultRealmService.java:262)
    ... 19 more
Caused by: org.wso2.carbon.user.core.UserStoreException: Cannot initialize pt.mydomain.wso2.carbon.user.core.ldap.MyDomainActiveDirectoryUserStoreManager. Error pt.mydomain.wso2.carbon.user.core.ldap.MyDomainActiveDirectoryUserStoreManager.<init>(org.wso2.carbon.user.api.RealmConfiguration, java.util.Map)Type class org.wso2.carbon.user.core.UserStoreException
    at org.wso2.carbon.user.core.common.DefaultRealm.createObjectWithOptions(DefaultRealm.java:382)
    at org.wso2.carbon.user.core.common.DefaultRealm.initializeObjects(DefaultRealm.java:203)
    ... 21 more
Caused by: org.wso2.carbon.user.core.UserStoreException: Cannot initialize pt.mydomain.wso2.carbon.user.core.ldap.MyDomainActiveDirectoryUserStoreManager. Error pt.mydomain.wso2.carbon.user.core.ldap.MyDomainActiveDirectoryUserStoreManager.<init>(org.wso2.carbon.user.api.RealmConfiguration, java.util.Map)
    at org.wso2.carbon.user.core.common.DefaultRealm.createObjectWithOptions(DefaultRealm.java:374)
    ... 22 more
Caused by: java.lang.NoSuchMethodException: pt.mydomain.wso2.carbon.user.core.ldap.MyDomainActiveDirectoryUserStoreManager.<init>(org.wso2.carbon.user.api.RealmConfiguration, java.util.Map)
    at java.lang.Class.getConstructor0(Class.java:2892)
    at java.lang.Class.getConstructor(Class.java:1723)
    at org.wso2.carbon.user.core.common.DefaultRealm.createObjectWithOptions(DefaultRealm.java:365)
    ... 22 more
[2016-03-01 09:59:09,517]  INFO - TaglibUriRule TLD skipped. URI: http://tiles.apache.org/tags-tiles is already defined

我在pom.xml中使用了错误的依赖项吗?还是我错过了其他一些课?欢迎任何帮助。

2 个答案:

答案 0 :(得分:1)

是的,您使用的是错误的依赖版本。对于API Manager 1.10.0,您必须使用以下版本的依赖项。请注意,groupId也会更改

[INFO] com.install4j.runtime.beans.screens.InstallationScreen [ID 6]: Rollback barrier reached
       command: move 1 screens, executing actions, checking condition
[INFO] com.install4j.runtime.beans.screens.FinishedScreen [ID 10]: Show screen
       Variable changed: executeLauncherAction=true[class java.lang.Boolean]
[INFO] com.install4j.runtime.beans.screens.FinishedScreen [ID 10]: command: finish
       cleaning up
       Finished

答案 1 :(得分:0)

I solved my issue, I was importing class org.wso2.carbon.user.core.config.RealmConfiguration instead of org.wso2.carbon.user.api.RealmConfiguration