diff --git a/spring-security-rest-custom/.springBeans b/spring-security-rest-custom/.springBeans index a79097f40d..f25fc5ab49 100644 --- a/spring-security-rest-custom/.springBeans +++ b/spring-security-rest-custom/.springBeans @@ -1,13 +1,12 @@ 1 - + - src/main/webapp/WEB-INF/api-servlet.xml diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/config/MainWebAppInitializer.java b/spring-security-rest-custom/src/main/java/org/baeldung/config/MainWebAppInitializer.java index 0d2c2be770..0a11cdab72 100644 --- a/spring-security-rest-custom/src/main/java/org/baeldung/config/MainWebAppInitializer.java +++ b/spring-security-rest-custom/src/main/java/org/baeldung/config/MainWebAppInitializer.java @@ -2,6 +2,7 @@ package org.baeldung.config; import java.util.Set; +import javax.servlet.FilterRegistration.Dynamic; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; @@ -9,6 +10,7 @@ import javax.servlet.ServletRegistration; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.filter.DelegatingFilterProxy; import org.springframework.web.servlet.DispatcherServlet; public class MainWebAppInitializer implements WebApplicationInitializer { @@ -43,6 +45,11 @@ public class MainWebAppInitializer implements WebApplicationInitializer { if (!mappingConflicts.isEmpty()) { throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278"); } + + // spring security filter + final DelegatingFilterProxy springSecurityFilterChain = new DelegatingFilterProxy("springSecurityFilterChain"); + final Dynamic addedFilter = sc.addFilter("springSecurityFilterChain", springSecurityFilterChain); + addedFilter.addMappingForUrlPatterns(null, false, "/*"); } } 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 bbddbe2bae..0c2042f711 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,8 +2,10 @@ package org.baeldung.config.child; import java.util.List; +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.config.annotation.EnableWebMvc; @@ -12,6 +14,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter @Configuration @EnableWebMvc @ComponentScan("org.baeldung.web") +// @ImportResource({ "classpath:prop.xml" }) +// @PropertySource("classpath:foo.properties") public class WebConfig extends WebMvcConfigurerAdapter { public WebConfig() { @@ -26,6 +30,13 @@ public class WebConfig extends WebMvcConfigurerAdapter { converters.add(new MappingJackson2HttpMessageConverter()); } - // + // beans + + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + final PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); + ppc.setIgnoreUnresolvablePlaceholders(true); + return ppc; + } } \ No newline at end of file diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/config/parent/ServiceConfig.java b/spring-security-rest-custom/src/main/java/org/baeldung/config/parent/ServiceConfig.java index 11ff616f88..02af0165f4 100644 --- a/spring-security-rest-custom/src/main/java/org/baeldung/config/parent/ServiceConfig.java +++ b/spring-security-rest-custom/src/main/java/org/baeldung/config/parent/ServiceConfig.java @@ -1,14 +1,28 @@ package org.baeldung.config.parent; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @Configuration @ComponentScan("org.baeldung.service") +// @ImportResource({ "classpath:prop.xml" }) +@PropertySource("classpath:foo.properties") public class ServiceConfig { public ServiceConfig() { super(); } + // beans + + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + final PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); + ppc.setIgnoreUnresolvablePlaceholders(true); + return ppc; + } + } \ No newline at end of file diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/service/FooService.java b/spring-security-rest-custom/src/main/java/org/baeldung/service/FooService.java index 0709426e1f..cb374ae4b0 100644 --- a/spring-security-rest-custom/src/main/java/org/baeldung/service/FooService.java +++ b/spring-security-rest-custom/src/main/java/org/baeldung/service/FooService.java @@ -1,10 +1,20 @@ package org.baeldung.service; import org.baeldung.web.dto.Foo; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; @Service -public class FooService implements IFooService { +public class FooService implements IFooService, InitializingBean { + + @Value("${foo1}") + private String foo1; + + @Autowired + private Environment env; public FooService() { super(); @@ -17,4 +27,10 @@ public class FooService implements IFooService { return new Foo(); } + @Override + public final void afterPropertiesSet() { + System.out.println("In Parent Context, property via @Value = " + foo1); + System.out.println("In Parent Context, property via env = " + env.getProperty("foo2")); + } + } diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/FooController.java b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/FooController.java index 4fa8b8094e..74fd5e403f 100644 --- a/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/FooController.java +++ b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/FooController.java @@ -2,7 +2,10 @@ package org.baeldung.web.controller; import org.baeldung.service.IFooService; import org.baeldung.web.dto.Foo; +import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.Environment; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -10,8 +13,14 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller -@RequestMapping(value = "/foo") -public class FooController { +@RequestMapping(value = "/foos") +public class FooController implements InitializingBean { + + @Value("${foo1}") + private String foo1; + + @Autowired + private Environment env; @Autowired private IFooService service; @@ -28,4 +37,10 @@ public class FooController { return service.findOne(id); } + @Override + public final void afterPropertiesSet() { + System.out.println("In Child Context, property via @Value = " + foo1); + System.out.println("In Child Context, property via env = " + env.getProperty("foo2")); + } + } diff --git a/spring-security-rest-custom/src/main/resources/foo.properties b/spring-security-rest-custom/src/main/resources/foo.properties new file mode 100644 index 0000000000..3fe2eceff0 --- /dev/null +++ b/spring-security-rest-custom/src/main/resources/foo.properties @@ -0,0 +1,2 @@ +foo1=bar1 +foo2=bar2 \ No newline at end of file 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..4c11f2154c --- /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/webapp/WEB-INF/api-servlet.xml b/spring-security-rest-custom/src/main/webapp/WEB-INF/api-servlet.xml deleted file mode 100644 index d6e8f7549a..0000000000 --- a/spring-security-rest-custom/src/main/webapp/WEB-INF/api-servlet.xml +++ /dev/null @@ -1,5 +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 index 950bbab505..15f790d77a 100644 --- 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 @@ -37,7 +37,6 @@ /api/* - springSecurityFilterChain