2017-09-01 12 views
0

スプリングブートで静的コンテンツを提供するのに問題があります。スプリングブート静的コンテンツ

私はデフォルトの設定場所:src/main/resources/static/csssrc/main/resources/static/jsを使用しています。

ページが読み込まれると、すべての静的コンテンツに対して404が得られます。私はセキュリティ設定に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("/"); 
} 

...私は私のCSSは404をreturingさloginを打つ:ここhttp://localhost:8080/login

はレスポンスヘッダです:

Request 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 
+0

あなたはJarファイルとして実行していますか? – harshavmb

+0

デフォルトのresourcesフォルダを使用している場合、addresourceハンドラを設定する必要はありません。それを取り除いてからやり直すことができます。 SpringBootは静的フォルダを設定し、リソースを使用できる状態に設定します –

+0

https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-bootリソースハンドラの設定方法を参照してください。 –

答えて

0

この問題は、WebMvcConfigurationSupportを私のappConfigクラス(私は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); 
} 

EDITを追加する必要がありました。 WebMvcConfigurerAdapterを拡張するように設定クラスを変更しました。上記の変更は必要ありません。@AndyWilkinsonから下記のコメントをご覧ください。

+0

なぜWebMvcConfigurationSupportを拡張していますか?これは、Spring Bootアプリケーションでやることはやや珍しいことです。推奨されるアプローチは 'WebMvcConfigurerAdapter'を代わりに拡張することです。これにより、Spring MVCの自動設定をカスタマイズすることができます。また、Spring Bootのデフォルトを維持します。 –

+0

@AndyWilkinson、アンディあなたは正しいです..私は自分のコードを変更し、 –

関連する問題