我用Spring Boot编写应用程序,用Thymeleaf编写Spring Security,并尝试访问我的静态资源文件……
这是我的项目结构..。
.
├── mvnw
├── mvnw.cmd
├── nb-configuration.xml
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ ├── resources
│ │ │ ├── application.properties
│ │ │ ├── static
| | | | |---------------------------------this is image.jpg
│ │ │ ├── templates
│ │ │ └── ValidationMessages.properties
│ │ └── wro
│ │ ├── css
│ │ ├── fonts
│ │ ├── js
│ │ ├── scss
│ │ ├── wro.properties
│ │ └── wro.xml
│ └── test
│ └── java
│ └── com我在template/index.html中有一个HTML文件,我尝试使用标签
<img src="/praca.jpg" alt="sd"/>为什么我总是收到404错误?我做错了什么??
我的通用init类:
@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);
}
}发布于 2016-11-16 21:28:50
在您的模板中,您需要使用胸腺叶格式自行自动添加上下文。使用以下命令:
<img th:src="@{/praca.jpg}" alt="sd"/>/praca.jpg
应为静态或公共文件夹中的映像的完整路径
发布于 2017-03-04 10:28:51
我也遇到过类似的问题,那就是spring安全性的问题,所以任何有这个问题的人都可以尝试将他们的静态文件所在的文件夹添加到不需要身份验证的URL列表中,例如antMatchers("/css/**", "/fonts/**").permitAll()
发布于 2018-08-05 03:17:06
这就是我在Spring Boot中处理静态资源的方式,在你的WebConfig类或扩展WebMvcConfigurerAdapter的类中,添加以下内容:
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}然后,在resources中创建一个static文件夹,您可以在其中放置所有静态文件或文件夹,如resources/css、resources/js等
从您的视图中,您可以像这样访问它,例如:
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/skin-black.css">如果您使用的是Spring Security,请确保添加antMatchers
.antMatchers("/resources/**").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/js/**").permitAll()https://stackoverflow.com/questions/40618838
复制相似问题