【发布时间】:2013-06-17 00:58:31
【问题描述】:
我在成功登录后尝试访问受 Spring Security 保护的 URL 时收到 404 错误,但当我不使用 Spring Security 保护 URL 时不会收到错误。
我正在使用 Spring-MVC、Spring Security 和 Hibernate。我试图找出问题可能是什么,但完全失败了。我需要你们的帮助。
我的 spring-security.xml 文件如下:
<http auto-config="true">
<intercept-url pattern="/sec/*" access="ROLE_USER" />
<form-login login-page="/login"
authentication-success-handler-ref="successHandler" authentication-failure-handler-ref="failureHandler"
authentication-failure-url="/login/error" />
<remember-me/>
<logout logout-success-url="/login" />
<access-denied-handler error-page="/403"/>
</http>
dispatcher-servlet.xml 如下:
<mvc:annotation-driven/>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" p:definitions="/WEB-INF/tiles.xml" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/application"/>
<property name="cacheSeconds" value="1"/>
web.xml 如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.xml,
/WEB-INF/spring-security.xml,
/WEB-INF/applicationContext.xml,
/WEB-INF/spring-db.xml
</param-value>
</context-param>
<filter>
<filter-name>hibernateSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
<init-param>
<param-name>flushMode</param-name>
<param-value>ALWAYS</param-value>
</init-param>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<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>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
请注意,Spring Security 身份验证工作得很好。它保护的那种 url 不再被调度程序映射。有人请帮我解决这个问题。提前谢谢你。
【问题讨论】:
-
您的会话启用了吗?你用的是什么应用服务器?
-
我正在使用 tomcat 服务器。我也尝试了 glassfish,它产生了相同的结果。
-
authentication-failure-url="/login/error"你的/login/error工作正常吗?在没有它的情况下尝试使用 spring security。 -
感谢丹尼尔的时间。失败的 url 有效,因为它的访问是匿名的
-
您正在加载
dispatcher-servlet.xml两次!一次在您的根配置中,一次在您的 servlet 中(DispatcherServlet默认从/WEB-INF/{servletName}-servlet.xml加载它自己的配置)。只需将其从contextConfigLocation参数中删除,它可能会解决一些奇怪的行为。
标签: spring-mvc spring-security tiles2