001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.http;
019
020import java.io.IOException;
021import java.util.HashMap;
022import java.util.Map;
023import javax.servlet.Filter;
024import javax.servlet.FilterChain;
025import javax.servlet.FilterConfig;
026import javax.servlet.ServletException;
027import javax.servlet.ServletRequest;
028import javax.servlet.ServletResponse;
029import javax.servlet.http.HttpServletResponse;
030import org.apache.hadoop.conf.Configuration;
031import org.apache.hadoop.hbase.HBaseInterfaceAudience;
032import org.apache.yetus.audience.InterfaceAudience;
033
034@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
035public class ClickjackingPreventionFilter implements Filter {
036  private FilterConfig filterConfig;
037  private static final String DEFAULT_XFRAMEOPTIONS = "DENY";
038
039  @Override
040  public void init(FilterConfig filterConfig) throws ServletException {
041    this.filterConfig = filterConfig;
042  }
043
044  @Override
045  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
046    throws IOException, ServletException {
047    HttpServletResponse httpRes = (HttpServletResponse) res;
048    httpRes.addHeader("X-Frame-Options", filterConfig.getInitParameter("xframeoptions"));
049    chain.doFilter(req, res);
050  }
051
052  @Override
053  public void destroy() {
054  }
055
056  public static Map<String, String> getDefaultParameters(Configuration conf) {
057    Map<String, String> params = new HashMap<>();
058    params.put("xframeoptions",
059      conf.get("hbase.http.filter.xframeoptions.mode", DEFAULT_XFRAMEOPTIONS));
060    return params;
061  }
062}