SSO Implementation (Java Class Development)

Get option fields implementation example:

@Override

          public ArrayList<AuthEntity> getOptionFields() {
                      // set option fields
                      ArrayList<AuthEntity> entities = new ArrayList<AuthEntity>();
                      AuthEntity entity = null;
                     
                      entity = new AuthEntity(AuthEntity.AUTH_FIELD, "datasource");
                      entity.appendInfo("type", "datasource");
                      entity.appendInfo("label", "Data connection");
                      entity.appendInfo("description", "Database connection");
                      entity.appendInfo("required", true);
                      entities.add(entity);
                     
                      entity = new AuthEntity(AuthEntity.AUTH_FIELD, "sql_groups");
                      entity.appendInfo("type", "textarea");
                      entity.appendInfo("label", "GroupList SQL");
                      entity.appendInfo("description", "Group list sql\n\nEx) SELECT DISTINCT department_id, department_description FROM fm_department WHERE department_description like lower(?) ORDER BY department_id");
                      entity.appendInfo("required", true);
                      entities.add(entity);
                     
                      entity = new AuthEntity(AuthEntity.AUTH_FIELD, "sql_roles");
                      entity.appendInfo("type", "textarea");
                      entity.appendInfo("label", "RoleList SQL");
                      entity.appendInfo("description", "Role list sql\n\nEx) SELECT DISTINCT management_role FROM fm_position WHERE management_role like lower(?) ORDER BY management_role");
                      entity.appendInfo("required", true);
                      entities.add(entity);
                     
                      entity = new AuthEntity(AuthEntity.AUTH_FIELD, "sql_users");
                      entity.appendInfo("type", "textarea");
                      entity.appendInfo("label", "UserList SQL");
                      entity.appendInfo("description", "User list sql\n\nEx) "
                                              + "select n10.employee_id AS emp_id, \n"
                                              + "n10.full_name AS uname, \n"
                                              + "lower(n10.first_name || '.' || n10.last_name || '@example.com') AS email,\n"
                                              + "lower(n10.first_name || '.' || n10.last_name || '@example.com') AS userid,\n"
                                              + "'example.com' AS upasswd,\n"
                                              + "n11.department_description AS groupname,\n"
                                              + "n12.management_role AS dutyname\n"
                                              + "from fm_employee n10\n"
                                              + "inner join fm_department n11 on n10.department_id=n11.department_id\n"
                                              + "left outer join fm_position n12 on n10.position_id=n12.position_id\n"
                                              + "WHERE lower(n10.full_name) like lower(?) ORDER BY emp_id");
                      entities.add(entity);
                     
                      entity = new AuthEntity(AuthEntity.AUTH_FIELD, "sql_validate");
                      entity.appendInfo("type", "textarea");
                      entity.appendInfo("label", "User validation SQL");
                      entity.appendInfo("description", "User login check sql");
                      entity.appendInfo("required", false);
                      entities.add(entity);
                     
                      return entities;
          }

 

Get group lists implementation example:

@Override
          public ArrayList<AuthEntity> getGroupList(String filter, HashMap<String, Object> iprop) throws Exception {
                      ArrayList<AuthEntity> entities = null;
                     
                      Connection dbcon = (Connection) iprop.get("datasource");
                      String sql = (String) iprop.get("sql_groups");
                      if (filter != null && filter.length() > 0)
                      {
                                  filter = filter.toLowerCase() + "%";
                      }
                      else
                      {
                                  filter = "%";
                      }
                      Object[] params = new Object[]{filter};
                      String[] additional_parameters = new String[] {"department_id"};
                      entities = getAuthEntity(dbcon, AuthEntity.AUTH_GROUP, sql, params, "department_description", additional_parameters, null);
                     
                      return entities;
          }

Get Role list implementation example:

@Override
          public ArrayList<AuthEntity> getRoleList(String filter, HashMap<String, Object> iprop)
                      throws Exception
          {
                      ArrayList<AuthEntity> entities = null;
                     
                      Connection dbcon = (Connection) iprop.get("datasource");
                      String sql = (String) iprop.get("sql_roles");
                      if (filter != null && filter.length() > 0)
                      {
                                  filter = filter.toLowerCase() + "%";
                      }
                      else
                      {
                                  filter = "%";
                      }
                      Object[] params = new Object[]{filter};
                      entities = getAuthEntity(dbcon, AuthEntity.AUTH_ROLE, sql, params, "management_role", null, null);
                      return entities;
          }

Get user lists implementation example:

@Override
          public ArrayList<AuthEntity> getUserList(String filter, HashMap<String, Object> iprop)
                      throws Exception
          {
                      ArrayList<AuthEntity> entities = null;
                      Connection dbcon = (Connection) iprop.get("datasource");
                      String sql = (String) iprop.get("sql_users");
                     
                      if (filter != null && filter.length() > 0)
                      {
                                  filter = filter + "%";
                      }
                      else
                      {
                                  filter = "%";
                      }
                      Object[] params = new Object[]{filter};
                      String[] additional_parameters = new String[] {"emp_id", "email", "uname", "upasswd", "groupname"};
                      String[] list_parameters = new String[] {"dutyname"};
                      entities = getAuthEntity(dbcon, AuthEntity.AUTH_USER, sql, params, "userid", additional_parameters, list_parameters);
                     
                      return entities;
          }

User SQL need to implement return column email, uname, upasswd, groupname, dutyname.

Login validation:

public String beforeLogin(String userid, String password, HashMap<String, Object> iprop, HashMap<String, String> secfilter)
          {
                      Connection con = null;
                      PreparedStatement pstmt = null;
                      ResultSet rs = null;
                     
                      String uid = null;
                      System.out.println("tryLogin Extra");
                     
                      String mts = "0122483f-0155fb46";
                     
                      try
                      {
                                  if (userid != null && userid.startsWith("sso_sim_b6118e61573e4aaa_key_map:") == true)
                                  {
                                              this.setSecProp(iprop);
                                             
                                              HttpServletRequest req = (HttpServletRequest) iprop.get("request");
                                              HttpSession session = req.getSession(true);
                                                                                 
                                              //Object _osess = session.getAttribute("__sso_info"); //-- session
                                               
                                              Object _osess = session.getAttribute("__sso_info");
 
                                              if (_osess != null)
                                              {
                                                          String t = _osess.toString();
                                                          System.out.println(t);
                                                          String muserid = t.substring(t.indexOf(":") + 1);
 
                                                          System.out.println(">> login with ID : " + muserid);
                                                         
                                                          uid = getRegisteredUserUUID(mts, muserid);
                                              }
                                  }
                      }
                      catch (Exception e)
                      {
                                  e.printStackTrace();
                      }
                      finally
                      {
                                  try
                                  {
                                              if (rs != null)
                                              {
                                                          rs.close();
                                              }
                                              rs = null;
                                             
                                              if (pstmt != null)
                                              {
                                                          pstmt.close();
                                              }
                                              pstmt = null;
                                             
                                              if (con != null)
                                              {
                                                          con.close();
                                              }
                                  }
                                  catch (Exception e)
                                  {
                                              con = null;
                                  }
                      }
                     
                      return uid;
          }