wso2is 5.9 CustomUserStoremanager的配置问题

时间:2020-02-11 04:32:33

标签: wso2 wso2is

我试图获取CustomUserStoreManager, 在这里,我在下面添加了CustomStoreUserManager的代码, 已将test_come.xml放在userstore文件夹中,并将jar文件和mysql驱动程序以及jascypt jar添加到lib文件夹中。重新启动服务器后,在添加用户存储管理器列表的下拉列表中我看不到此信息。

<code>
    <?xml version="1.0" encoding="UTF-8"?>
    <UserStoreManager class="com.wso2.custom.usermgt.CustomUserStoreManager">
    <Property name="url">jdbc:mysql://localhost:3306/wso2</Property>
    <Property name="userName">root</Property>
    <Property encrypted="false" name="password">subhash123</Property>
    <Property name="driverName">com.mysql.jdbc.Driver</Property>
    <Property name="ReadGroups">true</Property>
    <Property name="WriteGroups">false</Property>
    <Property name="UsernameJavaRegEx">^[\S]{3,30}$</Property>
    <Property name="UsernameJavaScriptRegEx">^[\S]{3,30}$</Property>
    <Property name="UsernameJavaRegExViolationErrorMsg">Username pattern policy violated</Property>
    <Property name="PasswordJavaRegEx">^[\S]{5,30}$</Property>
    <Property name="PasswordJavaScriptRegEx">^[\S]{5,30}$</Property>
    <Property name="PasswordJavaRegExViolationErrorMsg">
     Password length should be within 5 to 30 characters</Property>
    <Property name="RolenameJavaRegEx">^[\S]{3,30}$</Property>
    <Property name="RolenameJavaScriptRegEx">^[\S]{3,30}$</Property>
    <Property name="CaseInsensitiveUsername">true</Property>
    <Property name="SCIMEnabled">false</Property>
    <Property name="IsBulkImportSupported">false</Property>
    <Property name="PasswordDigest">PLAIN_TEXT</Property>
    <Property name="StoreSaltedPassword">false</Property>
    <Property name="MultiAttributeSeparator">,</Property>
    <Property name="MaxUserNameListLength">100</Property>
    <Property name="MaxRoleNameListLength">100</Property>
    <Property name="UserRolesCacheEnabled">true</Property>
    <Property name="UserNameUniqueAcrossTenants">false</Property>
    <Property name="maxActive">50</Property>
    <Property name="maxWait">60000</Property>
    <Property name="minIdle">5</Property>
    <Property name="CountRetrieverClass">
    org.wso2.carbon.identity.user.store.count.jdbc.JDBCUserStoreCountRetriever
    </Property>
    <Property name="SelectUserSQL">SELECT * FROM CUSTOMER_DATA WHERE CUSTOMER_NAME=?</Property>
    <Property name="DomainName">test.com</Property>
    <Property name="Description"/>
    </UserStoreManager>

<code>

<code>

    public class CustomUserStoreManager extends JDBCUserStoreManager {
    private static Log log = LogFactory.getLog(CustomUserStoreManager.class);
    // This instance is used to generate the hash values
    private static StrongPasswordEncryptor passwordEncryptor = new StrongPasswordEncryptor();
    // You must implement at least one constructor
    public CustomUserStoreManager(RealmConfiguration realmConfig, Map<String, Object> properties, 
    ClaimManager
    claimManager, ProfileConfigurationManager profileManager, UserRealm realm, Integer tenantId)
    throws UserStoreException {
    super(realmConfig, properties, claimManager, profileManager, realm, tenantId);
    log.info("CustomUserStoreManager initialized...");
    }
    @Override
    public boolean doAuthenticate(String userName, Object credential) throws UserStoreException {
    boolean isAuthenticated = false;
    if (userName != null && credential != null) {
    try {
    String candidatePassword = String.copyValueOf(((Secret) credential).getChars());
    Connection dbConnection = null;
    ResultSet rs = null;
    PreparedStatement prepStmt = null;
    String sql = null;
    dbConnection = this.getDBConnection();
    dbConnection.setAutoCommit(false);
    // get the SQL statement used to select user details
    sql = this.realmConfig.getUserStoreProperty("SelectUserSQL");
    System.out.println("SQL IS -->"+sql);
    if (log.isDebugEnabled()) {
    log.debug(sql);
    }

    prepStmt = dbConnection.prepareStatement(sql);
    prepStmt.setString(1, userName);
    // check whether tenant id is used
    rs = prepStmt.executeQuery();
    if (rs.next()) {
    String storedPassword = rs.getString(3);
    System.out.println("PASSWORD IS -->"+storedPassword);
    System.out.println("candidatePassword IS -->"+candidatePassword);
    // check whether password is expired or not
    if(storedPassword.equalsIgnoreCase(candidatePassword))
    isAuthenticated = true;
    }
    dbConnection.commit();
    log.info(userName + " is authenticated? " + isAuthenticated);
    } catch (SQLException exp) { 
    log.error("Error occurred while retrieving user authentication info.", exp);
    throw new UserStoreException("Authentication Failure");
    }
    }
    return isAuthenticated;
    }

    @Override
    protected String preparePassword(Object password, String saltValue) throws UserStoreException {
    if (password != null) {
    String candidatePassword = String.copyValueOf(((Secret) password).getChars());
    // ignore saltValue for the time being
    log.info("Generating hash value using jasypt...");
    return passwordEncryptor.encryptPassword(String.copyValueOf(((Secret) password).getChars()));
    } else {
    log.error("Password cannot be null");
    throw new UserStoreException("Authentication Failure");
    }
    }
    @Override
    public Date getPasswordExpirationTime(String userName) throws UserStoreException {
    return new Date();
    }
    }

