title: SpringSecurity Autowire Workaround
body: <p>It's apparently well documented that <code>@Autowired</code> won't work inside a <code>UserDetailsService</code></p>
  
  <p>Is this true? If not, how can I autowire inside my UserDetailsService?</p>
  
  <p>If it is true, are there any work arounds? How can I execute a hibernate query?</p>
  
  <p>The accountDao I bind in AccountService works fine if I put it in the application context and use it in any other class. I read somewhere that Autowire didn't work because the <code>UserDetailsService</code> was out of the scope where spring would bind? and that a work around was to manually wire it in xml. If that's true? How does that work?</p>
  
  <p><strong>AccountService:</strong></p>
  
  <pre><code>@Service
  public class AccountService implements UserDetailsService {
  
      @Autowired
      AccountDao accountDao;
  
      @Override
      public UserDetails loadUserByUsername(String username)
              throws UsernameNotFoundException, DataAccessException {
  
          Account account = accountDao.getUser(username);
          if (account == null)
              throw new UsernameNotFoundException("No account found for '"
                      + username + "'");
  
          return account;
      }
  
      @SuppressWarnings("unused")
      private GrantedAuthority[] getAuthorities(boolean isAdmin) {
          List&lt;GrantedAuthority&gt; authList = new ArrayList&lt;GrantedAuthority&gt;(2);
          authList.add(new GrantedAuthorityImpl("ROLE_USER"));
          if (isAdmin) {
              authList.add(new GrantedAuthorityImpl("ROLE_ADMIN"));
          }
          return authList.toArray(new GrantedAuthority[] {});
      }
  
      public void setAccountDao(AccountDao accountDao) {
          this.accountDao = accountDao;
      }
  </code></pre>
  
  <p><strong>Security Context</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/security 
     http://www.springframework.org/schema/security/spring-security-3.0.xsd"&gt;
  
      &lt;!-- This is where we configure Spring-Security --&gt;
      &lt;security:http auto-config="true" use-expressions="true"
          access-denied-page="/krams/auth/denied"&gt;
  
          &lt;security:intercept-url pattern="/" access="permitAll" /&gt;
          &lt;security:intercept-url pattern="/campaign/*"
              access="hasRole('ROLE_ADMIN')" /&gt;
          &lt;!-- &lt;security:intercept-url pattern="/city/*" access="hasRole('ROLE_ADMIN')"/&gt; --&gt;
          &lt;security:intercept-url pattern="/company/*"
              access="hasRole('ROLE_ADMIN')" /&gt;
          &lt;security:intercept-url pattern="/krams/main/common"
              access="hasRole('ROLE_USER')" /&gt;
  
          &lt;security:form-login login-page="/auth/login"
              authentication-failure-url="/auth/login?error=true"
              default-target-url="/home" /&gt;
  
          &lt;security:logout invalidate-session="true"
              logout-success-url="/auth/login" logout-url="/auth/logout" /&gt;
  
      &lt;/security:http&gt;
  
      &lt;!-- Declare an authentication-manager to use a custom userDetailsService --&gt;
      &lt;security:authentication-manager&gt;
          &lt;security:authentication-provider
              user-service-ref="customUserDetailsService"&gt;
              &lt;security:password-encoder ref="passwordEncoder" /&gt;
          &lt;/security:authentication-provider&gt;
      &lt;/security:authentication-manager&gt;
  
      &lt;!-- Use a Md5 encoder since the user's passwords are stored as Md5 in the 
          database --&gt;
      &lt;bean
          class="org.springframework.security.authentication.encoding.Md5PasswordEncoder"
          id="passwordEncoder" /&gt;
  
  
      &lt;bean id="customUserDetailsService" class="com.groupdealclone.app.service.AccountService" /&gt;
      &lt;bean id="accountDao" class="com.groupdealclone.app.dao.JdbcAccountDao" /&gt;
  
  
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>Web.xml</strong></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"
      version="2.5"&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;
              /WEB-INF/spring/root-context.xml
              /WEB-INF/spring/security-context.xml
          &lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;!--    
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.html&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
       --&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&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;filter&gt;
          &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;
              org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
          &lt;/filter-class&gt;
      &lt;/filter&gt;
  
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;
              index.jsp
          &lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  
  &lt;/web-app&gt;
  </code></pre>