i'm working on simple project that implement
spring security.
the issue come when i tried to work with Logout link with Spring and Thymeleaf.
1.pom.xml
<!--Spring Boot Dependencies - Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Dependencies Spring Security-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>2.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
navbar.html
<ul class="nav navbar-nav navbar-right">
<!--<li><a th:href="#{/login}" th:text="#{navbar.login.text}"></a></li>-->
<li th:if="${#authorization.expression('isAuthenticated()')}">
<a th:href="#{/login}" th:text="#{navbar.login.text}"/>
</li>
<li th:if="${#authorization.expression('isAuthenticated()')}">
<form id="f" th:action="#{/logout}" method="post" role="form" class="navbar-form">
<button type="submit" th:text="#{navbar.logout.text}" class="btn btn-primary"/>
</form>
</li>
</ul>
error scrrenshot:
This error happens when the Thymeleaf Extras Spring Security version is not compatible with the Spring Framework version, in its case, the Spring Boot version works with the Spring Framework version 5.x and the Thymeleaf Extras Spring Security version you have is 4.x. You need update the artifactId in your pom.xml and either choose a compatible version or let Spring Boot choose the version for you
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<scope>compile</scope>
</dependency>
Related
I'm trying to run my first Spring MVC Program on eclipse using XML configuration, I have no error markings on my directory but I get this response when I run the program. I also tried with Spring java configuration and got similar response.
bug
I use eclipse Version: 2021-06 (4.20.0) and apache-tomcat-10.0.13 installed on my device, sending a request at http://localhost:8080/ returns apache-tomcat successfully installed. So far I have tried all suggestions I found online including updating maven project, maven build-clean install, cleared file cache, deleted temp folder in my root director changed workspace directory, uninstalled and reinstall apache-tomcat multiple times, cleared missing files on source directory in java build path but none of this approaches have worked. Below is a screenshot of my program (XML configuration)
Directory
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.idevelope</groupId>
<artifactId>spring-mvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Spring MVC</name>
<description>Spring MVC example</description>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>16</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
</plugin>
</plugins>
</build>
</project>
web.xml
dispatcher-servlet.xml
controller
user.java
(View Pages)
home.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Spring MVC tutorial - Home JSP</title>
</head>
<body>
<h1>Hello World!</h1>
<h4>Message- </h4><span>${message}</span>
<form:form action="showUser" modelAttribute="user" method="post">
<tr>
<td>
<form:label path="firstName">First Name</form:label>
</td>
<td>
<form:input path="firstName" id="firstname" />
</td>
</tr>
<tr>
<td>
<form:label path="lastName">Last Name</form:label>
</td>
<td>
<form:input path="lastName" id="lastname" />
</td>
</tr>
<input type="submit" value="Submit">
</form:form>
</body>
</html>
user.jsp
Please I need help to fix this bug.
In my case and as i searched internet - tomcat 10 does not work. I downloaded tomcat 9 and 404 disappeared. Spring MVC 5 does not work on Tomcat 10. This is because Tomcat 10 is based on Jakarta EE 9 where package names for APIs have changed from javax.* to jakarta.*.
maybe lack
*<mvc:annotation-driven/>*Annotation-driven injection (e.g. Controller, #requestMapping, etc.) results in a page always indicating that the relevant resource information cannot be accessed
You write, you try to run your first spring program with Spring MVC. I did not dive into your details, but I would suggest two different approaches to learn and play around with Spring MVC:
Spring initializer web site
When I try out new spring parts, I always start at
https://start.spring.io/
and just add my wanted dependencies (search for mvc when pressing add button).
After defined your setting there, you can just download a working maven or a gradle example directly from the web page as a zip file. After extraction the files can be directly imported by IDE (e.g. eclipse, intelliJ, etc.)
Official Spring MVC tutorial
When you are searching a good and out-of-the-box working MVC example you should look at https://spring.io/guides/gs/serving-web-content/
This works well and you can start directly without dependency problems etc.
Try adding tomcat jasper dependency to pom.xml
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>10.1.0-M5</version>
</dependency>
check tomcat version before adding the dependency
Good morning people!
I’m trying to make the Thymeleaf part of template and fragments work, but nothing happens on the page...
I already tested several configurations, but I could not do anything that would make this part of th:block work or get the layout.html correctly.
Can someone help me please? See code below:
new.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="layout">
<th:block layout:fragment="content">
<ul>
<li th:each="name : ${list}">
<p th:text="${name}"></p>
</li>
</ul>
</th:block>
</html>
layout.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<meta charset="ISO-8859-1" />
<title>Insert title here</title>
<link rel="stylesheet" th:href="#{/css/bootstrap.min.css}" />
</head>
<body>
<th:block layout:replace="fragments/header"></th:block>
<div class="container-fluid">
<div class="row">
<th:block layout:fragment="content"></th:block>
</div>
</div>
<script th:src="#{/js/jquery-3.3.1.min.js}"></script>
<script th:src="#{/js/bootstrap.min.js}"></script>
</body>
</html>
header.html:
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#nav">
<span class="navbar-toggler-icon"></span>
</button>
Spring App
<div class="collapse navbar-collapse" id="nav">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
Home
</li>
<li class="nav-item active">
About
</li>
</ul>
</div>
</nav>
InicioController:
package com.example.teste.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
#RequestMapping("/")
public class InicioController {
#GetMapping("/new")
public String newPath(Model model) {
List<String> list = new ArrayList<String>();
list.add("Teste 1");
list.add("Teste 2");
list.add("Teste 3");
list.add("Teste 4");
list.add("Teste 5");
model.addAttribute("list", list);
return "new";
}
}
I find it strange that Thymeleaf xmlns does not appear... Is this normal?
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>teste</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>teste</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I inserted another image that shows the empty part and also does not appear the xmlns: th = "http://www.thymeleaf.org" I do not know how far this is normal.
new.html:
UPDATE
The issue is caused by a dependency issue, when you add the following to your pom.xml it works:
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>2.0.5</version>
</dependency>
OLD ANSWER
You are probably missing the fragment declaration in your header.html, it should be like this:
<div th:fragment="header">
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#nav">
<span class="navbar-toggler-icon"></span>
</button>
Spring App
<div class="collapse navbar-collapse" id="nav">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
Home
</li>
<li class="nav-item active">
About
</li>
</ul>
</div>
</nav>
</div>
I also noticed you have in your layout.html the same fragment declaration as in new.html. I suppose one of these needs to be a replace tag?
I struggle to get Thymeleaf to work with Spring Security in my Spring Boot 1.4.3 based project.
Tags like e.g.
<div sec:authorize="hasAuthority('ADMIN')">
are simply not parsed.
If I try to add the SpringSecurityDialect manually like this:
#Bean
public SpringSecurityDialect securityDialect() {
return new SpringSecurityDialect();
}
I am getting:
Exception in thread "main" java.lang.NoClassDefFoundError: org/thymeleaf/dialect/IExpressionEnhancingDialect
I have included the following in my dependencies:
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
The SpringSecurityDialect does not seem to be added by the autoconfiguration.
After I add the Bean manually, I get the mentioned exception.
Is this a bug or am I missing something?
My Thymeleaf versions are:
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-extras-java8time.version>3.0.0.RELEASE</thymeleaf-extras-java8time.version>
<thymeleaf-layout-dialect.version>2.1.2</thymeleaf-layout-dialect.version>
To get it working, if you are using Thymeleaf 3.0.2 with Spring Boot 1.4, you need to force version 3.0.1.RELEASE of thymeleaf-extras-springsecurity4 (because it inherits version 2.1.2 which does not work in combination with Thymeleaf 3):
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>
The tags should be using the hasRole function.
<div sec:authorize="hasRole('ROLE_ADMIN')">
If you 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>
</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>
I used to have the same problem.
Thymeleaf SpringSecurity only works with versions 3.x.x of thymeleaf, and the version that's shipped with Spring-boot is something like 2.x.x atm.
Looking up how to add v3.x.x to my project brought me to the following documentation page:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-use-thymeleaf-3
So you just need to add your dependencies, and then add the following in your properties to override the default version of thymeleaf to your dependencies:
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
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:
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:
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>
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.
Hi new to java and I'm getting the following errors per the following lines from a .jsp that I have inherited. Using spring source as my editor.
NOTE: This loads fine per my latest eclipse (kepler) IDE.
<%# taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
Errors:
Can not find the tag library descriptor for "http://www.springframework.org/tags/
form"
Can not find the tag library descriptor for "http://www.springframework.org/
tags"
I have the following defined in my pom.xml:
<spring.version>3.2.1.RELEASE</spring.version>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
I added:
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
To mypom.xml file this will resolved the issue.
Try deleting the spring-mvc dependency, updating the maven project without that dependency and then re-add the spring-mvc dependency. Probably the .jar file is corrupted (as was the case in my project).
Add the following dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
For me, it worked seamlessly the answer provided by user3078016..
I simply added:
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
To my pom.xml and voilá, everything is clean now..
BTW, version 1.1.2 is actually the last (and current) version of this lib since 2005..
Thank you..