solr 增加账号密码

背景

当我们既不想将solr服务端口号加入防火墙,又不想将solr服务放开时,可给solr服务设置密码
然而solr管理界面没有增加修改用户名和密码的操作,所以本文介绍一下通过配置文件给solr服务添加密码。

操作步骤

创建role.properties文件

solr-6.5.0_k\server\etc 下创建文件,并添加一下配置

#    
# 这个文件定义用户名,密码和角色  
#    
# 格式如下    
#  <username>: <password>[,<rolename> ...]      
jiug: 111111,admin

修改 solr-jetty-context.xml 文件

solr-6.5.0_k/server/contexts 目录下

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath"><Property name="hostContext" default="/solr"/></Set>
  <Set name="war"><Property name="jetty.base"/>/solr-webapp/webapp</Set>
  <Set name="defaultsDescriptor"><Property name="jetty.base"/>/etc/webdefault.xml</Set>
  <Set name="extractWAR">false</Set>
  <!-- 配置账号密码 -->
  <Get name="securityHandler">
         <Set name="loginService">
                 <New class="org.eclipse.jetty.security.HashLoginService">
                         <Set name="name">jiugRealm</Set>  <!-- 名字与下文保存一致即可 -->
                        <Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/role.properties</Set>
                 </New>
         </Set>
  </Get>
</Configure>

修改 web.xml 文件

solr-6.5.0_k/server/solr-webapp/webapp/WEB-INF 目录下
在前添加一下配置

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Solr</web-resource-name> <!--描述-->
        <url-pattern>/</url-pattern>               <!-- 验证的网页的位置-->
      </web-resource-collection>
       <auth-constraint>
          <role-name>admin</role-name>   <!-- 验证的角色,别写成用户名,如有多个角色可以写多个role-name 标签-->
       </auth-constraint>
   </security-constraint>
   <login-config>
          <auth-method>BASIC</auth-method>            <!-- 关键-->
          <realm-name>TestRealm</realm-name>
  </login-config>

完成配置重启服务

再次访问则需要输入账号密码!
在这里插入图片描述

程序访问solr

修改完配置之后,程序访问solr会报401,权限验证不通过
需要进行一下修改才能访问solr

修改访问URL

# 原先http\://127.0.0.1\:8983/solr/jiug
http\://jiug:111111@127.0.0.1\:8983/solr/jiug

引入依赖

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.7</version>
</dependency>

完成以上操作程序即可成功访问solr!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Solr客户端连接验证通常是通过配置文件实现的,而不是通过Java代码实现的。但是,如果您想通过Java代码实现验证,可以考虑以下步骤: 1. 创建一个实现SolrClient接口的自定义类,例如MySolrClient。 2. 在MySolrClient类中添加一个带有用户名和密码参数的构造函数。在该构造函数中,使用用户名和密码创建一个HttpSolrClient对象,并将其赋值给MySolrClient的实例变量。 3. 在MySolrClient类中添加一个验证方法,例如authenticate()。在该方法中,尝试连接Solr服务器并执行一个简单的查询。如果查询成功,返回true,否则返回false。 4. 在MySolrClient类的其他方法中,首先调用authenticate()方法来确保客户端已经通过验证。如果验证失败,则拒绝执行后续操作。 下面是一个示例代码: ``` import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.impl.SolrClientImpl; public class MySolrClient extends SolrClientImpl { private String username; private String password; public MySolrClient(String baseUrl, String username, String password) { super(baseUrl); this.username = username; this.password = password; HttpSolrClient.Builder builder = new HttpSolrClient.Builder(baseUrl); builder.withHttpClient(new CustomHttpClient(username, password)); this.solrClient = builder.build(); } public boolean authenticate() { try { this.solrClient.ping(); return true; } catch (Exception e) { return false; } } public void addBean(Object obj) throws IOException, SolrServerException { if (!authenticate()) { throw new SolrServerException("Authentication failed"); } super.addBean(obj); } // other methods... private class CustomHttpClient extends CloseableHttpClient { private CloseableHttpClient delegate; private String username; private String password; public CustomHttpClient(String username, String password) { this.delegate = HttpClients.createDefault(); this.username = username; this.password = password; } @Override public CloseableHttpResponse execute(HttpUriRequest request) throws IOException { if (request instanceof HttpPost) { HttpPost post = (HttpPost) request; post.setHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes())); } return delegate.execute(request); } // other methods... } } ``` 在这个示例中,我们创建了一个名为MySolrClient的类,继承自SolrClientImpl,并实现了authenticate()方法来验证客户端。我们还重写了addBean()方法,并在其中首先调用authenticate()方法来确保客户端已通过验证。我们还创建了一个CustomHttpClient类来处理HTTP请求,并在其中添加了基本身份验证头。最后,我们在MySolrClient的构造函数中创建了一个CustomHttpClient对象,并将其传递给HttpSolrClient.Builder。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

必成公

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值