點擊劫持:X-Frame-Options未配置

X-Frame-Options未配置
可以配置的參數有三個:
1.DENY:瀏覽器拒絕當前頁面加載任何Frame頁面。
2.SAMEORIGIN:頁面只能加載入同源域名下的頁面。
3.ALLOW-FROM uri:只能被嵌入到指定域名的框架中。
一般選第二個參數就可以了。

方式一:每個頁面添加設置:

<% response.addHeader("x-frame-options","SAMEORIGIN");%>

方式二:項目代碼中加過濾器設置:

public class FrameFilter implements Filter {
 
 
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;  
    HttpServletResponse response = (HttpServletResponse) res;  
    //設置x-frame-options
    response.setHeader("x-frame-options", "SAMEORIGIN");  
    chain.doFilter(request, response);
    }  
 
    public void init(FilterConfig config) throws ServletException {
    }
    
    public void destroy() {
    }

}

然後web.xml中配置此過濾器,不再贅述。

方式三:tomcat中設置(如果使用的服務器是tomcat,可以在tomcat中設置,tomcat下所有應用都會生效)    

tomcat目錄/conf/web.xml中的找httpHeaderSecurity配置,將其前面的註釋去掉即可。
 <filter-mapping>
        <filter-name>httpHeaderSecurity</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

<filter>
        <filter-name>httpHeaderSecurity</filter-name>
        <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
        <async-supported>true</async-supported>
        // 添加以下代碼start
      <init-param>
        <param-name>antiClickJackingEnabled</param-name>
        <param-value>true</param-value>
      </init-param>
      <init-param>
        <param-name>antiClickJackingOption</param-name>
        <param-value>SAMEORIGIN</param-value>
      </init-param>    //添加end

    </filter>

以上三種方式都可以,是項目情況而定使用哪種!

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