config issues with wso2is 5.9 CustomUserStoremanager

牧云@^-^@ 提交于 2020-03-05 00:27:35

问题


Am trying to get the CustomUserStoreManager, Here I have added below the code for CustomStoreUserManager, I have placed the test_come.xml in userstore folder and added the jar file and mysql driver and jascypt jars to the lib folder. After restarting the server i cant see this in the drop down for add userstore manager list....

<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:


You need to make this as an osgi service. In the pom file also you have to add <packaging> element as bundle since it is an osgi service. You can find a sample source code here: https://github.com/Manukam/wso2-custom-user-store.

You may need to verify the relevant dependency versions for the components according to the release matrix: https://wso2.com/products/carbon/release-matrix/

Please follow this sample code to understand how to register this CustomUserStoreManager as an osgi service https://github.com/Manukam/wso2-custom-user-store/blob/AD/src/main/java/com/wso2/carbon/custom/user/store/manager/internal/CustomUserStoreMgtDSComponent.java

Please read this blog to know further on osgi service: https://medium.com/@dewni.matheesha/how-to-write-a-wso2-custom-osgi-component-2fd90de7eb1a



来源:https://stackoverflow.com/questions/60162072/config-issues-with-wso2is-5-9-customuserstoremanager

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!