solr 增加账号密码,solr 6.5 配置用户名密码

发布时间:2018-05-12作者:laosun阅读(5063)

solr

linux 系统 solr 配置增加用户名密码,配置账号密码,Solr在5.0版本后,不再提供war包部署的方式

    solr没有在管理界面增加修改用户名和密码的操作,那么我们在服务器配置完成后,总不能暴露在外吧!所以博主整理了下资料,可以通过配置文件增加用户名和密码。


    进入这个文件下,/Users/sun/Documents/d/solr-6.5.1/server/etc,创建一个role.properties文件。

    内容如下:

    #    
    # 这个文件定义用户名,密码和角色  
    #    
    # 格式如下    
    #  <username>: <password>[,<rolename> ...]    
    #    
    #userName: password,role    
    xxxxx: xxxxxx,xxxxxx   #例如:test:admin123,admin

    然后进入 /Users/sun/Documents/d/solr-6.5.1/server/contexts 这个文件,修改下边的 solr-jetty-context.xml

    <?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">TestRealm</Set>  <!-- 这仅仅是一个名字而已-->
                            <Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/role.properties</Set>
                     </New>
             </Set>
      </Get>
    </Configure>

    进入:/Users/sun/Documents/d/solr-6.5.1/server/solr-webapp/webapp/WEB-INF 修改 web.xml

    Solr在5.0版本后,不再提供war包部署的方式,所以直接找上边这个目录即可。

    <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>
      #在文件最后 </web-app> 之前增加即可

    配置完成,重启服务!再访问的时候浏览器就会提示需要账号密码才能进入,如下图所示:

    image.png

    那么在程序中使用的时候,如何输入账号和密码呢? 看下边

    url 格式修改成下边这种:

    http://test:admin123@localhost:8983/solr/my_core 

    引入必备依赖:

    <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
    <dependency> 
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.7</version>
    </dependency>


    相关阅读:

    linux 系统安装 solr,以及各版本的下载地址

    solr 6.5 配置中文分词 IK Analyzer

    Java 对 solr 的增删改查,java源码


4 +1

版权声明

 Java  solr

 请文明留言

0 条评论