gpt4 book ai didi

java - 如何拒绝来自 iframe 的站点访问?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:32:33 29 4
gpt4 key购买 nike

出于安全原因,我注意到一些网站拒绝从 iFrame 访问其注册和登录页面。我认为这是个好主意。

我想知道他们需要什么设置才能做到这一点,因为我想在我的网站上做同样的事情。有问题的网站是用 Java 构建的,并在 Apache Tomcat 上运行。

如果有人知道这是如何完成的,那么如果您能分享就太好了。

最佳答案

这就是我使用的并且有效。我从这里得到了一切:OWASP Clickjacking protection in java

在 web.xml 中,添加以下内容之一,具体取决于您要执行的策略:

<display-name>OWASP ClickjackFilter</display-name>
<filter>
<filter-name>ClickjackFilterDeny</filter-name>
<filter-class>org.owasp.filters.ClickjackFilter</filter-class>
<init-param>
<param-name>mode</param-name>
<param-value>DENY</param-value>
</init-param>
</filter>

<filter>
<filter-name>ClickjackFilterSameOrigin</filter-name>
<filter-class>org.owasp.filters.ClickjackFilter</filter-class>
<init-param>
<param-name>mode</param-name>
<param-value>SAMEORIGIN</param-value>
</init-param>
</filter>

<!-- use the Deny version to prevent anyone, including yourself, from framing the page -->
<filter-mapping>
<filter-name>ClickjackFilterDeny</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- use the SameOrigin version to allow your application to frame, but nobody else
<filter-mapping>
<filter-name>ClickjackFilterSameOrigin</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->

...

然后在java代码中:

public class ClickjackFilter implements Filter 
{

private String mode = "DENY";

/**
* Add X-FRAME-OPTIONS response header to tell IE8 (and any other browsers who
* decide to implement) not to display this content in a frame. For details, please
* refer to http://blogs.msdn.com/sdl/archive/2009/02/05/clickjacking-defense-in-ie8.aspx.
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse res = (HttpServletResponse)response;
//If you have Tomcat 5 or 6, there is a known bug using this code. You must have the doFilter first:
chain.doFilter(request, response);
res.addHeader("X-FRAME-OPTIONS", mode );
//Otherwise use this:
//res.addHeader("X-FRAME-OPTIONS", mode );
//chain.doFilter(request, response);

}

public void destroy() {
}

public void init(FilterConfig filterConfig) {
String configMode = filterConfig.getInitParameter("mode");
if ( configMode != null ) {
mode = configMode;
}
}

关于java - 如何拒绝来自 iframe 的站点访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8397267/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com