Spring安全注销转到j_spring_security_logout

时间:2014-03-24 08:13:13

标签: java spring spring-security logout spring-3

在我的Web应用程序中,当我尝试注销时,它转到j_spring_security_logout而不是给定的页面。 在我的spring-security.xml页面中,我添加了

<logout logout-success-url="/login" delete-cookies="JSESSIONID" />

当我使用spring security 3.1.4.RELEASE 版本时,问题就是这个问题。 现在我使用 3.2.2.RELEASE

我也尝试过以下方法。没有工作

<logout logout-url="/logout" delete-cookies="JSESSIONID" />

弹簧security.xml文件

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" 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.xsd">

<http auto-config='true'>
    <intercept-url pattern="/login*" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <intercept-url pattern="/**" access="ROLE_USER" />
    <form-login login-page="/login" default-target-url="/transaction-view"
        always-use-default-target="true" authentication-failure-url="/loginfailed" />
    <logout logout-url="/logout" logout-success-url="/login.jsp" delete-cookies="JSESSIONID" />
    <session-management invalid-session-url="/invalidSession.htm">
        <concurrency-control max-sessions="1"
            error-if-maximum-exceeded="true" /> <!--this will throw error to second login attempt -->
    </session-management>
    <!-- <custom-filter before="FORM_LOGIN_FILTER" ref="myFilter" /> -->
    <csrf />
</http>

<beans:bean id="customSecurityService"
    class="com.fg.monitoringtool.web.security.SecurityService"></beans:bean>
<beans:bean id="passwordEncoder"
    class="com.fg.monitoringtool.web.security.PasswordEncoderMD5"></beans:bean>


<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="customSecurityService">
        <password-encoder ref="passwordEncoder">
        </password-encoder>
    </authentication-provider>

</authentication-manager>

提前致谢。

2 个答案:

答案 0 :(得分:36)

如果启用了Spring Security CSRF保护,则必须使用POST注销:

<c:url var="logoutUrl" value="/logout"/>
<form action="${logoutUrl}" method="post">
  <input type="submit" value="Log out" />
  <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>

答案 1 :(得分:1)

使用默认注销网址的更好方法是

<c:url var="logoutUrl" value="j_spring_security_logout"/>
<form action="${logoutUrl}" method="post">
  <input type="submit" value="Log out" />
  <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>