Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make thymeleaf spring security namespace available?

I've got a Spring Boot app. From what I understand a boot app will only need the dependency in the pom and all is great. Unfortunately, that's not the case and even when I overcomplicate my configuration it still doesn't work - I can't use the sec namespace in my pages.

In my page the first issue is the namespace URI:

enter image description here

I've tried every option available in the Intellij fix menu and can't get it.

I suppose the result of that issue is the fact that I can't use the sec namespace anywhere. The pictured example may indeed be an invalid use but I've used <div> as well which is straight from the Thymeleaf examples:

enter image description here

Many of the answers here and other sources are relying on xml configuration as well, which is of no use. Still, I've made Java-based beans based on those xml examples with no luck.

What steps are required to use spring security and thymeleaf integration in a spring boot app using only Java based configuration (if that)?

pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>
    ...
</dependencies>
like image 452
ChiefTwoPencils Avatar asked Jun 02 '26 17:06

ChiefTwoPencils


1 Answers

I have encountered the same issue and for me it helped to define schema locations like this:

<html lang="en"
  xmlns:th="http://www.thymeleaf.org"
  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
  xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.thymeleaf.org http://www.thymeleaf.org
  http://www.ultraq.net.nz/thymeleaf/layout http://www.ultraq.net.nz/thymeleaf/layout
  http://www.thymeleaf.org/thymeleaf-extras-springsecurity4 http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

...

This includes also additional dialects for layout and spring security 4 which you can remove if you are not using them.

like image 154
Darko Minarik Avatar answered Jun 05 '26 08:06

Darko Minarik