From 6e5310900ba457fd6d8ff96024af00cb88429ae4 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Sat, 13 Oct 2018 19:57:04 +0100 Subject: [PATCH] Switch from thymeleaf-extras-springsecurity4 to extras-springsecurity5 Closes gh-14806 --- .../spring-boot-autoconfigure/pom.xml | 2 +- .../thymeleaf/ThymeleafAutoConfiguration.java | 2 +- ...ymeleafReactiveAutoConfigurationTests.java | 23 +++++++++++++++++++ ...hymeleafServletAutoConfigurationTests.java | 21 +++++++++++++++++ .../resources/templates/security-dialect.html | 1 + .../spring-boot-dependencies/pom.xml | 6 ++--- spring-boot-project/spring-boot-docs/pom.xml | 2 +- 7 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/resources/templates/security-dialect.html diff --git a/spring-boot-project/spring-boot-autoconfigure/pom.xml b/spring-boot-project/spring-boot-autoconfigure/pom.xml index 6fe3ae319e..02a879739b 100755 --- a/spring-boot-project/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-project/spring-boot-autoconfigure/pom.xml @@ -655,7 +655,7 @@ org.thymeleaf.extras - thymeleaf-extras-springsecurity4 + thymeleaf-extras-springsecurity5 true diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java index 3579b1d2ad..ab08e499b0 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java @@ -28,7 +28,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.thymeleaf.dialect.IDialect; import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect; -import org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect; +import org.thymeleaf.extras.springsecurity5.dialect.SpringSecurityDialect; import org.thymeleaf.spring5.ISpringWebFluxTemplateEngine; import org.thymeleaf.spring5.SpringTemplateEngine; import org.thymeleaf.spring5.SpringWebFluxTemplateEngine; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafReactiveAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafReactiveAutoConfigurationTests.java index f93b055890..bf13da46dd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafReactiveAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafReactiveAutoConfigurationTests.java @@ -27,8 +27,11 @@ import org.junit.Rule; import org.junit.Test; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; +import org.thymeleaf.context.IContext; +import org.thymeleaf.extras.springsecurity5.util.SpringSecurityContextUtils; import org.thymeleaf.spring5.ISpringWebFluxTemplateEngine; import org.thymeleaf.spring5.SpringWebFluxTemplateEngine; +import org.thymeleaf.spring5.context.webflux.SpringWebFluxContext; import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; import org.thymeleaf.spring5.view.reactive.ThymeleafReactiveViewResolver; import org.thymeleaf.templateresolver.ITemplateResolver; @@ -42,6 +45,10 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; +import org.springframework.mock.http.server.reactive.MockServerHttpRequest; +import org.springframework.mock.web.server.MockServerWebExchange; +import org.springframework.security.authentication.TestingAuthenticationToken; +import org.springframework.security.core.context.SecurityContextImpl; import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -190,6 +197,22 @@ public class ThymeleafReactiveAutoConfigurationTests { assertThat(result).isEqualTo("2015-11-24"); } + @Test + public void useSecurityDialect() { + load(BaseConfiguration.class); + ISpringWebFluxTemplateEngine engine = this.context + .getBean(ISpringWebFluxTemplateEngine.class); + MockServerWebExchange exchange = MockServerWebExchange + .from(MockServerHttpRequest.get("/test").build()); + exchange.getAttributes().put( + SpringSecurityContextUtils.SECURITY_CONTEXT_MODEL_ATTRIBUTE_NAME, + new SecurityContextImpl( + new TestingAuthenticationToken("alice", "admin"))); + IContext attrs = new SpringWebFluxContext(exchange); + String result = engine.process("security-dialect", attrs); + assertThat(result).isEqualTo("
alice
\n"); + } + @Test public void renderTemplate() { load(BaseConfiguration.class); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java index 07720e8823..eff14e6168 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java @@ -30,6 +30,7 @@ import org.junit.Rule; import org.junit.Test; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; +import org.thymeleaf.context.WebContext; import org.thymeleaf.spring5.SpringTemplateEngine; import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; import org.thymeleaf.spring5.view.ThymeleafView; @@ -48,6 +49,9 @@ import org.springframework.context.annotation.Import; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockServletContext; +import org.springframework.security.authentication.TestingAuthenticationToken; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.context.SecurityContextImpl; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.ViewResolver; @@ -183,6 +187,23 @@ public class ThymeleafServletAutoConfigurationTests { assertThat(result).isEqualTo("2015-11-24"); } + @Test + public void useSecurityDialect() { + load(BaseConfiguration.class); + TemplateEngine engine = this.context.getBean(TemplateEngine.class); + WebContext attrs = new WebContext(new MockHttpServletRequest(), + new MockHttpServletResponse(), new MockServletContext()); + try { + SecurityContextHolder.setContext(new SecurityContextImpl( + new TestingAuthenticationToken("alice", "admin"))); + String result = engine.process("security-dialect", attrs); + assertThat(result).isEqualTo("
alice
\n"); + } + finally { + SecurityContextHolder.clearContext(); + } + } + @Test public void renderTemplate() { load(BaseConfiguration.class); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/templates/security-dialect.html b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/templates/security-dialect.html new file mode 100644 index 0000000000..a22a79e607 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/templates/security-dialect.html @@ -0,0 +1 @@ +
diff --git a/spring-boot-project/spring-boot-dependencies/pom.xml b/spring-boot-project/spring-boot-dependencies/pom.xml index fdaf69e4de..ef208e0bde 100644 --- a/spring-boot-project/spring-boot-dependencies/pom.xml +++ b/spring-boot-project/spring-boot-dependencies/pom.xml @@ -182,7 +182,7 @@ ${javax-mail.version} 1.5.0 3.0.10.RELEASE - 3.0.2.RELEASE + 3.0.3.RELEASE 2.3.0 2.0.1 3.0.1.RELEASE @@ -2937,8 +2937,8 @@
org.thymeleaf.extras - thymeleaf-extras-springsecurity4 - ${thymeleaf-extras-springsecurity4.version} + thymeleaf-extras-springsecurity5 + ${thymeleaf-extras-springsecurity5.version} org.webjars diff --git a/spring-boot-project/spring-boot-docs/pom.xml b/spring-boot-project/spring-boot-docs/pom.xml index 6c565a6913..57f9d063e8 100644 --- a/spring-boot-project/spring-boot-docs/pom.xml +++ b/spring-boot-project/spring-boot-docs/pom.xml @@ -904,7 +904,7 @@ org.thymeleaf.extras - thymeleaf-extras-springsecurity4 + thymeleaf-extras-springsecurity5 true