我很習慣使用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的UserDetailsService的:
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>