2017-04-03 121 views
0

我有一个关于Elasticsearch的问题。我使用Search Guard作为传输层安全性,当我想从elasticsearch读取数据时,我必须提供用户名和密码。就像这样:访问Elasticsearch时如何哈希密码

import org.apache.spark.{SparkConf, SparkContext} 

import org.elasticsearch.spark._ 

object ReadDataFromES { 
def main(args: Array[String]) { 
    val conf = new SparkConf() 
    .setAppName("SampleESApp") 
    .set("es.index.auto.create", "true") 
    .set("es.nodes", "localhost:9200") 
    .set("es.net.http.auth.user", "elastic_user") 
    .set("es.net.http.auth.pass", "elastic_password") 
    val sc = new SparkContext(conf) 

    print("Reading data \n\n\n") 
    val RDD = sc.esRDD("bank/account") 
    println(RDD.first()) 
    println("\n\n") 
    /* 
    print("Writing data \n\n\n") 
    RDD.saveToEs("bank/spark") 
    */ 
} 
} 

但问题是,我不希望有密码(es.net.http.auth.pass参数的值)在我的代码纯文本。有谁知道一种方法来散列这个密码?

+0

关于使用环境变量是什么? –

回答

0

您可以将密码存储为加密文本,然后在需要使用密码时将其解密。例如,看到How to encrypt and decrypt String with my passphrase in Java (Pc not mobile platform)?

或者,看到这个答案val encryptedPassword = BCrypt.hashpw(password.trim(),BCrypt.gensalt())

https://stackoverflow.com/questions/43189900/hash-salt-and-save-password-slick

BCrypt可(例如)从春季

http://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/crypto/bcrypt/BCrypt.html