【问题标题】:Change x-frame-options header for a single controller in Spring Boot在 Spring Boot 中更改单个控制器的 x-frame-options 标头
【发布时间】:2019-03-07 17:42:59
【问题描述】:

假设您有一个 Spring Boot 应用程序,并且您只想创建一个可以嵌入为 iFrame 的页面。所有其他页面仍应具有默认的 x-frame-options: deny 标头。

  • 我找不到相应的注解(我希望有类似 @CrossOrigin 的注解,但对于标头是通用的)
  • 我尝试通过 httpServletResponse 更改标头,但之后似乎安全标头被覆盖
  • 我尝试使用 http.antmatcher("/controller").frameOptions().disable() 但这会破坏我的其余身份验证 - 我错过了 .allow(domain) 方法

我知道我可以创建一些过滤器代码,但我希望有一个更简单的解决方案。

有什么想法吗?

【问题讨论】:

标签: spring spring-boot spring-security x-frame-options


【解决方案1】:

将这些标头设置为响应对我有用。

response.setHeader("X-Frame-Options", "SAMEORIGIN");
response.setHeader("Content-Security-Policy", " frame-ancestors 'self'");

X-Frame-Options 已被 Content-Security-Policy (frame-ancestors) 取代,但某些浏览器不支持它,因此最好同时设置目前。 参考:https://infosec.mozilla.org/guidelines/web_security#x-frame-options

【讨论】:

    【解决方案2】:

    仅允许特定控制器使用 iframe 选项,而不允许所有网站使用它,这是我的方法:

     @RequestMapping("/someiframepath")
        public String iframe(HttpServletResponse response, Model model) {
            response.setHeader("X-Frame-Options", "");
            .... DO SOMETHING ....
            return "your view";
        }
    

    希望对你有帮助!

    【讨论】:

    • 谢谢!我认为这确实是我正在寻找的解决方案!
    • 这对我不起作用。 Spring 安全性优先,所以我的标题被忽略了。有什么建议?使用spring boot 1.5.18
    猜你喜欢
    • 2016-05-29
    • 2017-02-09
    • 2013-11-16
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 2021-12-10
    • 2015-04-23
    • 2014-11-30
    相关资源
    最近更新 更多