solr權限控制之web界面和Java相關操作

solr權限控制之web界面和Java相關操作


一、在配置solr的時候我們曾在solr的WEB-INF/web.xml中註釋一段代碼,那段代碼就是對權限的控制。只需將註釋代碼更改爲以下即可

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Restrict access to Solr admin</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>DELETE</http-method>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
        <http-method>PUT</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>solr</role-name>
        <role-name>admin</role-name>
    </auth-constraint>
    <user-data-constraint>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>
<login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>default</realm-name>
</login-config>

二、替換之後進行如下操作

//在apache-tomcat-9.0.0.M9/conf下的tomcat-users.xml文件最後增加:
<role rolename="solr"/>  
<user username="admin" password="new-password" roles="solr"/>

以上操作方式來源於網絡,親測可用。


三、對於Java程序的操作

① spring的配置文件中solr.host=admin:123456@IP:端口號/solr/core名稱

//例如:
solr.host=admin:123456@127.0.0.1:8080/solr/new_core

② 可以配置攔截器,進行請求過濾。Solr通過BASIC認證。可以添加請求頭的方式進行認證(未實驗,不提供代碼示例。僅供參考)。

四、定時全量索引和增量索引權限配置

網上有很多關於solr-dataimporthandler-scheduler的jar.在前期的文章中也有說明和源碼提供下載。但是源碼中由於原作者的編碼風格(不知道第幾任作者,哈哈),也走了一些彎路。在此說明一下:
日誌的提示級別,會導致在solr的web界面報異常,其實並不是異常信息。在GitHub下載源碼沒有此問題。

對於定時索引機上權限之後如何配置,查看官方文檔沒找到很好的解決辦法,就在solr-dataimporthandler-scheduler.jar進行改造。在配置文件中增加認證字段,在發送請求是進行請求頭的添加(採用BASIC)。

//示例:
#################################################
#                                               #
#       dataimport scheduler properties         #
#                                               #
#################################################
#  to sync or not to sync
#  1 - active; anything else - inactive
syncEnabled=1
#  which cores to schedule
#  in a multi-core environment you can decide which cores you want syncronized
#  leave empty or comment it out if using single-core deployment
syncCores=new_core
#  solr server name or IP address
#  [defaults to localhost if empty]
server=172.0.0.1
#  solr server port
#  [defaults to 80 if empty]
port=8080
#  application name/context
#  [defaults to current ServletContextListener's context (app) name]
webapp=solr
#  URL params [mandatory]
#  remainder of URL
#  增量索引
params=/deltaimport?command=delta-import&clean=false&commit=true
#  schedule interval
#  number of minutes between two runs
#  [defaults to 30 if empty]
interval=1
#  重做索引的時間間隔,單位分鐘,默認7200,即5天; 
#  爲空,爲0,或者註釋掉:表示永不重做索引
reBuildIndexInterval=0
#  重做索引的參數
#  全量索引
reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true
#  重做索引時間間隔的計時開始時間,第一次真正執行的時間=reBuildIndexBeginTime+reBuildIndexInterval*60*1000;
#  兩種格式:2016-012-11 14:10:00 或者  14:10:00,後一種會自動補全日期部分爲服務啓動時的日期
reBuildIndexBeginTime=00:00:00
#  權限認證(用戶名:密碼)
authorization=admin:123456

源碼修改:

//SolrDataImportProperties.java增加
public static final String AUTHORIZATION = "authorization";

//BaseTimerTask.java增加
protected String authorization;
protected void reloadParams() {
        ....
        this.authorization = this.p.getProperty(SolrDataImportProperties.AUTHORIZATION);
    }
    protected void sendHttpPost(String completeUrl, String coreName) {
            //省略前半段......
            URL url = new URL(completeUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("type", "submit");
            conn.setDoOutput(true);
            //增加部分====start====
            if (this.authorization != null){
                String encoding = Base64.encode(this.authorization.getBytes());
                conn.setRequestProperty  ("Authorization", "Basic " + encoding);
            }
            //增加部分======end=====
            conn.connect();

            logger.info(core + "<index update process> Full URL\t\t\t\t" +
                    conn.getURL());
            //省略後半段

solr-dataimporthandler-scheduler源碼
jar包下載

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章