Why this Spring application with java-based configuration don’t work properly

java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present – check whether you have multiple ContextLoader* definitions in your web.xml! In your WebAppInitializer, you are registering a ContextLoaderListener. Because you are using AbstractSecurityWebApplicationInitializer with the super constructor that accepts a Class[] Creates a new instance that will instantiate the ContextLoaderListener with … Read more

My Application Could not open ServletContext resource

Quote from the Spring reference doc: Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there… Your servlet is called spring-dispatcher, so it looks for /WEB-INF/spring-dispatcher-servlet.xml. You need to have this servlet configuration, and define web related beans … Read more

Spring Boot with embedded Tomcat behind Apache proxy

I had the same problem the other day. After some debugging of Spring Boot 1.3 I found the following solution. 1. You have to setup the headers on your Apache proxy: <VirtualHost *:443> ServerName www.myapp.org ProxyPass / http://127.0.0.1:8080/ RequestHeader set X-Forwarded-Proto https RequestHeader set X-Forwarded-Port 443 ProxyPreserveHost On … (SSL directives omitted for readability) </VirtualHost> … Read more

Spring Security HTTP Basic for RESTFul and FormLogin (Cookies) for web – Annotations

Waited for 2 days and didn’t get any help here. But my research provided me a solution 🙂 Solution @Configuration @EnableWebMvcSecurity @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ @Autowired private AuthenticationProvider authenticationProvider; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(authenticationProvider); } @Configuration @Order(1) public static class ApiWebSecurityConfig … Read more

Spring security: adding “On successful login event listener”

You need to define a Spring Bean which implements ApplicationListener. Then, in your code, do something like this: public void onApplicationEvent(ApplicationEvent appEvent) { if (appEvent instanceof AuthenticationSuccessEvent) { AuthenticationSuccessEvent event = (AuthenticationSuccessEvent) appEvent; UserDetails userDetails = (UserDetails) event.getAuthentication().getPrincipal(); // …. } } Then, in your applicationContext.xml file, just define that bean and it will automatically … Read more