我是Spring新手,使用的是Spring 3.2.5 RELEASE。我有一个名为MongoUserDetailsService的自定义UserDetailsSevice。这是我的application-security.xml。
<http auto-config="true">
<intercept-url pattern="/secured/*" access="ROLE_USER" />
<form-login login-processing-url="/login" login-page="/loginPage"
username-parameter="username" password-parameter="password"
default-target-url="/secured/mypage" authentication-failure-url="/loginPage?auth=fail" />
<logout logout-url="/logout" logout-success-url="/logoutPage" />
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="mongoUserDetailsService">
<password-encoder hash="plaintext" />
</authentication-provider>
</authentication-manager>
这是我的dispatcher-servlet.xml
<context:component-scan base-package="com.srccodes.spring.controller" />
<mvc:annotation-driven />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="mongoUserDetailsService" class="com.srccodes.spring.security.MongoUserDetailsService">
</bean>
我在application-security.xml中收到了一个bean not found错误,其中提供了身份验证提供程序。我检查了路径并且它们是正确的。
我也在添加我的web.xml。
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring context files to be loaded -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml,
/WEB-INF/application-security.xml,
/WEB-INF/mongo-config.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- filter declaration for Spring Security -->
<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 :(得分:0)
必须导入dispatcher-servlet.xml,以下代码显示import sysntax: 1-如果dispatcher-servlet.xml是buildPath:
<import resource="classpath:DIR/dispatcher-servlet.xml" />
2-如果是WEB-INF DIR
<import resource="DIR/dispatcher-servlet.xml" />
我希望能帮到你
答案 1 :(得分:0)
如果您已将application-security.xml
定义为根上下文(即,它是通过... ContextLoaderListener加载)而您的dispatcher-servlet.xml
通过DispatcherServlet
定义,则您将具有可见性/范围的问题。 DispatcherServlet上下文是根上下文的子项,因此DispatcherServlet上下文中定义的bean对根上下文不可见,但根上下文bean对所有子servlet上下文都是可见的。
请将您的mongoUserDetailsService
移至您的application-security.xml
更新:
您正在加载dispatcher-servlet.xml配置两次,一次是
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.xml
/WEB-INF/application-security.xml
/WEB-INF/mongo-config.xml
</param-value>
</context-param>
并使用DispatcherServlet隐式地(通过Spring约定)。您应该签出Spring reference以更好地理解ApplicationContext和DispatcherServlet
<context:component-scan base-package="com.srccodes.spring" /> <context:component-scan base-package="com.srccodes.spring.security" /> <context:component-scan base-package="com.srccodes.spring.domain" /> <context:component-scan base-package="com.srccodes.spring.repositories" /> <context:component-scan base-package="com.srccodes.spring.controller" />