From 08c9791cb4559e27857bfdc5c911072c4ffd3ba8 Mon Sep 17 00:00:00 2001 From: maibin Date: Thu, 13 Oct 2016 07:08:59 +0200 Subject: [PATCH] Thymeleaf decorator (#747) * Expression-Based Access Control PermitAll, hasRole, hasAnyRole etc. I modified classes regards to Security * Added test cases for Spring Security Expressions * Handler Interceptor - logging example * Test for logger interceptor * Removed conflicted part * UserInterceptor (adding user information to model) * Spring Handler Interceptor - session timers * Spring Security CSRF attack protection with Thymeleaf * Fix and(); * Logger update * Changed config for Thymeleaf * Thymeleaf Natural Processing and Inlining * Expression Utility Objects, Thymeleaf * listOfStudents edited * Thymeleaf layout decorators --- spring-thymeleaf/pom.xml | 356 +++++++++--------- .../thymeleaf/config/WebMVCConfig.java | 4 + .../controller/LayoutDialectController.java | 16 + .../main/webapp/WEB-INF/views/content.html | 16 + .../webapp/WEB-INF/views/listStudents.html | 2 +- .../main/webapp/WEB-INF/views/template.html | 20 + .../LayoutDialectControllerTest.java | 58 +++ 7 files changed, 296 insertions(+), 176 deletions(-) create mode 100644 spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/LayoutDialectController.java create mode 100644 spring-thymeleaf/src/main/webapp/WEB-INF/views/content.html create mode 100644 spring-thymeleaf/src/main/webapp/WEB-INF/views/template.html create mode 100644 spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/LayoutDialectControllerTest.java diff --git a/spring-thymeleaf/pom.xml b/spring-thymeleaf/pom.xml index a13f1de4c7..960b358fe2 100644 --- a/spring-thymeleaf/pom.xml +++ b/spring-thymeleaf/pom.xml @@ -5,186 +5,192 @@ spring-thymeleaf 0.1-SNAPSHOT war - - 1.8 - - 4.3.3.RELEASE - 3.0.1 - - 1.7.12 - 1.1.3 - - 3.0.1.RELEASE - - 1.1.0.Final - 5.1.2.Final + + 1.8 + + 4.3.3.RELEASE + 3.0.1 + + 1.7.12 + 1.1.3 + + 3.0.1.RELEASE + + 1.1.0.Final + 5.1.2.Final - - 3.5.1 - 2.6 - 2.19.1 - 1.4.18 - + + 3.5.1 + 2.6 + 2.19.1 + 1.4.18 + - - - - org.springframework - spring-context - ${org.springframework-version} - - - - commons-logging - commons-logging - - - - - org.springframework - spring-webmvc - ${org.springframework-version} - - - - org.springframework.security - spring-security-web - 4.1.3.RELEASE - - - org.springframework.security - spring-security-config - 4.1.3.RELEASE - - - - org.thymeleaf - thymeleaf - ${org.thymeleaf-version} - - - org.thymeleaf - thymeleaf-spring4 - ${org.thymeleaf-version} - - - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - - org.slf4j - jcl-over-slf4j - ${org.slf4j.version} - - - - org.slf4j - log4j-over-slf4j - ${org.slf4j.version} - - - - javax.servlet - javax.servlet-api - ${javax.servlet-version} - provided - - - - javax.validation - validation-api - ${javax.validation-version} - - - org.hibernate - hibernate-validator - ${org.hibernate-version} - - + + + + org.springframework + spring-context + ${org.springframework-version} + + + + commons-logging + commons-logging + + + + + org.springframework + spring-webmvc + ${org.springframework-version} + + + + org.springframework.security + spring-security-web + 4.1.3.RELEASE + + + org.springframework.security + spring-security-config + 4.1.3.RELEASE + + + + org.thymeleaf + thymeleaf + ${org.thymeleaf-version} + + + org.thymeleaf + thymeleaf-spring4 + ${org.thymeleaf-version} + + + + nz.net.ultraq.thymeleaf + thymeleaf-layout-dialect + 2.0.4 + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + + javax.servlet + javax.servlet-api + ${javax.servlet-version} + provided + + + + javax.validation + validation-api + ${javax.validation-version} + + + org.hibernate + hibernate-validator + ${org.hibernate-version} + + - - org.springframework - spring-test - 4.1.3.RELEASE - test - + + org.springframework + spring-test + 4.1.3.RELEASE + test + - - - org.springframework.security - spring-security-test - 4.1.3.RELEASE - test - + + + org.springframework.security + spring-security-test + 4.1.3.RELEASE + test + - - - junit - junit - 4.12 - test - + + + junit + junit + 4.12 + test + - + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${java-version} + ${java-version} + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + false + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + + + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + true + + jetty8x + embedded + + + + + + 8082 + + + + + + - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - ${java-version} - ${java-version} - - - - org.apache.maven.plugins - maven-war-plugin - ${maven-war-plugin.version} - - false - - - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - - - - - - - org.codehaus.cargo - cargo-maven2-plugin - ${cargo-maven2-plugin.version} - - true - - jetty8x - embedded - - - - - - 8082 - - - - - - - diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java index 444b780673..ab048bdd87 100644 --- a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java @@ -22,6 +22,9 @@ import org.thymeleaf.templateresolver.ITemplateResolver; import com.baeldung.thymeleaf.formatter.NameFormatter; import com.baeldung.thymeleaf.utils.ArrayUtil; +import nz.net.ultraq.thymeleaf.LayoutDialect; +import nz.net.ultraq.thymeleaf.decorators.strategies.GroupingStrategy; + @Configuration @EnableWebMvc @@ -70,6 +73,7 @@ public class WebMVCConfig extends WebMvcConfigurerAdapter implements Application private TemplateEngine templateEngine(ITemplateResolver templateResolver) { SpringTemplateEngine engine = new SpringTemplateEngine(); + engine.addDialect(new LayoutDialect(new GroupingStrategy())); engine.setTemplateResolver(templateResolver); return engine; } diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/LayoutDialectController.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/LayoutDialectController.java new file mode 100644 index 0000000000..28a38ce30b --- /dev/null +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/LayoutDialectController.java @@ -0,0 +1,16 @@ +package com.baeldung.thymeleaf.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Controller +public class LayoutDialectController { + + @RequestMapping(value = "/layout", method = RequestMethod.GET) + public String getNewPage(Model model) { + return "content.html"; + } + +} diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/content.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/content.html new file mode 100644 index 0000000000..1184e28e13 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/content.html @@ -0,0 +1,16 @@ + + + +Layout Dialect Example + + +
+

This is a custom content that you can provide

+
+ + + \ No newline at end of file diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html index a894e41e88..c7d17a4f66 100644 --- a/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html @@ -6,7 +6,7 @@ - + + +
+

New dialect example

+
+
+

Your page content goes here

+
+ + + \ No newline at end of file diff --git a/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/LayoutDialectControllerTest.java b/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/LayoutDialectControllerTest.java new file mode 100644 index 0000000000..33731f130d --- /dev/null +++ b/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/LayoutDialectControllerTest.java @@ -0,0 +1,58 @@ +package com.baeldung.thymeleaf.controller; + +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import javax.servlet.Filter; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockHttpSession; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.RequestPostProcessor; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.thymeleaf.config.InitSecurity; +import com.baeldung.thymeleaf.config.WebApp; +import com.baeldung.thymeleaf.config.WebMVCConfig; +import com.baeldung.thymeleaf.config.WebMVCSecurity; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class }) +public class LayoutDialectControllerTest { + + @Autowired + WebApplicationContext wac; + @Autowired + MockHttpSession session; + + private MockMvc mockMvc; + + @Autowired + private Filter springSecurityFilterChain; + + protected RequestPostProcessor testUser() { + return user("user1").password("user1Pass").roles("USER"); + } + + @Before + public void setup() { + mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build(); + } + + @Test + public void testGetDates() throws Exception{ + mockMvc.perform(get("/layout").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("content.html")); + } + +}