Spring Security 3 - AuthenticationSuccessHandler/FailureHandler 不工作
Posted
技术标签:
【中文标题】Spring Security 3 - AuthenticationSuccessHandler/FailureHandler 不工作【英文标题】:Spring Security 3 - AuthenticationSuccessHandler/FailureHandler Not Working 【发布时间】:2011-09-19 20:46:41 【问题描述】:我正在使用 Spring Security 3 对用户进行身份验证,并且我正在使用弹出窗口中的表单来允许用户输入他们的凭据。我正在使用 Ajax 来处理登录。登录过程效果很好,但是我无法让 Spring Security 将其重定向到我的配置中指定的成功/失败处理程序。相反,它总是返回站点的主页 (home.do)。我知道 Spring Security 正确地对用户进行身份验证,因为当我重新加载页面时,如果用户未通过身份验证,则会出现隐藏的链接(而之前显然没有)。
有人知道为什么会这样吗?我的配置如下。如果需要更多信息,请告诉我。
身份验证配置.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.3.xsd">
<bean id="customUserDetailsService" class="com.mydomain.services.AuthenticationService">
<property name="userDAO">
<bean class="com.mydomain.dao.UserDAOImpl" />
</property>
</bean>
<security:http auto-config='true' use-expressions="true">
<security:http-basic />
<security:logout logout-url="/logout"
logout-success-url="/home.do" />
<security:session-management
invalid-session-url="/sessionTimeout.htm" />
</security:http>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider
user-service-ref='customUserDetailsService'>
</security:authentication-provider>
</security:authentication-manager>
<bean id="authenticationFilter"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<property name="filterProcessesUrl" value="/j_spring_security_check" />
<property name="authenticationManager" ref="authenticationManager" />
<property name="authenticationFailureHandler" ref="failureHandler" />
<property name="authenticationSuccessHandler" ref="successHandler" />
</bean>
<bean id="successHandler"
class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
<property name="alwaysUseDefaultTargetUrl" value="false"/>
<property name="defaultTargetUrl" value="/loginSuccess.do" />
</bean>
<bean id="failureHandler"
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<property name="defaultFailureUrl" value="/login.jsp" />
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>MyWebApp</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:services-config.xml
classpath:authentication-config.xml
</param-value>
</context-param>
<welcome-file-list>
<welcome-file>home.do</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Resource Servlet</servlet-name>
<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
</servlet>
<!-- Map all /resources requests to the Resource Servlet for handling -->
<servlet-mapping>
<servlet-name>Resource Servlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
<!-- security configuration -->
<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>
<!-- end of security configuration -->
</web-app>
【问题讨论】:
【参考方案1】:XHR 正在获得重定向并跟随它。
请参阅this question 的答案,了解下一步操作的示例。
【讨论】:
非常感谢您的回复。我不确定这是怎么回事。返回的数据是 home.do 的整个文本(我什至不在 home.do 上……我从内页登录)。此外,响应代码是 200,而不是您建议的帖子中提到的其他代码之一。此外,我尝试使用表单登录,虽然登录再次成功,但我只是被引导回到 home.do。即使留给浏览器也不会发生重定向。当然,我可能是错的......以前发生过!以上是关于Spring Security 3 - AuthenticationSuccessHandler/FailureHandler 不工作的主要内容,如果未能解决你的问题,请参考以下文章
Spring Security入门(3-7)Spring Security处理页面的ajax请求
Spring Security入门(3-8)Spring Security获取session中的UserDetail
Spring Security入门(3-6)Spring Security 的鉴权 - 自定义权限前缀
Spring Security入门(3-4)Spring Security 异常处理异常传递和异常获取
Spring 框架 4.0 和 Spring security 3.2.4 上的 Spring Security SAML 扩展