If you are using Spring Boot,use Spring Boot 2.0.0.RELEASE:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
you need just the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
Version of thymeleaf-extras-springsecurity4
will be inherited from the spring-boot-starter-parent
and would be 3.0.2.RELEASE
.
Thanks to @yglodt for pointing this out.
Also in your templates add spring-security namespace xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
and use hasRole
instead of hasAuthority
value in <sec:authorize>
tag:
<div sec:authorize="hasRole('ROLE_ADMIN')">
...
</div>