首页 > 解决方案 > GET http://localhost:8080/resources/css/index.css net::ERR_ABORTED 404

问题描述

我知道关于这个主题有类似的问题,但我似乎无法找到解决方案。我有一个 Spring MVC 项目,问题是我在运行应用程序时收到浏览器日志中标题中指定的错误。如果我在浏览器(不是来自 Spring)中打开 index.html,则会加载 CSS。

这是我的结构。

index.html CSS 声明

    <link rel="stylesheet" type="text/css" th:href="@{ /css/index.css }"
          href="../../resources/static/css/index.css">

控制器

@RequestMapping(value = { "/", "/index"}, method = RequestMethod.GET)
public ModelAndView index() {
    ModelAndView model = new ModelAndView();
    model.setViewName("index");

    return model;
}

我什至尝试在SecurityConfiguration中添加它

@Override
public void configure(WebSecurity web) throws Exception {
    super.configure(web);

    web.ignoring().antMatchers("/resources/**", "/static/**", "/css/**", "/icons/**", "/javascript/**");

}

但它不起作用。

路径很好,因为正如我所说,当我在浏览器中打开index.html时加载了 CSS,我认为问题出在SecurityConfiguration类上,但我不知道它是什么。

有人有什么主意吗?

标签: javahtmlcssspring-mvchttp-status-code-404

解决方案


所以错误确实来自SecurityConfiguration因为我没有使用正确的注释。正确的是

@EnableWebSecurity

我正在使用

@EnableWebMvc

所以现在我的课看起来像这样:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
private DataSource dataSource;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

}

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.csrf().disable();

    http.authorizeRequests().antMatchers("/", "/index").permitAll().and()
            .exceptionHandling().accessDeniedPage("/libraryAppError");
    http.authorizeRequests().antMatchers("/resources/**").permitAll().anyRequest().permitAll();
}
}

一切正常。


推荐阅读