GET http:// localhost:8080 / resources / css / index.css net :: ERR_ABORTED 404

时间:2019-10-24 13:38:38

标签: java html css spring-mvc http-status-code-404

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

This is my structure.

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 类,但我不知道这是什么。

有人有什么主意吗?

1 个答案:

答案 0 :(得分:1)

所以错误确实来自 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();
}
}

一切正常。