Thursday 21 November 2013

JAVA: Reading Weblogic Username/Password from the boot.properties file

In order to create a JMX connection to the WLS you must provide a username/password. If you are deploying a webapp to the server and want to read the properties you can use the following methods.

public Map init() {

Map<String, String> userCredentials = new HashMap<String, String>();
       //FIND THE PATH THIS CLASS FILE IS DEPLOYED TO
      Class cls = this.getClass();
      String path = cls.getProtectionDomain().getCodeSource().getLocation().getPath();
      String decodedPath = "FAIL";
       //STRIP THE PATH BACK TO /servers/
try {
              decodedPath = URLDecoder.decode(path, "UTF-8");
             decodedPath = decodedPath.substring(0, decodedPath.indexOf("/servers/"));
     } catch (Exception ex) {
             System.out.println(ex);
     }
//Change domain to aserver from mserver -- DEPENDING ON YOUR DOMAIN SETTINGS
      String domain = decodedPath.replaceAll("mserver", "aserver");

      //GET A STRING OF THE CONTENTS OF THE BOOT.PROPERTIES USING THE readBootProperties method
            String[] wlsbootProperties = readBootProperties(domain);
            String username = "";
            String password = "";
            //Find Username / Password strings
                     for (int i = 0; i < wlsbootProperties.length; i++) {
                if (wlsbootProperties[i].indexOf("username") > -1) {
                    username = wlsbootProperties[i];
                } else if (wlsbootProperties[i].indexOf("password") > -1) {
                    password = wlsbootProperties[i];
                }
            }

            //Strip back to GET AES STRINGS
            username =
                    username.substring(username.indexOf("{AES}"), username.length());
            password =
                    password.substring(password.indexOf("{AES}"), password.length());


            try { //CLEAN OUT BACKSLASHES
                while (username.indexOf("\\") > -1) {
                    username = username.replace("\\", "");
                }
                while (password.indexOf("\\") > -1) {
                    password = password.replace("\\", "");
                }
                           //DECRYPT
                String decryptUser =
                    new weblogic.security.internal.encryption.ClearOrEncryptedService(weblogic.security.internal.SerializedSystemIni.getEncryptionService(domain)).decrypt(username);
                           //DECRYPT
                String decryptPass =
                    new weblogic.security.internal.encryption.ClearOrEncryptedService(weblogic.security.internal.SerializedSystemIni.getEncryptionService(domain)).decrypt(password);
                           //STORE & Return
                userCredentials.put("username", decryptUser);
                userCredentials.put("password", decryptPass);

                return userCredentials;

            } catch (Exception ex) {
                System.out.print(ex);


            }
       
        return userCredentials;

    }

/**
     *  reads the boot properties from the domaindir
     */
    public String[] readBootProperties(String domainDir) {
        String str = "";
        File file =
            new File(domainDir + "/servers/AdminServer/security/boot.properties");
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(file);

            int content;

            while ((content = fis.read()) != -1) {
                // convert to char and display it
                str += (char)content;
                //System.out.print((char) content);
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return str.split("\n");
    }
Depending on your domain installation you might need to change a few strings to get the
readBootProperties(String domainDir)
Reading the correct files

1 comment:

  1. Thank you very much. I was looking for this kind of code for my problem.

    ReplyDelete