• 二、配置jetty-web.xml
  • 三、配置realm.properties文件
  • 四、重启namenode,使认证生效
  • 五、登录Hadoop Web页面验证

前言

Hadoop 2.x 版本,默认情况下,可以通过 http://ip地址:50070/explorer.html 访问HDFS页面,查看Namenode和Datanode状态,以及HDFS的相关文件等。但是这存在安全隐患,可能导致我们的文件信息的泄露,如果我们在页面里面添加个认证机制,只有验证之后的用户才可以进入页面里操作。

一、修改Hadoop的web.xml配置文件

文件路径: /usr/hdp/2.5.3.0-37/hadoop-hdfs/webapps/hdfs/WEB-INF

执行 cat web.xml 可查看原有配置如下

hadoop代码页面 hadoop管理页面_大数据


编辑web.xml,填入以下内容(这是我的配置, role-name 要根据自己的实际情况配置,我的hadoop用户是在rootgroup下的,所以填root):

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
     http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License. See accompanying LICENSE file.
-->
<web-app version="2.4" xmlns=" http://java.sun.com/xml/ns/j2ee ">
    <!-- Add this towards the bottom of the xml document. -->
    <security-constraint>
 

        <web-resource-collection>
            <web-resource-name>Protected</web-resource-name>
            <url-pattern>/*</url-pattern>    <!--  这是正在被保护的路径,当前设定的是root路径下的web ui -->
        </web-resource-collection>
        <auth-constraint>
            <role-name>root</role-name>   <!--  hadoop用户的组名 -->
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>BASIC</auth-method>  <!-- HTTP规范,Base64 编码方式, 采用Base64编码具有不可读性,需要解码后才能阅读。-->
        <realm-name>jobtrackerRealm</realm-name>  <!-- 这个名称要和下面jetty里的name一致-->
      </login-config>
</web-app>

二、配置jetty-web.xml

和web.xml在同一个文件夹下,如果没有,则新增一个,填入以下内容

<Configure class="org.mortbay.jetty.webapp.WebAppContext">
  <Get name="securityHandler">
    <Set name="userRealm">
      <New class="org.mortbay.jetty.security.HashUserRealm">
        <Set name="name">jobtrackerRealm</Set>  <!-- 这个名称要和上面web里的realm-name一致-->
        <Set name="config">
                <SystemProperty name="hadoop.home.dir"/>/etc/hadoop/realm.properties  <!-- 存放用户和用户密码的地方-->
        </Set>
      </New>
    </Set>
  </Get>
</Configure>

/etc/hadoop/realm.properties是配置用户名密码的文件

三、配置realm.properties文件

在 jetty-web.xml 配置的路径下,/etc/hadoop/realm.properties ,这个文件是没有 的,要先创建。