<code>
<code>

    <?xml version="1.0" encoding="UTF-8"?>
    <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>org.wso2.sample</groupId>
    <artifactId>CustomReadOnlyJDBCUserStoreManager</artifactId>
    <version>1.0.0</version>
    <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>ignore</checksumPolicy>
    </releases>
    </repository>
    </repositories>
    <dependencies>
    <dependency>
    <groupId>org.wso2.carbon</groupId>
    <artifactId>org.wso2.carbon.user.core</artifactId>
    <version>4.4.11</version>
    </dependency>
    <dependency>
    <groupId>org.wso2.carbon</groupId>
    <artifactId>org.wso2.carbon.utils</artifactId>
    <version>4.4.11</version>
    </dependency>
    <dependency>
    <groupId>org.wso2.carbon</groupId>
    <artifactId>org.wso2.carbon.user.api</artifactId>
    <version>4.4.11</version>
    </dependency>
    <dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.2</version>
    </dependency>
    </dependencies>

     <build>
    <plugins>
    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.1</version>
    <inherited>true</inherited>
    <configuration>
    <encoding>UTF-8</encoding>
    <source>1.7</source>
    <target>1.7</target>
    </configuration>
    </plugin>
    <plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-scr-plugin</artifactId>
    <version>1.7.2</version>
    <executions>
    <execution>
    <id>generate-scr-scrdescriptor</id>
    <goals>
    <goal>scr</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    <plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>2.3.5</version>
    <extensions>true</extensions>
    <configuration>
    <instructions>
    <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
    <Bundle-Name>${project.artifactId}</Bundle-Name>
    <Private-Package>
    org.wso2.sample.user.store.manager.internal
    </Private-Package>
    <Export-Package>
    !org.wso2.sample.user.store.manager.internal,
    org.wso2.sample.user.store.manager.*,
    </Export-Package>
    <Import-Package>
    org.wso2.carbon.*,
    org.apache.commons.logging.*,
    org.osgi.framework.*,
    org.osgi.service.component.*
    </Import-Package>
    </instructions>
    </configuration>
    </plugin>
    </plugins>
    </build>
    </project>
<code>

1 个答案:

答案 0 :(得分:0)

您需要将此作为osgi服务。在pom文件中,您还必须将<packaging>元素添加为 bundle ,因为它是osgi服务。您可以在此处找到示例源代码:https://github.com/Manukam/wso2-custom-user-store

您可能需要根据发布矩阵来验证组件的相关依赖项版本:https://wso2.com/products/carbon/release-matrix/

请遵循以下示例代码,以了解如何将此CustomUserStoreManager注册为osgi服务https://github.com/Manukam/wso2-custom-user-store/blob/AD/src/main/java/com/wso2/carbon/custom/user/store/manager/internal/CustomUserStoreMgtDSComponent.java

请阅读此博客以进一步了解osgi服务:https://medium.com/@dewni.matheesha/how-to-write-a-wso2-custom-osgi-component-2fd90de7eb1a