他的回复:
认证代码public class LoadProperties { private static String ipPattern = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)"; private static Properties properties = new Properties(); public static String keytabPath; public static final String CONFIGURATION_FILE_NAME = "esParams.properties"; public static final String FUSIONINSIGHT_ES_PARAM_LOCATION = "fusioninsight_es_param_location"; public static final String FUSIONINSIGHT_ES_PARAM_LOCATION_DEFAULT = "/data/app/zjaudit/fi-authorization"; private static String configPath; public static Configuration loadProperties() throws IOException { initProperties(); Configuration configuration = new Configuration(); configuration.setClusterName(loadClusterName()); configuration.setTransportAddress(loadTransportAddress()); configuration.setSecureMode(loadIsSecureMode()); if (configuration.isSecureMode()) { configuration.setPrincipal(loadPrincipal()); configuration.setKeyTabPath(loadPath("keytabPath")); configuration.setKrb5Path(loadPath("krb5Path")); keytabPath = configuration.getKeyTabPath(); setSecurityConfig(configuration.getPrincipal(), keytabPath, configuration.getKrb5Path()); } configuration.setSniff(loadIsSniff()); log.info("configuration:" + configuration); return configuration; } private static void initProperties() { try { configPath = System.getProperty(FUSIONINSIGHT_ES_PARAM_LOCATION, FUSIONINSIGHT_ES_PARAM_LOCATION_DEFAULT) + File.separator + CONFIGURATION_FILE_NAME; properties.load(new FileInputStream(configPath)); } catch (Exception e) { log.error("Failed to load properties file {}.", configPath); throw new IllegalArgumentException(); } } public static String loadClusterName() { String clusterName = properties.getProperty("clusterName"); if (null == clusterName || clusterName.isEmpty()) { log.error("clusterName is empty, please configure it in {}", configPath); throw new IllegalArgumentException(); } return clusterName; } private static Set loadTransportAddress() { String serverHosts = properties.getProperty("esServerHosts"); if (null == serverHosts || serverHosts.isEmpty()) { log.error("Please configure esServerHosts in {}.", configPath); log.error("The format of esServerHosts is ip1:port1,ip2:port2,ipn,portn"); throw new IllegalArgumentException(); } String[] hosts = serverHosts.split(","); Set transportAddresses = new HashSet(hosts.length); for (String host : hosts) { String[] ipAndPort = host.split(":"); String esClientIP = ipAndPort[0]; String esClientPort = ipAndPort[1]; if (!Pattern.matches(ipPattern, esClientIP)) { log.error("The configuration clientIP format is wrong, please configure it in {}.", configPath); throw new IllegalArgumentException(); } if (null == esClientPort || esClientPort.isEmpty()) { log.error("The configuration esClientIPPort is empty, please configure it in {}.", configPath); throw new IllegalArgumentException(); } try { transportAddresses.add(new TransportAddress(InetAddress.getByName(esClientIP), Integer.valueOf(esClientPort))); } catch (Exception e) { log.error("Init esServerHosts occur error : {}", e.getMessage()); throw new IllegalArgumentException(); } } return transportAddresses; } private static String loadPath(String path) { String loadedPath = properties.getProperty(path); if (null == loadedPath || loadedPath.isEmpty()) { loadedPath = System.getProperty(FUSIONINSIGHT_ES_PARAM_LOCATION, FUSIONINSIGHT_ES_PARAM_LOCATION_DEFAULT) + File.separator; log.warn(path + " is empty, using the default path [{}].", loadedPath); } return loadedPath; } private static boolean loadIsSecureMode() { return !properties.getProperty("isSecureMode").equals("false"); } private static boolean loadIsSniff() { return !properties.getProperty("isSniff").equals("false"); } private static String loadPrincipal() { String principal = properties.getProperty("principal"); if (null == principal || principal.isEmpty()) { log.error("Please configure principal in {}.", configPath); throw new IllegalArgumentException(); } return principal; } private static void setSecurityConfig(String principal, String keytabPath, String krb5Path) throws IOException { LoginUtil.setJaasFile(principal, keytabPath + "user.keytab"); System.setProperty("es.security.indication", "true"); LoginUtil.setKrb5Config(krb5Path + "krb5.conf"); } }