質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Spring Security

Spring Securityは、Springのサブプロジェクトの一つでWebアプリケーションに必要な機能を追加します。正規ユーザーであるかを確認するための「認証機能」と、ユーザーのアクセスを制御する「認可機能」を簡単に追加することが可能です。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

2回答

4675閲覧

[Spring Boot 2.0] Thymeleafがcss、jsを読み込まない

Yoshi-Koyama

総合スコア23

Spring Security

Spring Securityは、Springのサブプロジェクトの一つでWebアプリケーションに必要な機能を追加します。正規ユーザーであるかを確認するための「認証機能」と、ユーザーのアクセスを制御する「認可機能」を簡単に追加することが可能です。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2019/06/30 06:42

編集2019/06/30 08:48

###実現したいこと

Thymeleafを使用したcssファイル(static下のファイル)を読み込みたいです。
Spring Securityまわりを直してみましたが読み込んでいません。

sourceを確認するとcssがhtmlに書き換わっています。

  • Source

Source

もしほか原因がありそうであればご教示ください。

###発生している問題・エラーメッセージ

  • ブラウザ上に表示される画面

ブラウザ上に表示される画面

  • Console

Console

###該当のソースコード

Thymeleafのソース(index.htmlのheadタグ部分のみ抜粋)

index.html

1 <head> 2 <meta charset="utf-8"> 3 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 4 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 5 <meta name="description" content=""> 6 <meta name="author" content=""> 7 8 <title>S-Support 1 - Dashboard</title> 9 10 <!-- Custom fonts for this template--> 11 <link th:href="@{/vendor/fontawesome-free/css/all.min.css}" rel="stylesheet" type="text/css"> 12 <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet"> 13 14 <!-- Custom styles for this template--> 15 <link th:href="@{/css/sb-admin-2.min.css}" rel="stylesheet"> 16 <link th:href="@{/css/style.css}" rel="stylesheet"> 17 </head>

SecurityConfiguration.java

1package com.salessupportapp.salessupportapp.config; 2 3import org.springframework.context.annotation.Configuration; 4import org.springframework.security.config.annotation.web.builders.HttpSecurity; 5import org.springframework.security.config.annotation.web.builders.WebSecurity; 6import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 7 8@Configuration 9public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 10 @Override 11 public void configure(WebSecurity web) throws Exception { 12 // セキュリティ設定を無視するリクエスト設定 13 // 静的リソース(images、css、javascript)に対するアクセスはセキュリティ設定を無視する 14 web.ignoring().antMatchers( 15 "/images/**", 16 "/css/**", 17 "/js/**", 18 "/scss/**", 19 "/vendor/**"); 20 } 21 22 @Override 23 protected void configure(HttpSecurity http) throws Exception { 24 http.authorizeRequests() 25 .antMatchers("/").permitAll() 26 .antMatchers("/h2_console/**").permitAll() 27 // アクセス権限の設定 28 // staticディレクトリにある、'/css/','fonts','/js/'は制限なし 29 .antMatchers("/css/**", "/js/**", "/scss/**", "/vendor/**").permitAll(); 30 31 http.csrf().disable(); 32 http.headers().frameOptions().disable(); 33 } 34} 35
$ tree -L 3 -d src/main src/main ├── java │ └── com │ └── salessupportapp └── resources ├── startbootstrap-sb-admin-2-gh-pages │ ├── css │ ├── img │ ├── js │ ├── scss │ └── vendor ├── static │ ├── css │ ├── img │ ├── js │ ├── scss │ └── vendor └── templates └── fragment

build.gradle

1plugins { 2 id 'org.springframework.boot' version '2.1.0.RELEASE' 3 id 'java' 4 id 'eclipse' 5} 6 7apply plugin: 'io.spring.dependency-management' 8apply plugin: 'idea' 9 10group = 'com.sales-support-app' 11version = '0.0.1-SNAPSHOT' 12sourceCompatibility = '1.8' 13 14configurations { 15 developmentOnly 16 runtimeClasspath { 17 extendsFrom developmentOnly 18 } 19 compileOnly { 20 extendsFrom annotationProcessor 21 } 22} 23 24repositories { 25 mavenCentral() 26} 27 28dependencies { 29 implementation 'org.springframework.boot:spring-boot-starter-actuator' 30 implementation 'org.springframework.boot:spring-boot-starter-security' 31 implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' 32 implementation 'org.springframework.boot:spring-boot-starter-web' 33 implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1' 34 implementation 'org.springframework.session:spring-session-core' 35 compileOnly 'org.projectlombok:lombok' 36 developmentOnly 'org.springframework.boot:spring-boot-devtools' 37 runtimeOnly 'com.h2database:h2' 38 runtimeOnly 'mysql:mysql-connector-java' 39 annotationProcessor 'org.projectlombok:lombok' 40 testImplementation 'org.springframework.boot:spring-boot-starter-test' 41 testImplementation 'org.springframework.security:spring-security-test' 42} 43idea { 44 module { 45 inheritOutputDirs = false 46 outputDir = file("$buildDir/classes/java/main") 47 } 48} 49

###試したこと

  • SecurityConfigurationの見直し
  • pathやディレクトリ配置の確認
  • style.cssをためしに作成し読み込み

###補足情報(言語/FW/ツール等のバージョンなど)

IDE:IntelliJ
OS:macOS Sierra
Java Version: 9.0.1
Spring Boot: 2.1.0

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/06/30 08:15

おそらくセキュリティ問題、 401エラー
Yoshi-Koyama

2019/06/30 08:17

asahinaさん。 いつもasahinaさんの回答を陰ながら参考にさせてもらってます。 やはりセキュリティの設定がうまくいっていないんですね・・・。 Spring Securityを外して試してみます。
退会済みユーザー

退会済みユーザー

2019/06/30 08:27

帰ってきてるHTMLはおそらくログインページのはずよ
Yoshi-Koyama

2019/06/30 08:34

loginページを無効化したかったので ``SecurityConfiguration `` のconfigureメソッドで無効化したつもりだったのですが、それが間違っているんでしょうか? ``` @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/").permitAll() .antMatchers("/h2_console/**").permitAll() // アクセス権限の設定 // staticディレクトリにある、'/css/','fonts','/js/'は制限なし .antMatchers("/css/**", "/js/**", "/scss/**", "/vendor/**").permitAll(); http.csrf().disable(); http.headers().frameOptions().disable(); } ```
Yoshi-Koyama

2019/06/30 08:34

markdown効かないのか・・・見にくくてすみません。
guest

回答2

0

自己解決

解決しました。
stackoverflowにて同じ現象を発見しました(ググり力のなさ!)。

https://stackoverflow.com/questions/49420914/thymeleaf-intellij-and-spring-boot-not-loading-css-files-correctly

GetMappingに("/")がないことが問題でした。
localhost:8080/で自動でマッピングされるのですが、静的ファイルが読み込まれないんですね。

投稿2019/06/30 13:36

Yoshi-Koyama

総合スコア23

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

まさかIDEの問題かと考えサンプルプロジェクトを作ってみましたが、cssは問題なく読み込まれました。
またサンプルプロジェクトのソース検証ツールを確認したところ、cssファイルの中身がhtmlファイルに置き換わっているということは起きていませんでした。

やはり何かが原因でcssをはじめとして静的ファイルが書き換わっていることが問題のようです。

投稿2019/06/30 08:08

編集2019/06/30 08:16
Yoshi-Koyama

総合スコア23

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問