Thymeleafで、Spring Securityのダイアレクトが使えない
解決済
回答 1
投稿
- 評価
- クリップ 1
- VIEW 4,240
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
ご教授お願いいたします。
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
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')
のように変えてみてはどうでしょうか?
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.21%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2018/08/06 21:51
ご回答ありがとうございます。
上記試してみたのですが、
```
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
を記載してるのですが、、、、
ちょっともう少し調べてみます。
2018/08/06 22: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')
}