生成访问Solr后台时用于授权登录的证书密码,官方给的信息是可以通过“sha256(password+salt) hash”加密,以下是个人结合官方资料编写的帮助类:
创建security.json的详细步骤我就不贴了,有需要可以参考以下官方资源:
官方地址:Basic Authentication Plugin | Apache Solr Reference Guide 6.6
附上security.json的文件内容,默认密码是SolrRocks:
{
"authentication": {
"blockUnknown": true,
"class": "solr.BasicAuthPlugin",
"credentials": {
"solr": "IV0EHq1OnNrj6gvRCwvFwTrZ1+z1oBbnQdiVC3otuq0= Ndd7LKvVBAaZIF0QAVi1ekCfAJXr1GGfLtRUXhgrF8c="
}
},
"authorization": {
"class": "solr.RuleBasedAuthorizationPlugin",
"permissions": [
{
"name": "security-edit",
"role": "admin"
}
],
"user-role": {
"solr": "admin"
}
}
}
下面是生成solr密码的代码段:
package com.solr;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
import org.apache.commons.codec.binary.Base64;
public class SolrSecurity {
public static void main(String[] args) {
// 密码
String password = "SolrRocks";
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-256");
final Random random = new SecureRandom();
byte[] salt = new byte[32];
random.nextBytes(salt);
digest.reset();
digest.update(salt);
byte[] btPass = digest.digest(password.getBytes(StandardCharsets.UTF_8));
digest.reset();
btPass = digest.digest(btPass);
System.out.println(Base64.encodeBase64String(btPass) + " " + Base64.encodeBase64String(salt));
} catch (NoSuchAlgorithmException e) {
System.err.println("Unknown algorithm: " + e.getMessage());
}
}
}
覆盖原密码后重新启动Solr服务,希望可以帮到各位。