Thymeleaf显示base64字符串为图片
时间: 2023-12-30 19:06:17 浏览: 246
要在Thymeleaf中将Base64字符串显示为图片,可以使用以下步骤:
1. 在HTML文件中添加以下代码:
```html
<img th:src="@{'data:image/png;base64,' + ${base64String}}" />
```
其中,`base64String`是你要显示的Base64字符串。
2. 确保将`thymeleaf-extras-springsecurity5`依赖项添加到项目中。这是因为在显示Base64字符串时,需要使用Spring Security对数据进行转义。
3. 在Spring Security配置文件中添加以下代码:
```java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers().frameOptions().disable();
http.csrf().disable();
http.authorizeRequests().antMatchers("/h2-console/**").permitAll();
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public SpringSecurityDialect springSecurityDialect() {
return new SpringSecurityDialect();
}
}
```
这将禁用Spring Security的CSRF保护和X-Frame-Options,以便在浏览器中正确显示图片。
4. 在application.properties文件中添加以下代码:
```properties
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false
```
这将确保Thymeleaf正确地解析HTML文件并显示Base64字符串作为图片。
完成以上步骤后,在Thymeleaf中就可以将Base64字符串显示为图片了。
阅读全文
相关推荐
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![exe](https://img-home.csdnimg.cn/images/20241231044909.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)