0

我很習慣使用Spring和Spring Security,但我無法弄清楚爲什麼在這裏依賴注入不起作用。 我有兩個基本身份驗證系統的應用程序,一個用於用戶,另一個用於兩個管理員。這是安全的context.xml:爲什麼這個bean沒有被注入?

<http use-expressions="true"> 
    <intercept-url pattern="/" access="isAuthenticated()" /> 
    <intercept-url pattern="/*" access="isAuthenticated()" /> 
    <intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')" /> 
    <intercept-url pattern="/admin/*" access="hasRole('ROLE_ADMIN')" /> 
    <http-basic /> 
</http> 

<beans:bean id="userDetailsServiceImpl" 
    class="com.foo.bar.service.LoginServiceImpl" /> 

<authentication-manager> 
    <authentication-provider user-service-ref="userDetailsServiceImpl" /> 
    <authentication-provider> 
     <password-encoder hash="md5" /> 
     <user-service> 
      <user name="AdminOne" password="eh223rh2efi9hfuwefugwg" 
       authorities="ROLE_ADMIN" /> 
      <user name="AdminTwo" password="wdkjbcfwerjkbiwfviwefi" 
       authorities="ROLE_ADMIN" /> 
     </user-service> 
    </authentication-provider> 
</authentication-manager> 

用戶認證依靠這種implementetaion的UserDetailsS​​ervice的:

public class LoginServiceImpl implements UserDetailsService { 

@Autowired 
UserDetailsDAO dao; 

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { 

    try { 
     // Get the credentials 
     return credentials; 
    } catch (NoResultException exc) { 

     throw new UsernameNotFoundException("username not found"); 
    } catch (JDBCException jdbce) { 
     throw new DataAccessException("Cannot acess db", jdbce) { 
      private static final long serialVersionUID = 1L; 
     }; 
    } 
} 

的問題是,該領域UserDetailsDAO爲空。所以不注射。奇怪的是,在測試環境中,一切正常。在服務器啓動過程中不會引發異常,這也很奇怪,因爲通常會報告自動裝配中的錯誤。順便說一句,管理員的身份驗證就像一個魅力。

如果是有用的,這是在web.xml: 富

<servlet> 
    <servlet-name>Foo Dispatcher Servlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
     <!-- Here the context specific for a single project --> 
     /WEB-INF/spring/root-context.xml 
       /WEB-INF/spring/datasource-context.xml 
     </param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>StreamViewer Dispatcher Servlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
    <!-- Here the context for all the projects --> 
     /WEB-INF/spring/security-context.xml 
    </param-value> 
</context-param> 

<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

回答

0

如果您想使用自動裝配,您將需要掃描豆,而不是在XML文件中明確聲明他們。

此時應更換

<beans:bean id="userDetailsServiceImpl" 
    class="com.foo.bar.service.LoginServiceImpl" /> 

有:

<context:component-scan base-package="com.foo.bar" /> 

,並註解你LoginServiceImpl類:

@Service(name="userDetailsServiceImpl") 
1

你應該有<context:annotation-config />security-context.xml使AutowiredAnnotationBeanPostProcessor這臺值爲@Autowired帶註釋的字段。

從技術文檔:

默認 AutowiredAnnotationBeanPostProcessor將由 「背景:註釋的配置」來註冊和「上下文:組件掃描」 XML標籤。 如果您打算指定定製的AutowiredAnnotationBeanPostProcessor bean 定義,那麼刪除或關閉默認註釋配置。

請參閱Is context:annotation-config an alternative to @AutoWired?

相關問題