From a59168b67fe9fd0e8e46236104d19b671dcecd01 Mon Sep 17 00:00:00 2001 From: amit2103 Date: Thu, 23 May 2019 12:37:11 +0530 Subject: [PATCH 1/3] [BAEL-14777] - Removed old spring 4 and xml related settings --- .../org/baeldung/config/child/WebConfig.java | 2 - .../config/parent/SecurityConfig.java | 1 - .../src/main/resources/prop.xml | 13 ----- .../src/main/resources/webSecurityConfig.xml | 21 -------- .../src/main/webapp/WEB-INF/web_old.xml | 51 ------------------- 5 files changed, 88 deletions(-) delete mode 100644 spring-security-rest-custom/src/main/resources/prop.xml delete mode 100644 spring-security-rest-custom/src/main/resources/webSecurityConfig.xml delete mode 100644 spring-security-rest-custom/src/main/webapp/WEB-INF/web_old.xml diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/config/child/WebConfig.java b/spring-security-rest-custom/src/main/java/org/baeldung/config/child/WebConfig.java index d7b2da6fe7..6688d41ffa 100644 --- a/spring-security-rest-custom/src/main/java/org/baeldung/config/child/WebConfig.java +++ b/spring-security-rest-custom/src/main/java/org/baeldung/config/child/WebConfig.java @@ -14,8 +14,6 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableWebMvc @ComponentScan("org.baeldung.web") -// @ImportResource({ "classpath:prop.xml" }) -// @PropertySource("classpath:foo.properties") public class WebConfig implements WebMvcConfigurer { public WebConfig() { diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/config/parent/SecurityConfig.java b/spring-security-rest-custom/src/main/java/org/baeldung/config/parent/SecurityConfig.java index 616c2a7684..74e2df2174 100644 --- a/spring-security-rest-custom/src/main/java/org/baeldung/config/parent/SecurityConfig.java +++ b/spring-security-rest-custom/src/main/java/org/baeldung/config/parent/SecurityConfig.java @@ -10,7 +10,6 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration -// @ImportResource({ "classpath:webSecurityConfig.xml" }) @EnableWebSecurity @ComponentScan("org.baeldung.security") public class SecurityConfig extends WebSecurityConfigurerAdapter { diff --git a/spring-security-rest-custom/src/main/resources/prop.xml b/spring-security-rest-custom/src/main/resources/prop.xml deleted file mode 100644 index b853d939d6..0000000000 --- a/spring-security-rest-custom/src/main/resources/prop.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/spring-security-rest-custom/src/main/resources/webSecurityConfig.xml b/spring-security-rest-custom/src/main/resources/webSecurityConfig.xml deleted file mode 100644 index fa5dc894bf..0000000000 --- a/spring-security-rest-custom/src/main/resources/webSecurityConfig.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-security-rest-custom/src/main/webapp/WEB-INF/web_old.xml b/spring-security-rest-custom/src/main/webapp/WEB-INF/web_old.xml deleted file mode 100644 index 0fa9abd258..0000000000 --- a/spring-security-rest-custom/src/main/webapp/WEB-INF/web_old.xml +++ /dev/null @@ -1,51 +0,0 @@ - - - - Spring Security Custom Application - - - - contextClass - - org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - - contextConfigLocation - org.baeldung.config.parent - - - - org.springframework.web.context.ContextLoaderListener - - - - - api - org.springframework.web.servlet.DispatcherServlet - - - - - 1 - - - api - /api/* - - - - - springSecurityFilterChain - org.springframework.web.filter.DelegatingFilterProxy - - - springSecurityFilterChain - /* - - - \ No newline at end of file From d4a51e7ac883bb426d1c344a5e0ad4e5bf28b7ce Mon Sep 17 00:00:00 2001 From: amit2103 Date: Thu, 23 May 2019 21:06:21 +0530 Subject: [PATCH 2/3] [BAEL-14777] - Added wrongly deleted web security config xml --- .../src/main/resources/webSecurityConfig.xml | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 spring-security-rest-custom/src/main/resources/webSecurityConfig.xml diff --git a/spring-security-rest-custom/src/main/resources/webSecurityConfig.xml b/spring-security-rest-custom/src/main/resources/webSecurityConfig.xml new file mode 100644 index 0000000000..fa5dc894bf --- /dev/null +++ b/spring-security-rest-custom/src/main/resources/webSecurityConfig.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + \ No newline at end of file From fe2cf943d8423ef04c4c63c5532fa5d355bf253e Mon Sep 17 00:00:00 2001 From: amit2103 Date: Sun, 26 May 2019 11:57:20 +0530 Subject: [PATCH 3/3] [BAEL-14777] - Added Thymeleaf example and added reoved xmls and commented out codes --- spring-security-rest-custom/pom.xml | 8 +++ .../org/baeldung/config/child/WebConfig.java | 41 +++++++++++++++ .../config/parent/SecurityConfig.java | 1 + .../web/controller/ViewController.java | 13 +++++ .../src/main/resources/prop.xml | 13 +++++ .../src/main/resources/webSecurityConfig.xml | 4 +- .../main/webapp/WEB-INF/templates/index.html | 7 +++ .../src/main/webapp/WEB-INF/web_old.xml | 51 +++++++++++++++++++ 8 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 spring-security-rest-custom/src/main/java/org/baeldung/web/controller/ViewController.java create mode 100644 spring-security-rest-custom/src/main/resources/prop.xml create mode 100644 spring-security-rest-custom/src/main/webapp/WEB-INF/templates/index.html create mode 100644 spring-security-rest-custom/src/main/webapp/WEB-INF/web_old.xml diff --git a/spring-security-rest-custom/pom.xml b/spring-security-rest-custom/pom.xml index 38cbfddab8..2180b917e5 100644 --- a/spring-security-rest-custom/pom.xml +++ b/spring-security-rest-custom/pom.xml @@ -26,6 +26,14 @@ org.springframework.security spring-security-config + + org.thymeleaf.extras + thymeleaf-extras-springsecurity5 + + + org.thymeleaf + thymeleaf-spring5 + diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/config/child/WebConfig.java b/spring-security-rest-custom/src/main/java/org/baeldung/config/child/WebConfig.java index 6688d41ffa..7cbbcf31fa 100644 --- a/spring-security-rest-custom/src/main/java/org/baeldung/config/child/WebConfig.java +++ b/spring-security-rest-custom/src/main/java/org/baeldung/config/child/WebConfig.java @@ -2,19 +2,34 @@ package org.baeldung.config.child; import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.thymeleaf.extras.springsecurity5.dialect.SpringSecurityDialect; +import org.thymeleaf.spring5.ISpringTemplateEngine; +import org.thymeleaf.spring5.SpringTemplateEngine; +import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; +import org.thymeleaf.spring5.view.ThymeleafViewResolver; +import org.thymeleaf.templatemode.TemplateMode; +import org.thymeleaf.templateresolver.ITemplateResolver; @Configuration @EnableWebMvc @ComponentScan("org.baeldung.web") +//@ImportResource({ "classpath:prop.xml" }) +//@PropertySource("classpath:foo.properties") public class WebConfig implements WebMvcConfigurer { + + @Autowired + private ApplicationContext applicationContext; public WebConfig() { super(); @@ -35,5 +50,31 @@ public class WebConfig implements WebMvcConfigurer { ppc.setIgnoreUnresolvablePlaceholders(true); return ppc; } + + @Bean + public ViewResolver viewResolver() { + ThymeleafViewResolver resolver = new ThymeleafViewResolver(); + resolver.setTemplateEngine(templateEngine()); + resolver.setCharacterEncoding("UTF-8"); + return resolver; + } + + @Bean + public ISpringTemplateEngine templateEngine() { + SpringTemplateEngine engine = new SpringTemplateEngine(); + engine.setEnableSpringELCompiler(true); + engine.setTemplateResolver(templateResolver()); + engine.addDialect(new SpringSecurityDialect()); + return engine; + } + + private ITemplateResolver templateResolver() { + SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver(); + resolver.setApplicationContext(applicationContext); + resolver.setPrefix("/WEB-INF/templates/"); + resolver.setSuffix(".html"); + resolver.setTemplateMode(TemplateMode.HTML); + return resolver; + } } \ No newline at end of file diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/config/parent/SecurityConfig.java b/spring-security-rest-custom/src/main/java/org/baeldung/config/parent/SecurityConfig.java index 74e2df2174..fb3880a33f 100644 --- a/spring-security-rest-custom/src/main/java/org/baeldung/config/parent/SecurityConfig.java +++ b/spring-security-rest-custom/src/main/java/org/baeldung/config/parent/SecurityConfig.java @@ -10,6 +10,7 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration +//@ImportResource({ "classpath:webSecurityConfig.xml" }) @EnableWebSecurity @ComponentScan("org.baeldung.security") public class SecurityConfig extends WebSecurityConfigurerAdapter { diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/ViewController.java b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/ViewController.java new file mode 100644 index 0000000000..83c0292d98 --- /dev/null +++ b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/ViewController.java @@ -0,0 +1,13 @@ +package org.baeldung.web.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class ViewController { + + @RequestMapping({ "/index", "/" }) + public String index() { + return "index"; + } +} diff --git a/spring-security-rest-custom/src/main/resources/prop.xml b/spring-security-rest-custom/src/main/resources/prop.xml new file mode 100644 index 0000000000..edaecde655 --- /dev/null +++ b/spring-security-rest-custom/src/main/resources/prop.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/spring-security-rest-custom/src/main/resources/webSecurityConfig.xml b/spring-security-rest-custom/src/main/resources/webSecurityConfig.xml index fa5dc894bf..790b1b1716 100644 --- a/spring-security-rest-custom/src/main/resources/webSecurityConfig.xml +++ b/spring-security-rest-custom/src/main/resources/webSecurityConfig.xml @@ -2,9 +2,9 @@ diff --git a/spring-security-rest-custom/src/main/webapp/WEB-INF/templates/index.html b/spring-security-rest-custom/src/main/webapp/WEB-INF/templates/index.html new file mode 100644 index 0000000000..31cb66ae0a --- /dev/null +++ b/spring-security-rest-custom/src/main/webapp/WEB-INF/templates/index.html @@ -0,0 +1,7 @@ + + + +
Authenticated as
+ + \ No newline at end of file diff --git a/spring-security-rest-custom/src/main/webapp/WEB-INF/web_old.xml b/spring-security-rest-custom/src/main/webapp/WEB-INF/web_old.xml new file mode 100644 index 0000000000..0fa9abd258 --- /dev/null +++ b/spring-security-rest-custom/src/main/webapp/WEB-INF/web_old.xml @@ -0,0 +1,51 @@ + + + + Spring Security Custom Application + + + + contextClass + + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + + contextConfigLocation + org.baeldung.config.parent + + + + org.springframework.web.context.ContextLoaderListener + + + + + api + org.springframework.web.servlet.DispatcherServlet + + + + + 1 + + + api + /api/* + + + + + springSecurityFilterChain + org.springframework.web.filter.DelegatingFilterProxy + + + springSecurityFilterChain + /* + + + \ No newline at end of file