Spring


How to force Spring Boot Security to say a little bit more?

By default spring boot security isn’t too talkative.
If you struggle with some problems and want to see what is going on under the covers, you can always switch to wider log level.

You can do it by providing this property in your application.yml or application.properties file.

  1. logging.level.org.springframework.security: DEBUG

Additionaly you can turn on Spring Web debug logs and set debug parameter to true in your @EnableWebSecurity annotation

  1. logging.level.org.springframework.web: DEBUG
  1. @EnableWebSecurity(debug = true)
  2. public class SecurityConfig extends WebSecurityConfigurerAdapter {...}

Spring test configuration example.

Hello.
Today I want to show you simple test configuration in spring mvc project.
In example I use java based configuration(I admit that this kind of configuration I prefer).
But in future I want to present xml based configuration, too.

On the bottom of the post you find link to github repository with full project.

Class under testing:

  1. package com.raphaelsolarski.springtestconfig.controller;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6.  
  7. @Controller
  8. public class MyController {
  9.  
  10. @RequestMapping(path = "/", method = RequestMethod.GET)
  11. String home() {
  12. return "home";
  13. }
  14.  
  15. }

Test:

  1. package com.raphaelsolarski.springtestconfig.controller;
  2.  
  3. import com.raphaelsolarski.springtestconfig.config.WebConfig;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.test.context.ContextConfiguration;
  9. import org.springframework.test.context.TestExecutionListeners;
  10. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  11. import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
  12. import org.springframework.test.context.web.AnnotationConfigWebContextLoader;
  13. import org.springframework.test.context.web.WebAppConfiguration;
  14. import org.springframework.test.web.servlet.MockMvc;
  15. import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
  16. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  17. import org.springframework.web.context.WebApplicationContext;
  18.  
  19. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  20.  
  21. @RunWith(SpringJUnit4ClassRunner.class)
  22. @ContextConfiguration(classes = {WebConfig.class}, loader = AnnotationConfigWebContextLoader.class)
  23. @TestExecutionListeners(listeners = {DependencyInjectionTestExecutionListener.class})
  24. @WebAppConfiguration
  25. public class MyControllerTest {
  26.  
  27. @Autowired
  28. private WebApplicationContext webApplicationContext;
  29.  
  30. private MockMvc mockMvc;
  31.  
  32. @Before
  33. public void setUp() {
  34. mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
  35. }
  36.  
  37. @Test
  38. public void getRootShouldReturnHomeView() throws Exception {
  39. mockMvc.perform(get("/")).andExpect(MockMvcResultMatchers.view().name("home"));
  40. }
  41.  
  42. }

github-icon Full code on github.com

Links:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html


Problem with spring @Value annotation.

Hi. Today I expirienced problem with @Value annotation in spring.
This annotation can be used to inject value from properties files in spring based applications.
I will show you a typical use case of this annotation, the problem I encountered and how I dealt with it.

foo.properties:

  1. address=Warsaw

Foo.java:

  1. package com.raphaelsolarski.value;
  2.  
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.PropertySource;
  5. import org.springframework.stereotype.Component;
  6.  
  7. @Component
  8. @PropertySource("classpath:foo.properties")
  9. public class Foo {
  10.  
  11. @Value("${address}")
  12. private String address;
  13.  
  14. public String getAddress() {
  15. return address;
  16. }
  17.  
  18. }

FooTest.java:

  1. package com.raphaelsolarski.value;
  2.  
  3. import com.raphaelsolarski.value.config.Config;
  4. import org.junit.Assert;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.test.context.ContextConfiguration;
  9. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  10.  
  11. @RunWith(SpringJUnit4ClassRunner.class)
  12. @ContextConfiguration(classes = Config.class)
  13. public class FooTest {
  14.  
  15. @Autowired
  16. private Foo foo;
  17.  
  18. @Test
  19. public void shouldReturnPropertyValue() throws Exception {
  20. String actual = foo.getAddress();
  21. String expected = "Warsaw";
  22. Assert.assertEquals(expected, actual);
  23. }
  24.  
  25. }

The test will fail with the following error:

  1. org.junit.ComparisonFailure:
  2. Expected :Warsaw
  3. Actual :${address}

There is a simple solution. We have to declare a PropertySourcesPlaceholderConfigurer bean somewhere in spring configuration of our application.

Config.java

  1. package com.raphaelsolarski.value.config;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.ComponentScan;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
  7.  
  8. @Configuration
  9. @ComponentScan("com.raphaelsolarski.value")
  10. public class Config {
  11.  
  12. @Bean
  13. public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
  14. return new PropertySourcesPlaceholderConfigurer();
  15. }
  16.  
  17. }
  18.  

