首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拒绝在Thymeleaf中显示图像

拒绝在Thymeleaf中显示图像
EN

Stack Overflow用户
提问于 2019-08-31 08:28:42
回答 1查看 877关注 0票数 0

我有一个SpringBoot 2.1.7 SpringBoot应用程序。使用Thymeleaf模板引擎,我把这段代码放在Thymeleaf模板中,但是图像没有显示出来。

代码语言:javascript
复制
<object data="assets/img/icons/ico_status_up.svg" type="image/svg+xml">
    <img th:src="@{assets/img/icons/ico_status_up.png}"  alt="UP">
</object>

我在浏览器的控制台中看到了这个错误:

代码语言:javascript
复制
Refused to display 'http://localhost:8080/bonanza/pecador/assets/img/icons/ico_status_up.svg' in a frame because it set 'X-Frame-Options' to 'deny'.

我在我的项目中使用spring安全:

代码语言:javascript
复制
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <!-- Spring Security -->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>

   <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-31 08:45:13

问题是,您可能正在使用自配置的Spring安全模块。

正如在弹簧安全文档中解释的那样:

解决点击劫持问题的一种更现代的方法是使用X帧选项标头: X帧选项:拒绝 X框架选项响应头指示浏览器防止在响应中使用此标头的任何站点在框架内呈现。默认情况下,安全性禁用iframe.中的呈现。

因此,要配置这种行为,您应该配置Security:

代码语言:javascript
复制
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        // ...
        .headers()
            .frameOptions()
                .sameOrigin(); // or disable();
    }
}

此外,我鼓励您考虑这样的问题,其中包含了如何使用html标记来显示SVG - 为了SVG文件?的主题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57736409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档