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

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

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

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

Java

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

Gradle

Gradleは、ビルド自動化ツールです。 ソフトウェアパッケージやドキュメント、 または実際に何か他の種類のプロジェクトの構築、テスト、公開、展開などを自動化が出来ます

Thymeleaf

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

Spring Boot

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

Q&A

解決済

1回答

7134閲覧

Thymeleafで、Spring Securityのダイアレクトが使えない

takahiro00

総合スコア84

Spring Security

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

Java

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

Gradle

Gradleは、ビルド自動化ツールです。 ソフトウェアパッケージやドキュメント、 または実際に何か他の種類のプロジェクトの構築、テスト、公開、展開などを自動化が出来ます

Thymeleaf

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

Spring Boot

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

0グッド

1クリップ

投稿2018/08/04 04:46

Thymeleafで、Spring Securityのダイアレクトでユーザ名を表示したい。

Thymeleafで[sec:authentication]を使ってweb上にユーザ名を表示したいのですが、
「Hello, displayName!」と表示されてしまいます。(本来ならdisplayNameにユーザ名が入る想定)
HTMLソースを確認すると、sec:authenticationがそのまま残ってしまいます。
おそらくHTMLか、build.gradleの設定で問題があると思うのですが、、、

navi.html

<html lang="ja" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security4"> <header th:fragment="globalnavi"> <nav class="navbar navbar-expand-lg navbar-default"> <a class="navbar-brand" href="/">Spring Social Demo</a> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav mr-auto"> <li sec:authorize="isAuthenticated()">Hello, <span sec:authentication="principal.user.displayName">displayName</span>! </li> </ul> <form th:action="@{/logout}" method="post" class="form-inline my-2 my-lg-0"> <div sec:authorize="isAnonymous()"> <a class="btn btn-outline-success my-2 my-sm-0" href="/signin">Sign in</a> </div> <div sec:authorize="isAuthenticated()"> <input type="submit" class="btn btn-outline-success my-2 my-sm-0" value="Log out"> </div> </form> </div> </nav> </header>

build.gradle

buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.10.RELEASE") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' apply plugin: 'idea' group = 'com.example.demo' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 targetCompatibility = 1.8 jar { baseName = 'gs-accessing-twitter' version = '0.1.0' } task wrapper(type: Wrapper) { gradleVersion = '4.6' } repositories { mavenCentral() } ext['thymeleaf.version'] = '3.0.2.RELEASE' ext['thymeleaf-layout-dialect.version'] = '2.1.1' jar.baseName = 'thymeleaf' dependencies { compile("org.springframework.boot:spring-boot-starter-web") compile('org.springframework.boot:spring-boot-starter-social-twitter') compile('org.springframework.social:spring-social-core') compile('org.springframework.social:spring-social-config') compile("org.springframework.boot:spring-boot-starter-thymeleaf") compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4') compile("org.springframework.security:spring-security-core") compile("org.springframework.security:spring-security-config") compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-starter-jdbc') compile("org.springframework.boot:spring-boot-starter-security") compile("org.springframework.security:spring-security-web") compile('org.springframework.social:spring-social-security') compile('org.springframework.boot:spring-boot-starter-data-redis') compile('org.springframework.session:spring-session') compile('org.projectlombok:lombok:1.16.10') compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.apache.commons:commons-text:1.1') compile('org.webjars:bootstrap:3.3.6') compile('org.webjars:startbootstrap-sb-admin-2:3.3.7+1') compile('org.springframework.boot:spring-boot-devtools') compile('org.springframework.boot:spring-boot-starter-test') runtime('org.postgresql:postgresql') testCompile('org.springframework.boot:spring-boot-starter-test') }

試したこと

色々調べた結果、Thymeleafのバージョン等が影響しているようなので、Thymeleafのバージョンを3にしたり、2にしたり試したのですが、うまくいかず

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

言語:java1.8
FW:SpringBoot
Thymeleaf
ツール:eclipse

ご教授お願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

dependenciesで指定する依存関係に問題があるように思います。

直接の原因ではないと思いますが、

  • spring-boot-starter-webとspring-boot-starter-testが2つある
  • spring-boot-devtoolsはruntime
  • lombokはcompileOnly

また、気になる点では、spring-boot-starter-securityがあればspring-security-core、spring-security-config、spring-security-webは明示しなくてもいいかと思うのですが、この点は断言できません。

最後に、下記の定義は

compile('org.projectlombok:lombok:1.16.10') compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4')

次の2行を削除し

ext['thymeleaf.version'] = '3.0.2.RELEASE' ext['thymeleaf-layout-dialect.version'] = '2.1.1'

lombokはバージョンを省略
thymeleaf-extras-springsecurity4はバージョンを明記

compileOnly('org.projectlombok:lombok') compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.2.RELEASE')

のように変えてみてはどうでしょうか?

投稿2018/08/04 05:31

rubytomato

総合スコア1752

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

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

takahiro00

2018/08/06 12:51

rubytomatoさん ご回答ありがとうございます。 上記試してみたのですが、 ``` ext['thymeleaf.version'] = '3.0.2.RELEASE' ext['thymeleaf-layout-dialect.version'] = '2.1.1' ``` 上記を削除して ``` compileOnly('org.projectlombok:lombok') compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.2.RELEASE') ``` compile部分を上記に編集したところで、 ``` Template mode "HTML" has not been configured ``` 上記のエラーが出てしまいます。。。。 application.propertiesには spring.thymeleaf.mode=HTML を記載してるのですが、、、、 ちょっともう少し調べてみます。
takahiro00

2018/08/06 13:26

すみません色々いじってたら解決しました。 以下に書き換えることで解決できました。 詳しく分からないのですが、thymeleafのバージョン、thymeleaf-layout-dialectのバージョン thymeleaf-extras-springsecurity4のバージョンで互換性が無く??エラーになっていたようでした。 ext['thymeleaf.version'] = '3.0.9.RELEASE' ext['thymeleaf-layout-dialect.version'] = '2.1.1' dependencies { compile("org.springframework.boot:spring-boot-starter-web") compile('org.springframework.boot:spring-boot-starter-social-twitter') compile('org.springframework.social:spring-social-core') compile('org.springframework.social:spring-social-config') compile('org.springframework.boot:spring-boot-starter-thymeleaf') compile('nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect') compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.2.RELEASE') compile("org.springframework.security:spring-security-core") compile("org.springframework.security:spring-security-config") compile('org.springframework.boot:spring-boot-starter-jdbc') compile("org.springframework.boot:spring-boot-starter-security") compile("org.springframework.security:spring-security-web") compile('org.springframework.social:spring-social-security') compile('org.springframework.boot:spring-boot-starter-data-redis') compile('org.springframework.session:spring-session') compileOnly('org.projectlombok:lombok') compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.apache.commons:commons-text:1.1') compile('org.webjars:bootstrap:3.3.6') compile('org.webjars:startbootstrap-sb-admin-2:3.3.7+1') runtime('org.springframework.boot:spring-boot-devtools') compile('org.springframework.boot:spring-boot-starter-test') runtime('org.postgresql:postgresql') }
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問