github-icon Full code on github.com


How to add filter to spring mvc in java config?

In this post I show you how to add filter in two different kinds of spring based configurations.

Configuration by WebApplicationInitializer

  1. package raphaelsolarski.config;
  2.  
  3. import org.springframework.web.WebApplicationInitializer;
  4. import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
  5. import org.springframework.web.servlet.DispatcherServlet;
  6. import raphaelsolarski.filter.MyFilter;
  7. import javax.servlet.ServletContext;
  8. import javax.servlet.ServletRegistration;
  9.  
  10. public class MyWebAppInitializer implements WebApplicationInitializer {
  11.  
  12. public void onStartup(ServletContext container) {
  13. //here I can set up any kind of spring context -> xml/java...
  14. AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
  15. appContext.scan("raphaelsolarski");
  16. ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(appContext));
  17. registration.setLoadOnStartup(1);
  18. registration.addMapping("/");
  19. container.addFilter("My filter", MyFilter.class).addMappingForServletNames(null, false, "dispatcher");
  20. //or container.addFilter("My filter", MyFilter.class).addMappingForUrlPatterns(null, false, "/*");
  21. }
  22.  
  23. }

Configuration by AbstractAnnotationConfigDispatcherServletInitializer

  1. package com.raphaelsolarski.config;
  2.  
  3. import com.raphaelsolarski.filter.MyFilter;
  4. import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
  5. import javax.servlet.Filter;
  6.  
  7. public class MyDispatcher extends AbstractAnnotationConfigDispatcherServletInitializer{
  8. @Override
  9. protected Class<!--?-->[] getRootConfigClasses() {
  10. return null;
  11. }
  12.  
  13. @Override
  14. protected Class<!--?-->[] getServletConfigClasses() {
  15. return new Class<!--?-->[] {SpringConfig.class};
  16. }
  17.  
  18. @Override
  19. protected String[] getServletMappings() {
  20. return new String[] {"/"};
  21. }
  22.  
  23. @Override
  24. protected Filter[] getServletFilters() {
  25. return new Filter[] { new MyFilter() };
  26. }
  27. }

How to configure dispatcher servlet without web.xml?

In my first post I want show you how to simply configure Dispatcher Servlet with Spring without xml code.
I will cover three types of spring configuration you may want to use(I think about cofiguration of rest of application, not about dispatcher servlet).
Java annotations driven config, xml based and mixture of both of them.

In configuration I will WebApplicationInitializer interface and AbstractDispatcherServletInitializer class.
When you want to configure dispatcher servlet in java you probably should use one of them.

Configuration by implementing WebApplicationInitializer interface:

  1. import org.springframework.web.WebApplicationInitializer;
  2.  
  3. public class MyWebApplicationInitializer implements WebApplicationInitializer {
  4.  
  5. @Override
  6. public void onStartup(ServletContext container) {
  7. XmlWebApplicationContext appContext = new XmlWebApplicationContext();
  8. appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
  9.  
  10. ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(appContext));
  11. registration.setLoadOnStartup(1);
  12. registration.addMapping("/");
  13. }
  14.  
  15. }

Configuration by extending AbstractDispatcherServletInitializer(for using xml based spring configuration):

  1. public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {
  2.  
  3. @Override
  4. protected WebApplicationContext createRootApplicationContext() {
  5. return null;
  6. }
  7.  
  8. @Override
  9. protected WebApplicationContext createServletApplicationContext() {
  10. XmlWebApplicationContext cxt = new XmlWebApplicationContext();
  11. cxt.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
  12. return cxt;
  13. }
  14.  
  15. @Override
  16. protected String[] getServletMappings() {
  17. return new String[] { "/" };
  18. }
  19.  
  20. }

Configuration by extending AbstractAnnotationConfigDispatcherServletInitializer class(for java based spring configuration):

  1. public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  2.  
  3. @Override
  4. protected Class<!--?-->[] getRootConfigClasses() {
  5. return null;
  6. }
  7.  
  8. @Override
  9. protected Class<!--?-->[] getServletConfigClasses() {
  10. return new Class[] { MyWebConfig.class };
  11. }
  12.  
  13. @Override
  14. protected String[] getServletMappings() {
  15. return new String[] { "/" };
  16. }
  17.  
  18. }

Links:
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/WebApplicationInitializer.html
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-container-config