2017-01-23 180 views
4

我在WSO2 5.3WSO25.3自定义用户商店经理

我用例是调用API认证用户,并根据提供的凭据返回isAuthenticated为真/假。 我已经写了自定义用户存储(如MyAPIUserStoreManager延伸JDBCUserStoreManager)并放入droppins文件夹,我可以重新启动服务器之后在液滴看到这个回落, 然后我还配置了,立即应用作为服务提供者如我想通过使用api(自定义用户商店,即MyAPIUserStoreManager)验证的凭据登录到此应用程序。

这工作正常wso25.2版本:

但WSO25.3我的问题是,点击登录按钮提供的用户名/密码后,在日志它说以下内容: TID:[-1234 ] [] [2017-01-23 16:05:10,673]错误{org.wso2.carbon.user.core.common.AbstractUserStoreManager} - java.lang.ClassCastException:org.wso2.carbon.utils.Secret无法投射为java.lang.String TID:[-1234] [] [2017年1月23日16:05:10676] DEBUG {org.wso2.carbon.identity.application.authenticator.basicauth.BasicAuthenticator} - 用户认证失败,由于无效的凭据

这不会是thr尽管我的自定义用户存储,即MyAPIUserStoreManager莫名其妙。 我是否缺少配置?我也跟着跟着链接: https://docs.wso2.com/display/ADMIN44x/Writing+a+Custom+User+Store+Manager#WritingaCustomUserStoreManager-AbstractUserStoreManagerandimplementations

下面是自定义用户店长代码: 公共类MyAPIUserStoreManager扩展JDBCUserStoreManager {

private static Log log = LogFactory.getLog(MyAPIUserStoreManager.class); 
Map<String,String> userProperties; 

public MyAPIUserStoreManager() { 

} 

public MyAPIUserStoreManager(RealmConfiguration realmConfig, Map<String, Object> properties, 
           ClaimManager claimManager, ProfileConfigurationManager profileManager, UserRealm realm, 
           Integer tenantId) throws UserStoreException { 

    super(realmConfig, properties, claimManager, profileManager, realm, tenantId); 

} 

@Override 
public boolean doAuthenticate(String userName, Object credential) throws UserStoreException { 

    boolean isAuthenticated = false; 
    if (userName == null || credential == null) { 
     return false; 
    } 

    userName = userName.trim(); 

    String password = (String) credential; 
    password = password.trim(); 

    if (userName.equals("") || password.equals("")) { 
     return false; 
    } 
    Map<String, String> properties = initUserProperties(userName, password); 

    if (userName.equals(properties.get("loginName"))) { 
     isAuthenticated = true; 
    } 
    return isAuthenticated; 
} 

@Override 
public Map<String, String> getUserPropertyValues(String username, String[] propertyNames, 
               String profileName) throws UserStoreException { 

    Map<String,String> map = new HashMap<>(); 
    if (userProperties == null) { 
     log.warn("User property values not initialized for " + username + ", returning null"); 
     return null; 
    } 

    for (String propertyName : propertyNames) { 
     if ("accountId".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("accountId")); 
     } else if ("userStatusID".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("userStatusID")); 
     } else if ("loginName".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("loginName")); 
     } else if ("firstName".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("firstName")); 
     } else if ("lastName".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("lastName")); 
     } else if ("email".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("email")); 
     } else if ("phoneNumber".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("phoneNumber")); 
     } else if ("role".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("role")); 
     } else if ("roleId".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("roleId")); 
     } else if ("secretQuestionId".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("secretQuestionId")); 
     } else if ("secretAnswer".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("secretAnswer")); 
     } else if ("dateLastUpdated".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("dateLastUpdated")); 
     } else if ("lastUpdatedByUserId".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("lastUpdatedByUserId")); 
     } else if ("password".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("password")); 
     } else if ("existingsuperuser".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("existingsuperuser")); 
     } else if ("updateMeWithAnnouncements".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("updateMeWithAnnouncements")); 
     } else if ("blockAccess".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("blockAccess")); 
     } else if ("allowEndUserOutboundCallerId".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("allowEndUserOutboundCallerId")); 
     } else if ("allowCallBlocking".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("allowCallBlocking")); 
     } else if ("passExpiry".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("passExpiry")); 
     } else if ("passhash".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("passhash")); 
     } else if ("salt".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("salt")); 
     } else if ("passHistory".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("passHistory")); 
     } else if ("passAlgo".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("passAlgo")); 
     } else if ("sendEmail".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("sendEmail")); 
     } else if ("contactnumbers".equals(propertyName)) { 
      map.put(propertyName, userProperties.get("contactnumbers")); 
     } 
    } 
    return map; 
} 

@Override 
public org.wso2.carbon.user.api.Properties getDefaultUserStoreProperties() { 

    return MyAPIUserConstants.getDefaultUserStoreProperties(); 
} 

