Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to run a spring webmvc webapp with spring security in one spring container?
    text
    copied!<p>Here's my scenario: I've got a spring webapp (uses webmvc) that I just added spring security to. I've got a CRUD user manager page that allows a user with sufficient privileges to add users. On the server, this is handled by a com.myapp.UserController class. I've also got my own implementation of org.springframework.security.core.userdetails.UserDetailsService, called com.myapp.UserDetailsServiceImpl. Both the UserController and my UserDetailsService impl use a com.myapp.UserService that keeps track of users in a java.util.List.</p> <p>What I found was that when I created a new user via my web interface, logged out, and then tried to log back in with that new user, the user is not found. Upon closer inspection, it's because there are two different instances of com.myapp.UserService. The one used by the UserController has the new user, but the one used by UserDetailsServiceImpl does not.</p> <p>I tracked the cause down to the fact that two different spring containers run when I start this webapp. One is for webmvc (myapp-servlet.xml), and one is for security (myapp-security.xml). Each has its own instance of UserService. It's my understanding that this is a pretty common scenario for configuring spring webmvc with security:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"&gt; &lt;servlet&gt; &lt;servlet-name&gt;myapp&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;myapp&lt;/servlet-name&gt; &lt;url-pattern&gt;*.html&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/myapp-servlet.xml, /WEB-INF/myapp-security.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;filter&gt; &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;/web-app&gt; </code></pre> <p>In my myapp-servlet.xml, I've got component scanning going on, which picks up the UserService since it's annotated with @Service: </p> <pre><code> &lt;context:component-scan base-package="com.myapp" /&gt; </code></pre> <p>But UserService is also picked up by myapp-security.xml, because it's referenced as part of my authentication-manager configuration:</p> <pre><code> &lt;authentication-manager&gt; &lt;authentication-provider user-service-ref="userDetailsService" /&gt; &lt;/authentication-manager&gt; </code></pre> <p>Here is the relevant part of com.myapp.UserDetailsServiceImpl for completeness:</p> <pre><code>@Service("userDetailsService") public class UserDetailsServiceImpl implements UserDetailsService { @Autowired @Qualifier("testUserService") private UserService userService; ... } </code></pre> <p>And my UserController:</p> <pre><code>@Controller @RequestMapping("/admin/users") public class UserController { @Autowired @Qualifier("testUserService") private UserService userService; ... } </code></pre> <p>My question is this: is there a way to combine both the webmvc and the security into one spring container, so that I don't have duplicate instances of UserService being created?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload