spring boot静态内容

时间:2017-09-01 17:17:09

标签: css spring spring-boot

我在使用Spring Boot提供静态内容时遇到了麻烦。

我正在使用默认配置位置:src/main/resources/static/js404

当页面加载时,我获得了所有静态内容的permitAll。我甚至将@Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/css/**","/js/**").permitAll() .antMatchers("/services/**").hasRole("PREAUTH_USER") .antMatchers("/", "/dashboard").permitAll() .anyRequest().authenticated() .and() .formLogin().loginPage("/login") .permitAll() .and() .logout() .permitAll(); } 添加到我的安全设置中。

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
  registry.addResourceHandler("/**").addResourceLocations("/");
}

我的资源处理程序:

login

...当我点击404时,我的CSS正在推回http://localhost:8080/loginRequest URL:http://localhost:8080/css/preauth.css Request Method:GET Status Code:404 Remote Address:[::1]:8080 Referrer Policy:no-referrer-when-downgrade Response Headers view source Cache-Control:no-cache, no-store, max-age=0, must-revalidate Content-Type:application/json;charset=UTF-8 Date:Fri, 01 Sep 2017 17:04:59 GMT Expires:0 Pragma:no-cache Transfer-Encoding:chunked X-Content-Type-Options:nosniff X-Frame-Options:DENY X-XSS-Protection:1; mode=block Request Headers view source Accept:text/css,*/*;q=0.1 Accept-Encoding:gzip, deflate, br Accept-Language:en-US,en;q=0.8 Connection:keep-alive Cookie:JSESSIONID=D1399529A7AD61D0FA67ECF0343D3A03 Host:localhost:8080 Referer:http://localhost:8080/login User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36

以下是回复标题:

.mdl-layout__back-button

1 个答案:

答案 0 :(得分:0)

问题似乎是当我在appConfig类中扩展WebMvcConfigurationSupport(我需要配置messageconverters)时,WebMvcAutoConfiguration被禁用。所以我不得不添加

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
        "classpath:/META-INF/resources/", "classpath:/resources/",
        "classpath:/static/", "classpath:/public/" };

   @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!registry.hasMappingForPattern("/webjars/**")) {
    registry.addResourceHandler("/webjars/**").addResourceLocations(
            "classpath:/META-INF/resources/webjars/");
}
if (!registry.hasMappingForPattern("/**")) {
    registry.addResourceHandler("/**").addResourceLocations(
            CLASSPATH_RESOURCE_LOCATIONS);
}

编辑: 以上代码不是必需的。我更改了我的配置类以扩展WebMvcConfigurerAdapter,并且不需要进行上述更改。请参阅以下@AndyWilkinson的评论