6
10

More than 5 years have passed since last update.

【問題解決】Thymeleafで、Spring Securityのダイアレクト(`sec:authentication`など)が使えない

Last updated at Posted at 2018-01-13

環境

  • Spring Boot 1.5.9
  • Thymeleaf 3.0.9
  • Java8

やりたいこと

「thymeleaf-extras-springsecurity」というライブラリを使って、ThymleafでSpring Securityのsec:authorizesec:authenticationを使いたいです。
下記サイトを参考にしました。
http://www.thymeleaf.org/doc/articles/springsecurity.html

問題

ログインユーザの名前とロールを表示するために、以下のようなhtmlファイルとそれを表示するコントローラを作成しました。

security.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
    xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset="UTF-8">
<title>Spring Security thymeleaf</title>
</head>
<body>
Logged user: <span sec:authentication="name"></span>
Roles: <span sec:authentication="principal.authorities"></span>
</body>
</html>

ブラウザでアクセスしたところ、ログイン名やロールは表示されませんでした。
また、ブラウザでHTMLソースを確認したところ、sec:authenticationが残っていました。

pom.xmlは以下の通りです。

pom.xml
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
    </properties>

    <dependencies>
      <!-- spring boot 関係のライブラリは省略 -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

       <!-- ThymleafでSpring Securityのダイアレクトを使うためのライブラリ -->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        </dependency>

    </dependencies>

原因・解決策

「thymeleaf-extras-springsecurity」のバージョンが、Thymeleafのバージョンに対応していないことが、原因でした。

  • Thymeleaf: 3.0.9.RELEASE
  • thymeleaf-extras-springsecurity4:2.1.3.RELEASE(spring-boot-dependencies 1.5.9.RELEASE で定義されている)

「thymeleaf-extras-springsecurity」のREADMEには、

Current versions:
Version 3.0.2.RELEASE - for Thymeleaf 3.0 (requires Thymeleaf 3.0.3+)
Version 2.1.3.RELEASE - for Thymeleaf 2.1 (requires Thymeleaf 2.1.2+)

と書いてありました。
https://github.com/thymeleaf/thymeleaf-extras-springsecurity 引用

Thymeleafのバージョンが3.0.9である理由

親プロジェクトである「spring-boot-dependencies」のpom.xmlには、以下の通りバージョンが定義されています。

spring-boot-dependencies
<properties>
    <!-- Dependency versions -->
    <!-- ~ -->
    <thymeleaf.version>2.1.6.RELEASE</thymeleaf.version>
    <thymeleaf-extras-springsecurity4.version>2.1.3.RELEASE</thymeleaf-extras-springsecurity4.version>
    <thymeleaf-extras-conditionalcomments.version>2.1.2.RELEASE</thymeleaf-extras-conditionalcomments.version>
    <thymeleaf-layout-dialect.version>1.4.0</thymeleaf-layout-dialect.version>
    <thymeleaf-extras-data-attribute.version>1.3</thymeleaf-extras-data-attribute.version>
    <thymeleaf-extras-java8time.version>2.1.0.RELEASE</thymeleaf-extras-java8time.version>
    <!-- ~ -->
</properties>

Thyemeleafのバージョンは「2.1.6」です。

Thyemeleaf v2のデフォルト設定では、<br>など閉じていないタグは使えません。<br/>と書く必要があります。
HTML5では許可されている<br>などを使いたかったので、下記サイトを参考にして、Thymleafのバージョンを3にしました。
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-use-thymeleaf-3

pom.xml
    <properties>
        <!-- ~ -->
        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
    </properties>

で、バージョンを変更していたことを忘れていたのが、今回の問題の原因です。

解決策

「thymeleaf-extras-springsecurity」のバージョンを3.0.2に変更しました。

pom.xml
    <properties>
        <!-- ~ -->
        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
    </properties>

補足

ThymeleafでJava8の「Date and Time API」などを使うときも、Thymeleaf3に対応したバージョンに変更する必要があります。

If you are using any of the other auto-configured Thymeleaf Extras (Spring Security, Data Attribute, or Java 8 Time) you should also override each of their versions to one that is compatible with Thymeleaf 3.0.

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-use-thymeleaf-3 引用

同様な問題

Spring Securityのauthenticationオブジェクトを作成するために、security.htmlに以下を追記しました。

security.html
    <div th:text="${#authentication.name}">
        The value of the "name" property of the authentication object should appear here.
    </div>

対応前の状態(「thymeleaf-extras-springsecurity」」のバージョンが2)で、ブラウザからアクセスすると、コンソールに以下のエラーが出力されました。

console
2018-01-14 00:40:16.587 ERROR 17776 --- [nio-8081-exec-7] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8081-exec-7] Exception processing template "security": An error happened during template parsing (template: "class path resource [templates/security.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/security.html]")
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    ...
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "#authentication.name" (template: "security" - line 13, col 10)
    at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
...

参考にしたサイト

6
10
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
10