春天引导安全性的静态资源
问题描述:
我写的应用程序在启动春季,春季安全与Thymeleaf,我试图让我的访问静态资源文件...春天引导安全性的静态资源
这是我的项目结构...
3210我在模板/ index.html的HTML文件,其中我尝试使用标签
<img src="/praca.jpg" alt="sd"/>
为什么我总是得到404错误?我在哪里做错了什么?
我一般初始化类:
@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(InzynierkaApplication.class, args);
}
}
我的安全类:
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private UserAuthenticationDetails userAuthenticationDetails;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userAuthenticationDetails);
auth.authenticationProvider(authenticationProvider());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(userAuthenticationDetails);
authenticationProvider.setPasswordEncoder(passwordEncoder());
return authenticationProvider;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/","/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.usernameParameter("username")
.passwordParameter("password")
.defaultSuccessUrl("/",true)
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutUrl("/logout")
.logoutSuccessUrl("/login?logout")
.invalidateHttpSession(true);
}
}
答
在模板中,你需要使用thymeleaf格式自行自动添加上下文。使用此:
<img th:src="@{/praca.jpg}" alt="sd"/>
/praca.jpg
应该从静态或公用文件夹
+0
它不适合我。 – technics
答
我也有类似的问题,这个问题到图像的完整路径与春季安全,所以任何有此问题的人都可以尝试将其静态文件所在的文件夹添加到不需要身份验证的网址列表中 例如antMatchers("/css/**", "/fonts/**").permitAll()
只需确认,整个页面返回一个404或只是图像资产? –
404只返回图片资源。其他内容载入成功。 – technics