@Override 
public String[] getAllProfileNames() throws UserStoreException { 

    return new String[]{"default"}; 
} 

@Override 
public String[] getProfileNames(String userName) throws UserStoreException { 

    return new String[]{"default"}; 
} 

public boolean isMultipleProfilesAllowed() { 

    return false; 
} 

public boolean isReadOnly() throws UserStoreException { 

    return true; 
} 

private Map<String,String> initUserProperties(String userName, String password) throws UserStoreException { 

    userProperties = new HashMap<>(); 
    String url = realmConfig.getUserStoreProperty(MyAPIUserConstants.LOGIN_API); 

    if (url == null) { 
     throw new UserStoreException("Authentication API not defined"); 
    } 

    String params = URLEncoder.encode(userName + "," + password); 

    url = String.format(url, params); 

    HttpClient client = HttpClientBuilder.create().build(); 
    HttpGet request = new HttpGet(url); 
    log.debug("Lets see headers"); 
    request.addHeader("Authorization", "Basic ffgggggddddd"); //hard coding as of now 
    HttpResponse response; 
    String xmlResponse = null; 
    try { 
     log.debug("Authorization header is "+request.getFirstHeader("Authorization")); 
     response = client.execute(request); 
     if (response.getStatusLine().getStatusCode() == 200) { 
      HttpEntity resEntity = response.getEntity(); 
      xmlResponse = EntityUtils.toString(resEntity); 
      xmlResponse = MyApiUserStoreUtils.trim(xmlResponse); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    if (StringUtils.isNotEmpty(xmlResponse)) { 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder; 
     InputSource is; 
     try { 
      builder = factory.newDocumentBuilder(); 
      is = new InputSource(new StringReader(xmlResponse)); 
      Document doc = builder.parse(is); 
      userProperties.put("accountId", doc.getElementsByTagName("accountId").item(0).getTextContent()); 
      userProperties.put("userStatusID", doc.getElementsByTagName("userStatusID").item(0).getTextContent()); 
      userProperties.put("loginName", doc.getElementsByTagName("loginName").item(0).getTextContent()); 
      userProperties.put("firstName", doc.getElementsByTagName("firstName").item(0).getTextContent()); 
      userProperties.put("lastName", doc.getElementsByTagName("lastName").item(0).getTextContent()); 
      userProperties.put("email", doc.getElementsByTagName("email").item(0).getTextContent()); 
      userProperties.put("phoneNumber", doc.getElementsByTagName("phoneNumber").item(0).getTextContent()); 
      userProperties.put("role", doc.getElementsByTagName("role").item(0).getTextContent()); 
      userProperties.put("roleId", doc.getElementsByTagName("roleId").item(0).getTextContent()); 
      userProperties.put("secretQuestionId", doc.getElementsByTagName("secretQuestionId").item(0).getTextContent()); 
      userProperties.put("secretAnswer", doc.getElementsByTagName("secretAnswer").item(0).getTextContent()); 
      userProperties.put("dateLastUpdated", doc.getElementsByTagName("dateLastUpdated").item(0).getTextContent()); 
      userProperties.put("lastUpdatedByUserId", doc.getElementsByTagName("lastUpdatedByUserId").item(0).getTextContent()); 
      userProperties.put("password", doc.getElementsByTagName("password").item(0).getTextContent()); 
      userProperties.put("existingsuperuser", doc.getElementsByTagName("existingsuperuser").item(0).getTextContent()); 
      userProperties.put("updateMeWithAnnouncements", doc.getElementsByTagName("updateMeWithAnnouncements").item(0).getTextContent()); 
      userProperties.put("blockAccess", doc.getElementsByTagName("blockAccess").item(0).getTextContent()); 
      userProperties.put("allowEndUserOutboundCallerId", doc.getElementsByTagName("allowEndUserOutboundCallerId").item(0).getTextContent()); 
      userProperties.put("allowCallBlocking", doc.getElementsByTagName("allowCallBlocking").item(0).getTextContent()); 
      userProperties.put("passExpiry", doc.getElementsByTagName("passExpiry").item(0).getTextContent()); 
      userProperties.put("passhash", doc.getElementsByTagName("passhash").item(0).getTextContent()); 
      userProperties.put("salt", doc.getElementsByTagName("salt").item(0).getTextContent()); 
      userProperties.put("passHistory", doc.getElementsByTagName("passHistory").item(0).getTextContent()); 
      userProperties.put("passAlgo", doc.getElementsByTagName("passAlgo").item(0).getTextContent()); 
      userProperties.put("sendEmail", doc.getElementsByTagName("sendEmail").item(0).getTextContent()); 

      String contactNumbers = ""; 
      for (int i = 0 ; i < doc.getElementsByTagName("contactnumbers").getLength(); i++) { 
       if (StringUtils.isNotEmpty(contactNumbers)) { 
        contactNumbers += ","; 
       } 
       contactNumbers += doc.getElementsByTagName("contactnumbers").item(i).getTextContent(); 
      } 
      userProperties.put("contactnumbers", contactNumbers); 
     } catch (ParserConfigurationException e) { 
      throw new UserStoreException("Error while initializing document builder", e); 
     } catch (IOException e) { 
      throw new UserStoreException("Error while parsing Input source", e); 
     } catch (org.xml.sax.SAXException e) { 
      throw new UserStoreException("Error while parsing Input source", e); 
     } 
    } 

    return userProperties; 
} 

protected Connection getDBConnection() throws SQLException, UserStoreException { 
    return null; 
} 

protected boolean isExistingJDBCRole(RoleContext context) throws UserStoreException { 

    return false; 
} 

public boolean doCheckExistingUser(String userName) throws UserStoreException { 
    if (userProperties != null) { 
     return true; 
    } else { 
     return false; 
    } 
} 

public String[] getUserListOfJDBCRole(RoleContext ctx, String filter) throws UserStoreException { 

    String [] user = null; 
    if (userProperties != null) { 
     user = new String[]{userProperties.get("loginName")}; 
    } 
    return user; 
} 

public RoleDTO[] getRoleNamesWithDomain(boolean noHybridRoles) throws UserStoreException { 
    return null; 
} 

public String[] doGetExternalRoleListOfUser(String userName, String filter) throws UserStoreException { 
    return null; 
} 

@Override 
protected String[] doGetSharedRoleListOfUser(String userName, 
              String tenantDomain, String filter) throws UserStoreException { 
    return null; 
} 

public String[] getRoleListOfUser(String userName) throws UserStoreException { 
    return new String[]{"Internal/everyone"}; 
} 

public boolean isRecurssive() { 
    return false; 
} 

}

请建议。

这是我点击登录按钮后的日志: TID:[-1234] [] [2017-01-24 16:35:30,315] DEBUG {org.wso2.carbon.identity.application.authentication。 framework.util.FrameworkUtils} - 认证上下文为空 TID:[-1234] [] [2017-01-24 16:35:30,318] DEBUG {org.wso2.carbon.identity.application.authentication.framework.handler。 request.impl.DefaultAuthenticationRequestHandler} - 在认证流 TID:[-1234] [] [2017年1月24日16:35:30318] DEBUG {org.wso2.carbon.identity.application.authentication.framework.handler.sequence .impl.DefaultStepBasedSequenceHandler} - 执行步骤基于认证... TID:[-1234] [] [2017年1月24日16:35:30318] DEBUG {org.wso2.carbon.identity.application.authentication.framework .handler.sequence.impl。 DefaultStepBasedSequenceHandler} - 启动步骤:1个 TID:[-1234] [] [2017年1月24日16:35:30319] DEBUG {org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils} - 查找步骤 TID的已经认证IDPS:[-1234] [] [2017年1月24日16:35:30320] DEBUG {org.wso2.carbon.identity.application.authentication.framework.handler.step.impl.DefaultStepHandler } - 收到外部响应 TID:[-1234] [] [2017-01-24 16:35:30,320] DEBUG {org.wso2.carbon.identity.application.authentication.framework.handler.step。 impl.DefaultStepHandler} - BasicAuthenticator可以处理请求。 TID:[-1234] [] [2017-01-24 16:35:30,325]错误{org.wso2.carbon.user.core.common.AbstractUserStoreManager} - java.lang.ClassCastException:org.wso2.carbon。 utils.Secret不能转换为java.lang.String TID:[-1234] [] [2017-01-24 16:35:30,327] DEBUG {org.wso2.carbon.user.core.common.AbstractUserStoreManager} - 验证失败。提供错误的用户名或密码。 工具:[-1234] [] [2017-01-24 16:35:30,328] DEBUG {org.wso2.carbon.identity.application.authenticator.basicauth。BasicAuthenticator} - 用户验证失败,原因凭据无效

回答

-1

您可以添加调试日志如下的/repository/conf/log4j.properties文件

log4j.logger.org.wso2.carbon.user.core = DEBUG log4j.logger.org.wso2.carbon.identity.application.authenticator.basicauth.BasicAuthenticator = DEBUG

附加wso2carbon.log文件。

如果您还可以附加自定义,则效果更好。

我认为你可以为你的用例编写一个自定义的身份验证器。

感谢
Isura

+0

喜Isura,我已经添加了日志以及自定义用户存储经理为您reference.Could请你告诉/说明为什么它不打算自定义用户存储,而这是怎么回事到AbstractUserStoreManager – Raj